From a47cdd554b4ca0469e9a37506174b2eaee92fed0 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 24 Sep 2021 11:33:34 -0400 Subject: [PATCH] Initialize the regexes. --- crates/apub/src/activities/comment/mod.rs | 11 ++++++----- crates/utils/src/rate_limit/mod.rs | 1 - crates/utils/src/settings/mod.rs | 22 +++++++++++++++++----- crates/utils/src/settings/structs.rs | 7 +++++++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/apub/src/activities/comment/mod.rs b/crates/apub/src/activities/comment/mod.rs index 7d5259fdf..1810a38ac 100644 --- a/crates/apub/src/activities/comment/mod.rs +++ b/crates/apub/src/activities/comment/mod.rs @@ -19,7 +19,6 @@ use lemmy_utils::{ }; use lemmy_websocket::LemmyContext; use log::debug; -use reqwest::Client; use url::Url; pub mod create_or_update; @@ -136,16 +135,18 @@ async fn get_comment_parent_creator( /// using webfinger. async fn fetch_webfinger_url( mention: &MentionData, - client: &Client, - protocol_string: &str, + context: &LemmyContext, ) -> Result { let fetch_url = format!( "{}://{}/.well-known/webfinger?resource=acct:{}@{}", - protocol_string, mention.domain, mention.name, mention.domain + context.settings().get_protocol_string(), + mention.domain, + mention.name, + mention.domain ); debug!("Fetching webfinger url: {}", &fetch_url); - let response = retry(|| client.get(&fetch_url).send()).await?; + let response = retry(|| context.client().get(&fetch_url).send()).await?; let res: WebfingerResponse = response .json() diff --git a/crates/utils/src/rate_limit/mod.rs b/crates/utils/src/rate_limit/mod.rs index 209e7acb5..c1a4627c1 100644 --- a/crates/utils/src/rate_limit/mod.rs +++ b/crates/utils/src/rate_limit/mod.rs @@ -69,7 +69,6 @@ impl RateLimited { { // Does not need to be blocking because the RwLock in settings never held across await points, // and the operation here locks only long enough to clone - // let rate_limit: RateLimitConfig = Settings::get().rate_limit.unwrap_or_default(); let rate_limit = self.rate_limit_config; // before diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index 8808f9126..715c9128e 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -16,12 +16,22 @@ impl Settings { /// Warning: Only call this once. pub fn init() -> Result { // Read the config file - let config = from_str::(&Self::read_config_file()?)?; + let mut config = from_str::(&Self::read_config_file()?)?; if config.hostname == "unset" { return Err(anyhow!("Hostname variable is not set!").into()); } + // Initialize the regexes + config.webfinger_username_regex = Some( + Regex::new(&format!("^group:([a-z0-9_]{{3,}})@{}$", config.hostname)) + .expect("compile webfinger regex"), + ); + config.webfinger_username_regex = Some( + Regex::new(&format!("^acct:([a-z0-9_]{{3,}})@{}$", config.hostname)) + .expect("compile webfinger regex"), + ); + Ok(config) } @@ -76,15 +86,17 @@ impl Settings { Ok(Self::read_config_file()?) } - // TODO for these regexes: we can't use lazy_static here anymore, since the settings must be - // initialized first. pub fn webfinger_community_regex(&self) -> Regex { - Regex::new(&format!("^group:([a-z0-9_]{{3,}})@{}$", self.hostname)) + self + .webfinger_community_regex + .to_owned() .expect("compile webfinger regex") } pub fn webfinger_username_regex(&self) -> Regex { - Regex::new(&format!("^acct:([a-z0-9_]{{3,}})@{}$", self.hostname)) + self + .webfinger_username_regex + .to_owned() .expect("compile webfinger regex") } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 9ba8a5f94..293349348 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -1,3 +1,4 @@ +use regex::Regex; use serde::Deserialize; use std::net::{IpAddr, Ipv4Addr}; @@ -30,6 +31,12 @@ pub struct Settings { pub additional_slurs: Option, #[default(20)] pub actor_name_max_length: usize, + #[default(None)] + #[serde(skip)] + pub webfinger_community_regex: Option, + #[default(None)] + #[serde(skip)] + pub webfinger_username_regex: Option, } #[derive(Debug, Deserialize, Clone, SmartDefault)]