From 22ce4990969b8d52283b2b6eb96c89ec5a0e9d93 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 22 Jul 2021 22:04:45 +0200 Subject: [PATCH] add helper method for setting actor_name_max_length --- crates/utils/src/settings/structs.rs | 8 ++++++++ crates/utils/src/utils.rs | 11 +++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 1046bd147..ede1ce88c 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -21,6 +21,14 @@ pub struct Settings { pub(crate) actor_name_max_length: Option, } +impl Settings { + pub(crate) fn actor_name_max_length(&self) -> usize { + self + .actor_name_max_length + .unwrap_or_else(|| Settings::default().actor_name_max_length.unwrap()) + } +} + #[derive(Debug, Deserialize, Clone)] pub struct CaptchaConfig { pub enabled: bool, diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index d063359ba..d6e1f25d4 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -116,21 +116,16 @@ pub fn scrape_text_for_mentions(text: &str) -> Vec { } pub fn is_valid_actor_name(name: &str) -> bool { - let max_length = Settings::get() - .actor_name_max_length - .unwrap_or_else(|| Settings::default().actor_name_max_length.unwrap()); - name.chars().count() <= max_length && VALID_ACTOR_NAME_REGEX.is_match(name) + name.chars().count() <= Settings::get().actor_name_max_length() + && VALID_ACTOR_NAME_REGEX.is_match(name) } // Can't do a regex here, reverse lookarounds not supported pub fn is_valid_display_name(name: &str) -> bool { - let max_length = Settings::get() - .actor_name_max_length - .unwrap_or_else(|| Settings::default().actor_name_max_length.unwrap()); !name.starts_with('@') && !name.starts_with('\u{200b}') && name.chars().count() >= 3 - && name.chars().count() <= max_length + && name.chars().count() <= Settings::get().actor_name_max_length() } pub fn is_valid_matrix_id(matrix_id: &str) -> bool {