From a745fa6f43b2499bb1a7b19b562bd9f988b0ac08 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 22 Jun 2022 16:24:54 -0400 Subject: [PATCH] Remove update and read site config. Fixes #2306 (#2329) * Remove update and read site config. Fixes #2306 * Removing lazy_static, removing Settings::get() --- crates/api/src/lib.rs | 10 ++---- crates/api/src/local_user/ban_person.rs | 2 +- crates/api/src/local_user/get_captcha.rs | 2 +- crates/api/src/local_user/reset_password.rs | 2 +- crates/api/src/local_user/save_settings.rs | 2 +- crates/api/src/local_user/verify_email.rs | 2 +- crates/api/src/site/config/mod.rs | 2 -- crates/api/src/site/config/read.rs | 31 ----------------- crates/api/src/site/config/update.rs | 33 ------------------- crates/api/src/site/leave_admin.rs | 3 +- crates/api/src/site/mod.rs | 1 - crates/api/src/site/purge/community.rs | 6 ++-- crates/api/src/site/purge/person.rs | 6 ++-- crates/api/src/site/purge/post.rs | 4 +-- .../site/registration_applications/approve.rs | 2 +- crates/api_common/src/request.rs | 6 ++-- crates/api_common/src/site.rs | 16 --------- crates/api_crud/src/post/create.rs | 2 +- crates/api_crud/src/post/update.rs | 2 +- crates/api_crud/src/private_message/create.rs | 2 +- crates/api_crud/src/site/create.rs | 3 +- crates/api_crud/src/site/read.rs | 3 +- crates/api_crud/src/user/create.rs | 9 ++--- crates/api_crud/src/user/delete.rs | 2 +- .../apub/src/activities/block/block_user.rs | 6 ++-- .../src/activities/deletion/delete_user.rs | 2 +- crates/apub/src/fetcher/mod.rs | 4 +-- crates/apub/src/http/site.rs | 8 +++-- crates/apub/src/lib.rs | 12 +++++-- crates/apub/src/objects/comment.rs | 4 +-- crates/apub/src/objects/community.rs | 4 +-- crates/apub/src/objects/instance.rs | 2 +- crates/apub/src/objects/mod.rs | 8 ++--- crates/apub/src/objects/person.rs | 2 +- crates/apub/src/objects/post.rs | 8 ++--- crates/apub/src/objects/private_message.rs | 3 +- crates/apub/src/protocol/objects/group.rs | 2 +- crates/utils/src/settings/mod.rs | 32 +++--------------- crates/utils/src/test.rs | 8 ++--- crates/websocket/src/lib.rs | 12 +++---- crates/websocket/src/send.rs | 6 ++-- src/api_routes.rs | 4 +-- src/main.rs | 4 +-- 43 files changed, 84 insertions(+), 200 deletions(-) delete mode 100644 crates/api/src/site/config/mod.rs delete mode 100644 crates/api/src/site/config/read.rs delete mode 100644 crates/api/src/site/config/update.rs diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 5083b286..2974103e 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -98,12 +98,6 @@ pub async fn match_websocket_operation( // Site ops UserOperation::GetModlog => do_websocket_operation::(context, id, op, data).await, - UserOperation::GetSiteConfig => { - do_websocket_operation::(context, id, op, data).await - } - UserOperation::SaveSiteConfig => { - do_websocket_operation::(context, id, op, data).await - } UserOperation::PurgePerson => { do_websocket_operation::(context, id, op, data).await } @@ -226,13 +220,13 @@ mod tests { traits::Crud, utils::establish_unpooled_connection, }; - use lemmy_utils::{claims::Claims, settings::structs::Settings}; + use lemmy_utils::{claims::Claims, settings::SETTINGS}; #[test] fn test_should_not_validate_user_token_after_password_change() { let conn = establish_unpooled_connection(); let secret = Secret::init(&conn).unwrap(); - let settings = Settings::init().unwrap(); + let settings = &SETTINGS.to_owned(); let new_person = PersonForm { name: "Gerry9812".into(), diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index e9900985..cc239cac 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -52,7 +52,7 @@ impl Perform for BanPerson { remove_user_data( person.id, context.pool(), - &context.settings(), + context.settings(), context.client(), ) .await?; diff --git a/crates/api/src/local_user/get_captcha.rs b/crates/api/src/local_user/get_captcha.rs index 2740036c..b6355036 100644 --- a/crates/api/src/local_user/get_captcha.rs +++ b/crates/api/src/local_user/get_captcha.rs @@ -17,7 +17,7 @@ impl Perform for GetCaptcha { context: &Data, _websocket_id: Option, ) -> Result { - let captcha_settings = context.settings().captcha; + let captcha_settings = &context.settings().captcha; if !captcha_settings.enabled { return Ok(GetCaptchaResponse { ok: None }); diff --git a/crates/api/src/local_user/reset_password.rs b/crates/api/src/local_user/reset_password.rs index 98112b70..14071cb6 100644 --- a/crates/api/src/local_user/reset_password.rs +++ b/crates/api/src/local_user/reset_password.rs @@ -29,7 +29,7 @@ impl Perform for PasswordReset { .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?; // Email the pure token to the user. - send_password_reset_email(&local_user_view, context.pool(), &context.settings()).await?; + send_password_reset_email(&local_user_view, context.pool(), context.settings()).await?; Ok(PasswordResetResponse {}) } } diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index eba76907..ec343214 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -48,7 +48,7 @@ impl Perform for SaveUserSettings { let previous_email = local_user_view.local_user.email.clone().unwrap_or_default(); // Only send the verification email if there was an email change if previous_email.ne(email) { - send_verification_email(&local_user_view, email, context.pool(), &context.settings()) + send_verification_email(&local_user_view, email, context.pool(), context.settings()) .await?; } } diff --git a/crates/api/src/local_user/verify_email.rs b/crates/api/src/local_user/verify_email.rs index 7f179726..63a99891 100644 --- a/crates/api/src/local_user/verify_email.rs +++ b/crates/api/src/local_user/verify_email.rs @@ -49,7 +49,7 @@ impl Perform for VerifyEmail { }) .await??; - send_email_verification_success(&local_user_view, &context.settings())?; + send_email_verification_success(&local_user_view, context.settings())?; blocking(context.pool(), move |conn| { EmailVerification::delete_old_tokens_for_local_user(conn, local_user_id) diff --git a/crates/api/src/site/config/mod.rs b/crates/api/src/site/config/mod.rs deleted file mode 100644 index d538ff20..00000000 --- a/crates/api/src/site/config/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod read; -mod update; diff --git a/crates/api/src/site/config/read.rs b/crates/api/src/site/config/read.rs deleted file mode 100644 index 4f82e125..00000000 --- a/crates/api/src/site/config/read.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::Perform; -use actix_web::web::Data; -use lemmy_api_common::{ - site::{GetSiteConfig, GetSiteConfigResponse}, - utils::{get_local_user_view_from_jwt, is_admin}, -}; -use lemmy_utils::{error::LemmyError, settings::structs::Settings, ConnectionId}; -use lemmy_websocket::LemmyContext; - -#[async_trait::async_trait(?Send)] -impl Perform for GetSiteConfig { - type Response = GetSiteConfigResponse; - - #[tracing::instrument(skip(context, _websocket_id))] - async fn perform( - &self, - context: &Data, - _websocket_id: Option, - ) -> Result { - let data: &GetSiteConfig = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; - - // Only let admins read this - is_admin(&local_user_view)?; - - let config_hjson = Settings::read_config_file()?; - - Ok(GetSiteConfigResponse { config_hjson }) - } -} diff --git a/crates/api/src/site/config/update.rs b/crates/api/src/site/config/update.rs deleted file mode 100644 index 4d4f064e..00000000 --- a/crates/api/src/site/config/update.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::Perform; -use actix_web::web::Data; -use lemmy_api_common::{ - site::{GetSiteConfigResponse, SaveSiteConfig}, - utils::{get_local_user_view_from_jwt, is_admin}, -}; -use lemmy_utils::{error::LemmyError, settings::structs::Settings, ConnectionId}; -use lemmy_websocket::LemmyContext; - -#[async_trait::async_trait(?Send)] -impl Perform for SaveSiteConfig { - type Response = GetSiteConfigResponse; - - #[tracing::instrument(skip(context, _websocket_id))] - async fn perform( - &self, - context: &Data, - _websocket_id: Option, - ) -> Result { - let data: &SaveSiteConfig = self; - let local_user_view = - get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; - - // Only let admins read this - is_admin(&local_user_view)?; - - // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem - let config_hjson = Settings::save_config_file(&data.config_hjson) - .map_err(|e| e.with_message("couldnt_update_site"))?; - - Ok(GetSiteConfigResponse { config_hjson }) - } -} diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index 55b49fc7..19cca5cc 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -57,8 +57,7 @@ impl Perform for LeaveAdmin { let site_view = blocking(context.pool(), SiteView::read_local).await??; let admins = blocking(context.pool(), PersonViewSafe::admins).await??; - let federated_instances = - build_federated_instances(context.pool(), &context.settings()).await?; + let federated_instances = build_federated_instances(context.pool(), context.settings()).await?; Ok(GetSiteResponse { site_view: Some(site_view), diff --git a/crates/api/src/site/mod.rs b/crates/api/src/site/mod.rs index 1c860cb4..531c0508 100644 --- a/crates/api/src/site/mod.rs +++ b/crates/api/src/site/mod.rs @@ -1,4 +1,3 @@ -mod config; mod leave_admin; mod mod_log; mod purge; diff --git a/crates/api/src/site/purge/community.rs b/crates/api/src/site/purge/community.rs index 9acb2a82..5f1e3888 100644 --- a/crates/api/src/site/purge/community.rs +++ b/crates/api/src/site/purge/community.rs @@ -41,13 +41,13 @@ impl Perform for PurgeCommunity { .await??; if let Some(banner) = community.banner { - purge_image_from_pictrs(context.client(), &context.settings(), &banner) + purge_image_from_pictrs(context.client(), context.settings(), &banner) .await .ok(); } if let Some(icon) = community.icon { - purge_image_from_pictrs(context.client(), &context.settings(), &icon) + purge_image_from_pictrs(context.client(), context.settings(), &icon) .await .ok(); } @@ -55,7 +55,7 @@ impl Perform for PurgeCommunity { purge_image_posts_for_community( community_id, context.pool(), - &context.settings(), + context.settings(), context.client(), ) .await?; diff --git a/crates/api/src/site/purge/person.rs b/crates/api/src/site/purge/person.rs index fe875858..350b9a47 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -37,13 +37,13 @@ impl Perform for PurgePerson { let person = blocking(context.pool(), move |conn| Person::read(conn, person_id)).await??; if let Some(banner) = person.banner { - purge_image_from_pictrs(context.client(), &context.settings(), &banner) + purge_image_from_pictrs(context.client(), context.settings(), &banner) .await .ok(); } if let Some(avatar) = person.avatar { - purge_image_from_pictrs(context.client(), &context.settings(), &avatar) + purge_image_from_pictrs(context.client(), context.settings(), &avatar) .await .ok(); } @@ -51,7 +51,7 @@ impl Perform for PurgePerson { purge_image_posts_for_person( person_id, context.pool(), - &context.settings(), + context.settings(), context.client(), ) .await?; diff --git a/crates/api/src/site/purge/post.rs b/crates/api/src/site/purge/post.rs index 99080083..b7e13ade 100644 --- a/crates/api/src/site/purge/post.rs +++ b/crates/api/src/site/purge/post.rs @@ -39,13 +39,13 @@ impl Perform for PurgePost { // Purge image if let Some(url) = post.url { - purge_image_from_pictrs(context.client(), &context.settings(), &url) + purge_image_from_pictrs(context.client(), context.settings(), &url) .await .ok(); } // Purge thumbnail if let Some(thumbnail_url) = post.thumbnail_url { - purge_image_from_pictrs(context.client(), &context.settings(), &thumbnail_url) + purge_image_from_pictrs(context.client(), context.settings(), &thumbnail_url) .await .ok(); } diff --git a/crates/api/src/site/registration_applications/approve.rs b/crates/api/src/site/registration_applications/approve.rs index 04000b63..5ebf812d 100644 --- a/crates/api/src/site/registration_applications/approve.rs +++ b/crates/api/src/site/registration_applications/approve.rs @@ -66,7 +66,7 @@ impl Perform for ApproveRegistrationApplication { .await??; if approved_local_user_view.local_user.email.is_some() { - send_application_approved_email(&approved_local_user_view, &context.settings())?; + send_application_approved_email(&approved_local_user_view, context.settings())?; } } diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 37bb39a0..1bc81861 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -262,15 +262,15 @@ pub fn build_user_agent(settings: &Settings) -> String { #[cfg(test)] mod tests { use crate::request::{build_user_agent, fetch_site_metadata, SiteMetadata}; - use lemmy_utils::settings::structs::Settings; + use lemmy_utils::settings::SETTINGS; use url::Url; // These helped with testing #[actix_rt::test] async fn test_site_metadata() { - let settings = Settings::init().unwrap(); + let settings = &SETTINGS.to_owned(); let client = reqwest::Client::builder() - .user_agent(build_user_agent(&settings)) + .user_agent(build_user_agent(settings)) .build() .unwrap() .into(); diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index f61d3405..8a0e16a9 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -179,22 +179,6 @@ pub struct LeaveAdmin { pub auth: Sensitive, } -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct GetSiteConfig { - pub auth: Sensitive, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct GetSiteConfigResponse { - pub config_hjson: String, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct SaveSiteConfig { - pub config_hjson: String, - pub auth: Sensitive, -} - #[derive(Debug, Serialize, Deserialize, Clone)] pub struct FederatedInstances { pub linked: Vec, diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 9151e3ba..191951ed 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -87,7 +87,7 @@ impl PerformCrud for CreatePost { // Fetch post links and pictrs cached image let data_url = data.url.as_ref(); let (metadata_res, thumbnail_url) = - fetch_site_data(context.client(), &context.settings(), data_url).await; + fetch_site_data(context.client(), context.settings(), data_url).await; let (embed_title, embed_description, embed_video_url) = metadata_res .map(|u| (u.title, u.description, u.embed_video_url)) .unwrap_or_default(); diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 3f3e0c2a..ac4910cd 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -70,7 +70,7 @@ impl PerformCrud for EditPost { // Fetch post links and Pictrs cached image let data_url = data.url.as_ref(); let (metadata_res, thumbnail_url) = - fetch_site_data(context.client(), &context.settings(), data_url).await; + fetch_site_data(context.client(), context.settings(), data_url).await; let (embed_title, embed_description, embed_video_url) = metadata_res .map(|u| (u.title, u.description, u.embed_video_url)) .unwrap_or_default(); diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index a368e3a5..979c575d 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -119,7 +119,7 @@ impl PerformCrud for CreatePrivateMessage { &content_slurs_removed, &local_recipient.person.name, ), - &context.settings(), + context.settings(), ); } diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 57301e10..05a8c202 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -15,7 +15,6 @@ use lemmy_db_schema::{ use lemmy_db_views::structs::SiteView; use lemmy_utils::{ error::LemmyError, - settings::structs::Settings, utils::{check_slurs, check_slurs_opt}, ConnectionId, }; @@ -57,7 +56,7 @@ impl PerformCrud for CreateSite { site_description_length_check(desc)?; } - let actor_id: DbUrl = Url::parse(&Settings::get().get_protocol_and_hostname())?.into(); + let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into(); let inbox_url = Some(generate_site_inbox_url(&actor_id)?); let keypair = generate_actor_keypair()?; let site_form = SiteForm { diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 1ef84a91..d34be2ca 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -120,8 +120,7 @@ impl PerformCrud for GetSite { None }; - let federated_instances = - build_federated_instances(context.pool(), &context.settings()).await?; + let federated_instances = build_federated_instances(context.pool(), context.settings()).await?; Ok(GetSiteResponse { site_view, diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index ee26509b..edb04892 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -211,13 +211,8 @@ impl PerformCrud for Register { .email .clone() .expect("email was provided"); - send_verification_email( - &local_user_view, - &email, - context.pool(), - &context.settings(), - ) - .await?; + send_verification_email(&local_user_view, &email, context.pool(), context.settings()) + .await?; login_response.verify_email_sent = true; } diff --git a/crates/api_crud/src/user/delete.rs b/crates/api_crud/src/user/delete.rs index 7273d990..59474767 100644 --- a/crates/api_crud/src/user/delete.rs +++ b/crates/api_crud/src/user/delete.rs @@ -36,7 +36,7 @@ impl PerformCrud for DeleteAccount { delete_user_account( local_user_view.person.id, context.pool(), - &context.settings(), + context.settings(), context.client(), ) .await?; diff --git a/crates/apub/src/activities/block/block_user.rs b/crates/apub/src/activities/block/block_user.rs index 32feea60..a18002f1 100644 --- a/crates/apub/src/activities/block/block_user.rs +++ b/crates/apub/src/activities/block/block_user.rs @@ -37,7 +37,7 @@ use lemmy_db_schema::{ }, traits::{Bannable, Crud, Followable}, }; -use lemmy_utils::{error::LemmyError, settings::structs::Settings, utils::convert_datetime}; +use lemmy_utils::{error::LemmyError, utils::convert_datetime}; use lemmy_websocket::LemmyContext; use url::Url; @@ -131,7 +131,7 @@ impl ActivityHandler for BlockUser { { SiteOrCommunity::Site(site) => { let domain = self.object.inner().domain().expect("url needs domain"); - if Settings::get().hostname == domain { + if context.settings().hostname == domain { return Err( anyhow!("Site bans from remote instance can't affect user's home instance").into(), ); @@ -184,7 +184,7 @@ impl ActivityHandler for BlockUser { remove_user_data( blocked_person.id, context.pool(), - &context.settings(), + context.settings(), context.client(), ) .await?; diff --git a/crates/apub/src/activities/deletion/delete_user.rs b/crates/apub/src/activities/deletion/delete_user.rs index 0e342bd1..9570d85d 100644 --- a/crates/apub/src/activities/deletion/delete_user.rs +++ b/crates/apub/src/activities/deletion/delete_user.rs @@ -54,7 +54,7 @@ impl ActivityHandler for DeleteUser { delete_user_account( actor.id, context.pool(), - &context.settings(), + context.settings(), context.client(), ) .await?; diff --git a/crates/apub/src/fetcher/mod.rs b/crates/apub/src/fetcher/mod.rs index 274c31d4..072cf7dc 100644 --- a/crates/apub/src/fetcher/mod.rs +++ b/crates/apub/src/fetcher/mod.rs @@ -3,7 +3,7 @@ use activitypub_federation::traits::ApubObject; use itertools::Itertools; use lemmy_api_common::utils::blocking; use lemmy_db_schema::traits::ApubActor; -use lemmy_utils::{error::LemmyError, settings::structs::Settings}; +use lemmy_utils::error::LemmyError; use lemmy_websocket::LemmyContext; pub mod post_or_comment; @@ -35,7 +35,7 @@ where .collect_tuple() .expect("invalid query"); let name = name.to_string(); - let domain = format!("{}://{}", Settings::get().get_protocol_string(), domain); + let domain = format!("{}://{}", context.settings().get_protocol_string(), domain); let actor = blocking(context.pool(), move |conn| { DbActor::read_from_name_and_domain(conn, &name, &domain) }) diff --git a/crates/apub/src/http/site.rs b/crates/apub/src/http/site.rs index 3d832738..1fa66215 100644 --- a/crates/apub/src/http/site.rs +++ b/crates/apub/src/http/site.rs @@ -8,7 +8,7 @@ use activitypub_federation::{deser::context::WithContext, traits::ApubObject}; use actix_web::{web, HttpRequest, HttpResponse}; use lemmy_api_common::utils::blocking; use lemmy_db_schema::source::site::Site; -use lemmy_utils::{error::LemmyError, settings::structs::Settings}; +use lemmy_utils::error::LemmyError; use lemmy_websocket::LemmyContext; use url::Url; @@ -24,10 +24,12 @@ pub(crate) async fn get_apub_site_http( } #[tracing::instrument(skip_all)] -pub(crate) async fn get_apub_site_outbox() -> Result { +pub(crate) async fn get_apub_site_outbox( + context: web::Data, +) -> Result { let outbox_id = format!( "{}/site_outbox", - Settings::get().get_protocol_and_hostname() + context.settings().get_protocol_and_hostname() ); let outbox = EmptyOutbox::new(Url::parse(&outbox_id)?).await?; Ok(create_apub_response(&outbox)) diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 2f014e0d..64cfa0f8 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -8,7 +8,11 @@ use activitypub_federation::{ use anyhow::Context; use lemmy_api_common::utils::blocking; use lemmy_db_schema::{newtypes::DbUrl, source::activity::Activity, utils::DbPool}; -use lemmy_utils::{error::LemmyError, location_info, settings::structs::Settings}; +use lemmy_utils::{ + error::LemmyError, + location_info, + settings::{structs::Settings, SETTINGS}, +}; use lemmy_websocket::LemmyContext; use once_cell::sync::{Lazy, OnceCell}; use url::{ParseError, Url}; @@ -34,11 +38,13 @@ fn local_instance(context: &LemmyContext) -> &'static LocalInstance { .http_fetch_retry_limit(context.settings().federation.http_fetch_retry_limit) .worker_count(context.settings().federation.worker_count) .debug(context.settings().federation.debug) - .verify_url_function(|url| check_apub_id_valid(url, &Settings::get())) + // TODO No idea why, but you can't pass context.settings() to the verify_url_function closure + // without the value getting captured. + .verify_url_function(|url| check_apub_id_valid(url, &SETTINGS.to_owned())) .build() .expect("configure federation"); LocalInstance::new( - context.settings().hostname, + context.settings().hostname.to_owned(), context.client().clone(), settings, ) diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index f898c365..8885fe3b 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -142,8 +142,8 @@ impl ApubObject for ApubComment { Community::read(conn, community_id) }) .await??; - check_apub_id_valid_with_strictness(note.id.inner(), community.local, &context.settings())?; - verify_is_remote_object(note.id.inner())?; + check_apub_id_valid_with_strictness(note.id.inner(), community.local, context.settings())?; + verify_is_remote_object(note.id.inner(), context.settings())?; verify_person_in_community( ¬e.attributed_to, &community.into(), diff --git a/crates/apub/src/objects/community.rs b/crates/apub/src/objects/community.rs index ad75d37c..6e2f409a 100644 --- a/crates/apub/src/objects/community.rs +++ b/crates/apub/src/objects/community.rs @@ -206,9 +206,7 @@ impl ApubCommunity { .unique() .filter(|inbox: &Url| inbox.host_str() != Some(&context.settings().hostname)) // Don't send to blocked instances - .filter(|inbox| { - check_apub_id_valid_with_strictness(inbox, false, &context.settings()).is_ok() - }) + .filter(|inbox| check_apub_id_valid_with_strictness(inbox, false, context.settings()).is_ok()) .collect(); Ok(inboxes) diff --git a/crates/apub/src/objects/instance.rs b/crates/apub/src/objects/instance.rs index 1ae5e735..5e804221 100644 --- a/crates/apub/src/objects/instance.rs +++ b/crates/apub/src/objects/instance.rs @@ -103,7 +103,7 @@ impl ApubObject for ApubSite { data: &Self::DataType, _request_counter: &mut i32, ) -> Result<(), LemmyError> { - check_apub_id_valid_with_strictness(apub.id.inner(), true, &data.settings())?; + check_apub_id_valid_with_strictness(apub.id.inner(), true, data.settings())?; verify_domains_match(expected_domain, apub.id.inner())?; let slur_regex = &data.settings().slur_regex(); diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index 3b7aab2c..7293fc36 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -43,8 +43,8 @@ pub(crate) fn read_from_string_or_source_opt( /// wrapped in Announce. If we simply receive this like any other federated object, overwrite the /// existing, local Post. In particular, it will set the field local = false, so that the object /// can't be fetched from the Activitypub HTTP endpoint anymore (which only serves local objects). -pub(crate) fn verify_is_remote_object(id: &Url) -> Result<(), LemmyError> { - let local_domain = Settings::get().get_hostname_without_port()?; +pub(crate) fn verify_is_remote_object(id: &Url, settings: &Settings) -> Result<(), LemmyError> { + let local_domain = settings.get_hostname_without_port()?; if id.domain() == Some(&local_domain) { Err(anyhow!("cant accept local object from remote instance").into()) } else { @@ -68,7 +68,7 @@ pub(crate) mod tests { use lemmy_utils::{ error::LemmyError, rate_limit::{rate_limiter::RateLimiter, RateLimit}, - settings::structs::Settings, + settings::SETTINGS, }; use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use parking_lot::Mutex; @@ -96,7 +96,7 @@ pub(crate) mod tests { pub(crate) fn init_context() -> LemmyContext { // call this to run migrations establish_unpooled_connection(); - let settings = Settings::init().unwrap(); + let settings = SETTINGS.to_owned(); let rate_limiter = RateLimit { rate_limiter: Arc::new(Mutex::new(RateLimiter::default())), rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(), diff --git a/crates/apub/src/objects/person.rs b/crates/apub/src/objects/person.rs index d7e346aa..5d47fbc0 100644 --- a/crates/apub/src/objects/person.rs +++ b/crates/apub/src/objects/person.rs @@ -120,7 +120,7 @@ impl ApubObject for ApubPerson { _request_counter: &mut i32, ) -> Result<(), LemmyError> { verify_domains_match(person.id.inner(), expected_domain)?; - check_apub_id_valid_with_strictness(person.id.inner(), false, &context.settings())?; + check_apub_id_valid_with_strictness(person.id.inner(), false, context.settings())?; let slur_regex = &context.settings().slur_regex(); check_slurs(&person.preferred_username, slur_regex)?; diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 24424b9f..08219872 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -19,6 +19,7 @@ use activitystreams_kinds::public; use chrono::NaiveDateTime; use lemmy_api_common::{request::fetch_site_data, utils::blocking}; use lemmy_db_schema::{ + self, source::{ community::Community, moderator::{ModLockPost, ModLockPostForm, ModStickyPost, ModStickyPostForm}, @@ -26,7 +27,6 @@ use lemmy_db_schema::{ post::{Post, PostForm}, }, traits::Crud, - {self}, }; use lemmy_utils::{ error::LemmyError, @@ -132,11 +132,11 @@ impl ApubObject for ApubPost { // instance from the post author. if !page.is_mod_action(context).await? { verify_domains_match(page.id.inner(), expected_domain)?; - verify_is_remote_object(page.id.inner())?; + verify_is_remote_object(page.id.inner(), context.settings())?; }; let community = page.extract_community(context, request_counter).await?; - check_apub_id_valid_with_strictness(page.id.inner(), community.local, &context.settings())?; + check_apub_id_valid_with_strictness(page.id.inner(), community.local, context.settings())?; verify_person_in_community(&page.creator()?, &community, context, request_counter).await?; check_slurs(&page.name, &context.settings().slur_regex())?; verify_domains_match(page.creator()?.inner(), page.id.inner())?; @@ -168,7 +168,7 @@ impl ApubObject for ApubPost { page.url }; let (metadata_res, thumbnail_url) = if let Some(url) = &url { - fetch_site_data(context.client(), &context.settings(), Some(url)).await + fetch_site_data(context.client(), context.settings(), Some(url)).await } else { (None, page.image.map(|i| i.url.into())) }; diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index aab069e8..fd3f5135 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -24,7 +24,6 @@ use lemmy_db_schema::{ }; use lemmy_utils::{ error::LemmyError, - settings::structs::Settings, utils::{convert_datetime, markdown_to_html}, }; use lemmy_websocket::LemmyContext; @@ -109,7 +108,7 @@ impl ApubObject for ApubPrivateMessage { ) -> Result<(), LemmyError> { verify_domains_match(note.id.inner(), expected_domain)?; verify_domains_match(note.attributed_to.inner(), note.id.inner())?; - check_apub_id_valid_with_strictness(note.id.inner(), false, &Settings::get())?; + check_apub_id_valid_with_strictness(note.id.inner(), false, context.settings())?; let person = note .attributed_to .dereference(context, local_instance(context), request_counter) diff --git a/crates/apub/src/protocol/objects/group.rs b/crates/apub/src/protocol/objects/group.rs index ba01e12d..40409f64 100644 --- a/crates/apub/src/protocol/objects/group.rs +++ b/crates/apub/src/protocol/objects/group.rs @@ -63,7 +63,7 @@ impl Group { expected_domain: &Url, context: &LemmyContext, ) -> Result<(), LemmyError> { - check_apub_id_valid_with_strictness(self.id.inner(), true, &context.settings())?; + check_apub_id_valid_with_strictness(self.id.inner(), true, context.settings())?; verify_domains_match(expected_domain, self.id.inner())?; let slur_regex = &context.settings().slur_regex(); diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index 513f7ded..f095baf1 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -7,18 +7,18 @@ use anyhow::{anyhow, Context}; use deser_hjson::from_str; use once_cell::sync::Lazy; use regex::{Regex, RegexBuilder}; -use std::{env, fs, io::Error, sync::RwLock}; +use std::{env, fs, io::Error}; pub mod structs; static DEFAULT_CONFIG_FILE: &str = "config/config.hjson"; -static SETTINGS: Lazy> = - Lazy::new(|| RwLock::new(Settings::init().expect("Failed to load settings file"))); +pub static SETTINGS: Lazy = + Lazy::new(|| Settings::init().expect("Failed to load settings file")); static WEBFINGER_REGEX: Lazy = Lazy::new(|| { Regex::new(&format!( "^acct:([a-zA-Z0-9_]{{3,}})@{}$", - Settings::get().hostname + SETTINGS.hostname )) .expect("compile webfinger regex") }); @@ -29,7 +29,7 @@ impl Settings { /// Note: The env var `LEMMY_DATABASE_URL` is parsed in /// `lemmy_db_schema/src/lib.rs::get_database_url_from_env()` /// Warning: Only call this once. - pub fn init() -> Result { + pub(crate) fn init() -> Result { // Read the config file let config = from_str::(&Self::read_config_file()?)?; @@ -40,11 +40,6 @@ impl Settings { Ok(config) } - /// Returns the config as a struct. - pub fn get() -> Self { - SETTINGS.read().expect("read config").to_owned() - } - pub fn get_database_url(&self) -> String { let conf = &self.database; format!( @@ -91,23 +86,6 @@ impl Settings { ) } - pub fn save_config_file(data: &str) -> Result { - // check that the config is valid - from_str::(data)?; - - fs::write(Settings::get_config_location(), data)?; - - // Reload the new settings - // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804 - let mut new_settings = SETTINGS.write().expect("write config"); - *new_settings = match Settings::init() { - Ok(c) => c, - Err(e) => panic!("{}", e), - }; - - Ok(Self::read_config_file()?) - } - pub fn webfinger_regex(&self) -> Regex { WEBFINGER_REGEX.to_owned() } diff --git a/crates/utils/src/test.rs b/crates/utils/src/test.rs index a59d0412..e887bae4 100644 --- a/crates/utils/src/test.rs +++ b/crates/utils/src/test.rs @@ -1,5 +1,5 @@ use crate::{ - settings::structs::Settings, + settings::SETTINGS, utils::{ is_valid_actor_name, is_valid_display_name, @@ -24,7 +24,7 @@ fn test_mentions_regex() { #[test] fn test_valid_actor_name() { - let actor_name_max_length = Settings::init().unwrap().actor_name_max_length; + let actor_name_max_length = SETTINGS.actor_name_max_length; assert!(is_valid_actor_name("Hello_98", actor_name_max_length)); assert!(is_valid_actor_name("ten", actor_name_max_length)); assert!(!is_valid_actor_name("Hello-98", actor_name_max_length)); @@ -34,7 +34,7 @@ fn test_valid_actor_name() { #[test] fn test_valid_display_name() { - let actor_name_max_length = Settings::init().unwrap().actor_name_max_length; + let actor_name_max_length = SETTINGS.actor_name_max_length; assert!(is_valid_display_name("hello @there", actor_name_max_length)); assert!(!is_valid_display_name( "@hello there", @@ -65,7 +65,7 @@ fn test_valid_matrix_id() { #[test] fn test_slur_filter() { - let slur_regex = Settings::init().unwrap().slur_regex(); + let slur_regex = SETTINGS.slur_regex(); let test = "faggot test kike tranny cocksucker retardeds. Capitalized Niggerz. This is a bunch of other safe text."; let slur_free = "No slurs here"; diff --git a/crates/websocket/src/lib.rs b/crates/websocket/src/lib.rs index bac223b6..9aff6b7f 100644 --- a/crates/websocket/src/lib.rs +++ b/crates/websocket/src/lib.rs @@ -4,7 +4,10 @@ extern crate strum_macros; use crate::chat_server::ChatServer; use actix::Addr; use lemmy_db_schema::{source::secret::Secret, utils::DbPool}; -use lemmy_utils::{error::LemmyError, settings::structs::Settings}; +use lemmy_utils::{ + error::LemmyError, + settings::{structs::Settings, SETTINGS}, +}; use reqwest_middleware::ClientWithMiddleware; use serde::Serialize; @@ -47,9 +50,8 @@ impl LemmyContext { pub fn client(&self) -> &ClientWithMiddleware { &self.client } - pub fn settings(&self) -> Settings { - // TODO hacky solution to be able to hotload the settings. - Settings::get() + pub fn settings(&self) -> &'static Settings { + &SETTINGS } pub fn secret(&self) -> &Secret { &self.secret @@ -133,8 +135,6 @@ pub enum UserOperation { PasswordChange, MarkPrivateMessageAsRead, UserJoin, - GetSiteConfig, - SaveSiteConfig, PostJoin, CommunityJoin, ModJoin, diff --git a/crates/websocket/src/send.rs b/crates/websocket/src/send.rs index 7c6820c9..fbc88962 100644 --- a/crates/websocket/src/send.rs +++ b/crates/websocket/src/send.rs @@ -217,7 +217,7 @@ pub async fn send_local_notifs( &mention_user_view, &lang.notification_mentioned_by_subject(&person.name), &lang.notification_mentioned_by_body(&comment.content, &inbox_link, &person.name), - &context.settings(), + context.settings(), ) } } @@ -252,7 +252,7 @@ pub async fn send_local_notifs( &parent_user_view, &lang.notification_comment_reply_subject(&person.name), &lang.notification_comment_reply_body(&comment.content, &inbox_link, &person.name), - &context.settings(), + context.settings(), ) } } @@ -282,7 +282,7 @@ pub async fn send_local_notifs( &parent_user_view, &lang.notification_post_reply_subject(&person.name), &lang.notification_post_reply_body(&comment.content, &inbox_link, &person.name), - &context.settings(), + context.settings(), ) } } diff --git a/src/api_routes.rs b/src/api_routes.rs index c9ff8803..36562c88 100644 --- a/src/api_routes.rs +++ b/src/api_routes.rs @@ -18,9 +18,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { .route("", web::get().to(route_get_crud::)) // Admin Actions .route("", web::post().to(route_post_crud::)) - .route("", web::put().to(route_post_crud::)) - .route("/config", web::get().to(route_get::)) - .route("/config", web::put().to(route_post::)), + .route("", web::put().to(route_post_crud::)), ) .service( web::resource("/modlog") diff --git a/src/main.rs b/src/main.rs index 3fbd0c65..3fc3bbc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ use lemmy_server::{ use lemmy_utils::{ error::LemmyError, rate_limit::{rate_limiter::RateLimiter, RateLimit}, - settings::structs::Settings, + settings::{structs::Settings, SETTINGS}, }; use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use parking_lot::Mutex; @@ -54,7 +54,7 @@ async fn main() -> Result<(), LemmyError> { return Ok(()); } - let settings = Settings::init().expect("Couldn't initialize settings."); + let settings = SETTINGS.to_owned(); init_logging(settings.opentelemetry_url.as_deref())?; -- 2.44.1