Add hack to hot reload settings.

remove_settings_and_secret_singletons
Dessalines 2021-09-25 11:27:56 -04:00
parent e012380821
commit d6b87dd687
43 changed files with 73 additions and 56 deletions

View File

@ -121,7 +121,7 @@ impl Perform for GetCaptcha {
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError> {
let captcha_settings = context.settings().to_owned().captcha;
let captcha_settings = context.settings().captcha;
if !captcha_settings.enabled {
return Ok(GetCaptchaResponse { ok: None });
@ -757,7 +757,7 @@ impl Perform for PasswordReset {
email,
&local_user_view.person.name,
html,
context.settings(),
&context.settings(),
)
.map_err(|e| ApiError::err(&e))?;

View File

@ -181,7 +181,7 @@ impl Perform for Search {
let community_actor_id = data
.community_name
.as_ref()
.map(|t| build_actor_id_from_shortname(EndpointType::Community, t, context.settings()).ok())
.map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
.unwrap_or(None);
let creator_id = data.creator_id;
match search_type {

View File

@ -125,7 +125,7 @@ impl PerformCrud for CreateComment {
post,
context.pool(),
true,
context.settings(),
&context.settings(),
)
.await?;

View File

@ -77,7 +77,7 @@ impl PerformCrud for DeleteComment {
post,
context.pool(),
false,
context.settings(),
&context.settings(),
)
.await?;
@ -172,7 +172,7 @@ impl PerformCrud for RemoveComment {
post,
context.pool(),
false,
context.settings(),
&context.settings(),
)
.await?;

View File

@ -32,7 +32,7 @@ impl PerformCrud for GetComments {
let community_actor_id = data
.community_name
.as_ref()
.map(|t| build_actor_id_from_shortname(EndpointType::Community, t, context.settings()).ok())
.map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
.unwrap_or(None);
let saved_only = data.saved_only;
let page = data.page;

View File

@ -83,7 +83,7 @@ impl PerformCrud for EditComment {
orig_comment.post,
context.pool(),
false,
context.settings(),
&context.settings(),
)
.await?;

View File

@ -36,7 +36,7 @@ impl PerformCrud for GetCommunity {
None => {
let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
let community_actor_id =
build_actor_id_from_shortname(EndpointType::Community, &name, context.settings())?;
build_actor_id_from_shortname(EndpointType::Community, &name, &context.settings())?;
blocking(context.pool(), move |conn| {
Community::read_from_apub_id(conn, &community_actor_id)

View File

@ -54,7 +54,7 @@ impl PerformCrud for CreatePost {
// Fetch post links and pictrs cached image
let data_url = data.url.as_ref();
let (metadata_res, pictrs_thumbnail) =
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_html) = metadata_res
.map(|u| (u.title, u.description, u.html))
.unwrap_or((None, None, None));

View File

@ -135,7 +135,7 @@ impl PerformCrud for GetPosts {
let community_actor_id = data
.community_name
.as_ref()
.map(|t| build_actor_id_from_shortname(EndpointType::Community, t, context.settings()).ok())
.map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
.unwrap_or(None);
let saved_only = data.saved_only;

View File

@ -54,7 +54,7 @@ impl PerformCrud for EditPost {
// Fetch post links and Pictrs cached image
let data_url = data.url.as_ref();
let (metadata_res, pictrs_thumbnail) =
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_html) = metadata_res
.map(|u| (u.title, u.description, u.html))
.unwrap_or((None, None, None));

View File

@ -105,7 +105,7 @@ impl PerformCrud for CreatePrivateMessage {
"Private Message from",
"Private Message",
&content_slurs_removed,
context.settings(),
&context.settings(),
);
}

View File

@ -43,7 +43,7 @@ impl PerformCrud for GetPersonDetails {
.to_owned()
.unwrap_or_else(|| "admin".to_string());
let actor_id =
build_actor_id_from_shortname(EndpointType::Person, &name, context.settings())?;
build_actor_id_from_shortname(EndpointType::Person, &name, &context.settings())?;
let person = blocking(context.pool(), move |conn| {
Person::read_from_apub_id(conn, &actor_id)

View File

@ -90,7 +90,7 @@ impl ActivityHandler for CreateOrUpdateComment {
let community = extract_community(&self.cc, context, request_counter).await?;
let community_id = ObjectId::new(community.actor_id());
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &community_id, context, request_counter).await?;
verify_domains_match(self.actor.inner(), self.object.id_unchecked())?;
// TODO: should add a check that the correct community is in cc (probably needs changes to

View File

@ -46,7 +46,7 @@ async fn get_notif_recipients(
post,
context.pool(),
true,
context.settings(),
&context.settings(),
)
.await
}

View File

@ -84,7 +84,7 @@ impl ActivityHandler for AddMod {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
verify_add_remove_moderator_target(&self.target, &self.cc[0])?;

View File

@ -103,7 +103,7 @@ impl ActivityHandler for AnnounceActivity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_community(&self.actor, context, request_counter).await?;
self.object.verify(context, request_counter).await?;
Ok(())

View File

@ -95,7 +95,7 @@ impl ActivityHandler for BlockUserFromCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
Ok(())

View File

@ -20,7 +20,7 @@ async fn list_community_follower_inboxes(
Ok(
vec![
community
.get_follower_inboxes(context.pool(), context.settings())
.get_follower_inboxes(context.pool(), &context.settings())
.await?,
additional_inboxes,
]
@ -28,7 +28,7 @@ async fn list_community_follower_inboxes(
.flatten()
.unique()
.filter(|inbox| inbox.host_str() != Some(&context.settings().hostname))
.filter(|inbox| check_is_apub_id_valid(inbox, false, context.settings()).is_ok())
.filter(|inbox| check_is_apub_id_valid(inbox, false, &context.settings()).is_ok())
.map(|inbox| inbox.to_owned())
.collect(),
)

View File

@ -86,7 +86,7 @@ impl ActivityHandler for RemoveMod {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
if let Some(target) = &self.target {
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;

View File

@ -82,7 +82,7 @@ impl ActivityHandler for UndoBlockUserFromCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
self.object.verify(context, request_counter).await?;

View File

@ -82,7 +82,7 @@ impl ActivityHandler for UpdateCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
Ok(())
@ -102,7 +102,7 @@ impl ActivityHandler for UpdateCommunity {
let updated_community = Group::from_apub_to_form(
&self.object,
&community.actor_id.clone().into(),
context.settings(),
&context.settings(),
)
.await?;
let cf = CommunityForm {

View File

@ -87,7 +87,7 @@ impl ActivityHandler for Delete {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_delete_activity(
&self.object,
self,

View File

@ -59,7 +59,7 @@ impl ActivityHandler for UndoDelete {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
self.object.verify(context, request_counter).await?;
verify_delete_activity(
&self.object.object,

View File

@ -80,7 +80,7 @@ impl ActivityHandler for AcceptFollowCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_urls_match(self.to.inner(), self.object.actor())?;
verify_urls_match(self.actor(), self.object.to.inner())?;
verify_community(&self.actor, context, request_counter).await?;

View File

@ -91,7 +91,7 @@ impl ActivityHandler for FollowCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_urls_match(self.to.inner(), self.object.inner())?;
verify_person(&self.actor, context, request_counter).await?;
Ok(())

View File

@ -74,7 +74,7 @@ impl ActivityHandler for UndoFollowCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_urls_match(self.to.inner(), self.object.object.inner())?;
verify_urls_match(self.actor(), self.object.actor())?;
verify_person(&self.actor, context, request_counter).await?;

View File

@ -86,7 +86,7 @@ impl ActivityHandler for CreateOrUpdatePost {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
let community = self.cc[0].dereference(context, request_counter).await?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
match self.kind {

View File

@ -66,7 +66,7 @@ impl ActivityHandler for CreateOrUpdatePrivateMessage {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person(&self.actor, context, request_counter).await?;
verify_domains_match(self.actor.inner(), self.object.id_unchecked())?;
self.object.verify(context, request_counter).await?;

View File

@ -77,7 +77,7 @@ impl ActivityHandler for DeletePrivateMessage {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person(&self.actor, context, request_counter).await?;
verify_domains_match(self.actor.inner(), &self.object)?;
Ok(())

View File

@ -76,7 +76,7 @@ impl ActivityHandler for UndoDeletePrivateMessage {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person(&self.actor, context, request_counter).await?;
verify_urls_match(self.actor(), self.object.actor())?;
verify_domains_match(self.actor(), &self.object.object)?;

View File

@ -43,7 +43,7 @@ impl ActivityHandler for UndoRemovePostCommentOrCommunity {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
self.object.verify(context, request_counter).await?;
verify_delete_activity(

View File

@ -91,7 +91,7 @@ impl ActivityHandler for UndoVote {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_urls_match(self.actor(), self.object.actor())?;
self.object.verify(context, request_counter).await?;

View File

@ -117,7 +117,7 @@ impl ActivityHandler for Vote {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, context.settings())?;
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
Ok(())
}

View File

@ -75,7 +75,7 @@ pub(crate) async fn fetch_community_outbox(
) -> Result<(), LemmyError> {
let outbox = fetch_remote_object::<OrderedCollection>(
context.client(),
context.settings(),
&context.settings(),
outbox,
recursion_counter,
)
@ -105,7 +105,7 @@ async fn fetch_community_mods(
if let Some(mods_url) = &group.moderators {
let mods = fetch_remote_object::<OrderedCollection>(
context.client(),
context.settings(),
&context.settings(),
mods_url,
recursion_counter,
)

View File

@ -34,7 +34,7 @@ pub(crate) async fn get_or_fetch_and_insert_post(
debug!("Fetching and creating remote post: {}", post_ap_id);
let page = fetch_remote_object::<Page>(
context.client(),
context.settings(),
&context.settings(),
post_ap_id,
recursion_counter,
)
@ -71,7 +71,7 @@ pub(crate) async fn get_or_fetch_and_insert_comment(
);
let comment = fetch_remote_object::<Note>(
context.client(),
context.settings(),
&context.settings(),
comment_ap_id,
recursion_counter,
)

View File

@ -33,7 +33,7 @@ pub(crate) async fn get_or_fetch_and_upsert_person(
debug!("Fetching and updating from remote person: {}", apub_id);
let person = fetch_remote_object::<ApubPerson>(
context.client(),
context.settings(),
&context.settings(),
apub_id,
recursion_counter,
)
@ -65,7 +65,7 @@ pub(crate) async fn get_or_fetch_and_upsert_person(
debug!("Fetching and creating remote person: {}", apub_id);
let person = fetch_remote_object::<ApubPerson>(
context.client(),
context.settings(),
&context.settings(),
apub_id,
recursion_counter,
)

View File

@ -98,7 +98,7 @@ where
if is_activity_already_known(context.pool(), activity.id_unchecked()).await? {
return Ok(HttpResponse::Ok().finish());
}
check_is_apub_id_valid(activity.actor(), false, context.settings())?;
check_is_apub_id_valid(activity.actor(), false, &context.settings())?;
info!("Verifying activity {}", activity.id_unchecked().to_string());
activity.verify(context, request_counter).await?;
assert_activity_not_local(&activity, &context.settings().hostname)?;

View File

@ -178,7 +178,7 @@ impl FromApub for Community {
expected_domain: &Url,
request_counter: &mut i32,
) -> Result<Community, LemmyError> {
let form = Group::from_apub_to_form(group, expected_domain, context.settings()).await?;
let form = Group::from_apub_to_form(group, expected_domain, &context.settings()).await?;
let community = blocking(context.pool(), move |conn| Community::upsert(conn, &form)).await??;
update_community_mods(group, &community, context, request_counter).await?;

View File

@ -155,7 +155,7 @@ impl FromApub for DbPerson {
check_slurs_opt(&display_name, slur_regex)?;
check_slurs_opt(&bio, slur_regex)?;
check_is_apub_id_valid(&person.id, false, context.settings())?;
check_is_apub_id_valid(&person.id, false, &context.settings())?;
let person_form = PersonForm {
name,

View File

@ -191,7 +191,7 @@ impl FromApub for Post {
let thumbnail_url: Option<Url> = page.image.clone().map(|i| i.url);
let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
fetch_site_data(context.client(), context.settings(), Some(url)).await
fetch_site_data(context.client(), &context.settings(), Some(url)).await
} else {
(None, thumbnail_url)
};

View File

@ -59,10 +59,7 @@ async fn upload(
};
let mut client_req = client.request_from(
format!(
"{}/image",
pictrs_url(context.settings().to_owned().pictrs_url)?
),
format!("{}/image", pictrs_url(context.settings().pictrs_url)?),
req.head(),
);
// remove content-encoding header so that pictrs doesnt send gzipped response
@ -92,7 +89,7 @@ async fn full_res(
let name = &filename.into_inner();
// If there are no query params, the URL is original
let pictrs_url_settings = context.settings().to_owned().pictrs_url;
let pictrs_url_settings = context.settings().pictrs_url;
let url = if params.format.is_none() && params.thumbnail.is_none() {
format!(
"{}/image/original/{}",
@ -160,7 +157,7 @@ async fn delete(
let url = format!(
"{}/image/delete/{}/{}",
pictrs_url(context.settings().to_owned().pictrs_url)?,
pictrs_url(context.settings().pictrs_url)?,
&token,
&file
);

View File

@ -2,12 +2,17 @@ use crate::{location_info, settings::structs::Settings, LemmyError};
use anyhow::{anyhow, Context};
use deser_hjson::from_str;
use regex::{Regex, RegexBuilder};
use std::{env, fs, io::Error};
use std::{env, fs, io::Error, sync::RwLock};
pub mod structs;
static CONFIG_FILE: &str = "config/config.hjson";
lazy_static! {
static ref SETTINGS: RwLock<Settings> =
RwLock::new(Settings::init().expect("Failed to load settings file"));
}
impl Settings {
/// Reads config from configuration file.
///
@ -35,6 +40,11 @@ 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!(
@ -83,6 +93,15 @@ impl Settings {
pub fn save_config_file(data: &str) -> Result<String, LemmyError> {
fs::write(CONFIG_FILE, 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()?)
}

View File

@ -55,8 +55,9 @@ impl LemmyContext {
pub fn activity_queue(&self) -> &QueueHandle {
&self.activity_queue
}
pub fn settings(&self) -> &Settings {
&self.settings
pub fn settings(&self) -> Settings {
// TODO hacky solution to be able to hotload the settings.
Settings::get()
}
pub fn secret(&self) -> &Secret {
&self.secret