Initialize the regexes.

remove_settings_and_secret_singletons
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 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<Url, LemmyError> {
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()

View File

@ -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

View File

@ -16,12 +16,22 @@ impl Settings {
/// Warning: Only call this once.
pub fn init() -> Result<Self, LemmyError> {
// 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" {
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")
}

View File

@ -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<String>,
#[default(20)]
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)]