Initialize the regexes.

This commit is contained in:
Dessalines 2021-09-24 11:33:34 -04:00
parent d2ebc0fbc3
commit a47cdd554b
4 changed files with 30 additions and 11 deletions

View file

@ -19,7 +19,6 @@ use lemmy_utils::{
}; };
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;
use reqwest::Client;
use url::Url; use url::Url;
pub mod create_or_update; pub mod create_or_update;
@ -136,16 +135,18 @@ async fn get_comment_parent_creator(
/// using webfinger. /// using webfinger.
async fn fetch_webfinger_url( async fn fetch_webfinger_url(
mention: &MentionData, mention: &MentionData,
client: &Client, context: &LemmyContext,
protocol_string: &str,
) -> Result<Url, LemmyError> { ) -> Result<Url, LemmyError> {
let fetch_url = format!( let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}", "{}://{}/.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); 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 let res: WebfingerResponse = response
.json() .json()

View file

@ -69,7 +69,6 @@ impl RateLimited {
{ {
// Does not need to be blocking because the RwLock in settings never held across await points, // 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 // 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; let rate_limit = self.rate_limit_config;
// before // before

View file

@ -16,12 +16,22 @@ impl Settings {
/// Warning: Only call this once. /// Warning: Only call this once.
pub fn init() -> Result<Self, LemmyError> { pub fn init() -> Result<Self, LemmyError> {
// Read the config file // Read the config file
let config = from_str::<Settings>(&Self::read_config_file()?)?; let mut config = from_str::<Settings>(&Self::read_config_file()?)?;
if config.hostname == "unset" { if config.hostname == "unset" {
return Err(anyhow!("Hostname variable is not set!").into()); 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) Ok(config)
} }
@ -76,15 +86,17 @@ impl Settings {
Ok(Self::read_config_file()?) 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 { 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") .expect("compile webfinger regex")
} }
pub fn webfinger_username_regex(&self) -> 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") .expect("compile webfinger regex")
} }

View file

@ -1,3 +1,4 @@
use regex::Regex;
use serde::Deserialize; use serde::Deserialize;
use std::net::{IpAddr, Ipv4Addr}; use std::net::{IpAddr, Ipv4Addr};
@ -30,6 +31,12 @@ pub struct Settings {
pub additional_slurs: Option<String>, pub additional_slurs: Option<String>,
#[default(20)] #[default(20)]
pub actor_name_max_length: usize, pub actor_name_max_length: usize,
#[default(None)]
#[serde(skip)]
pub webfinger_community_regex: Option<Regex>,
#[default(None)]
#[serde(skip)]
pub webfinger_username_regex: Option<Regex>,
} }
#[derive(Debug, Deserialize, Clone, SmartDefault)] #[derive(Debug, Deserialize, Clone, SmartDefault)]