]> Untitled Git - lemmy.git/commitdiff
Renaming to slur_filter. Fixes #1773 (#1801)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 28 Oct 2021 20:47:25 +0000 (16:47 -0400)
committerGitHub <noreply@github.com>
Thu, 28 Oct 2021 20:47:25 +0000 (20:47 +0000)
* Renaming to slur_filter. Fixes #1773

* Fixing config

* Updating doku for slur filter.

* Updating doku for slur filter again.

* Remove comment.

config/config.hjson
config/defaults.hjson
crates/utils/src/settings/mod.rs
crates/utils/src/settings/structs.rs
crates/utils/src/utils.rs
docker/lemmy.hjson

index 7c73aaafa925ab3744a08d20b53bde13808b3621..8d2ae84c73f6cb27b1f55ae3e1a9e94295885726 100644 (file)
@@ -5,4 +5,8 @@
   federation: {
     enabled: true
   }
+  slur_filter:
+    '''
+    (fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|ni((g{2,}|q)+|[gq]{2,})[e3r]+(s|z)?|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)
+    '''
 }
index 6907c36ff70421536fdd19774f0336e395eb5238..ff6df36dacb972d4ff4ba4e3b55de557101ce9cb 100644 (file)
   tls_enabled: true
   # Address where pictrs is available (for image hosting)
   pictrs_url: "http:#localhost:8080"
-  # Regex for slurs which are prohibited. Example: `(\bThis\b)|(\bis\b)|(\bsample\b)`
-  additional_slurs: "string"
+  slur_filter: "(\bThis\b)|(\bis\b)|(\bsample\b)"
   # Maximum length of local community and user names
   actor_name_max_length: 20
 }
index 40dd91664e761a699e8978200d8318661a86f6bf..e7bd5eb0ed9984e5e36fe933aca42e033cdb0a42 100644 (file)
@@ -113,15 +113,12 @@ impl Settings {
     WEBFINGER_USER_REGEX.to_owned()
   }
 
-  pub fn slur_regex(&self) -> Regex {
-    let mut slurs = r"(fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|ni((g{2,}|q)+|[gq]{2,})[e3r]+(s|z)?|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)".to_string();
-    if let Some(additional_slurs) = &self.additional_slurs {
-      slurs.push('|');
-      slurs.push_str(additional_slurs);
-    };
-    RegexBuilder::new(&slurs)
-      .case_insensitive(true)
-      .build()
-      .expect("compile regex")
+  pub fn slur_regex(&self) -> Option<Regex> {
+    self.slur_filter.as_ref().map(|slurs| {
+      RegexBuilder::new(slurs)
+        .case_insensitive(true)
+        .build()
+        .expect("compile regex")
+    })
   }
 }
index 4ec17906ba6d348605b2d866e33fdd2bd24336e3..300030816757f5b75a6226dca3dc53b577703588 100644 (file)
@@ -40,9 +40,9 @@ pub struct Settings {
   #[default(None)]
   #[doku(example = "http://localhost:8080")]
   pub pictrs_url: Option<String>,
-  /// Regex for slurs which are prohibited. Example: `(\bThis\b)|(\bis\b)|(\bsample\b)`
   #[default(None)]
-  pub additional_slurs: Option<String>,
+  #[doku(example = "(\\bThis\\b)|(\\bis\\b)|(\\bsample\\b)")]
+  pub slur_filter: Option<String>,
   /// Maximum length of local community and user names
   #[default(20)]
   pub actor_name_max_length: usize,
index 96dc340e4e1d3be811d3a3f8b2ebffc4ed64cb5d..3039a1cd474a0e5e7ec0526fb53aed3e0e8514f6 100644 (file)
@@ -28,30 +28,44 @@ pub fn convert_datetime(datetime: NaiveDateTime) -> DateTime<FixedOffset> {
   DateTime::<FixedOffset>::from_utc(datetime, FixedOffset::east(0))
 }
 
-pub fn remove_slurs(test: &str, slur_regex: &Regex) -> String {
-  slur_regex.replace_all(test, "*removed*").to_string()
+pub fn remove_slurs(test: &str, slur_regex: &Option<Regex>) -> String {
+  if let Some(slur_regex) = slur_regex {
+    slur_regex.replace_all(test, "*removed*").to_string()
+  } else {
+    test.to_string()
+  }
 }
 
-pub(crate) fn slur_check<'a>(test: &'a str, slur_regex: &'a Regex) -> Result<(), Vec<&'a str>> {
-  let mut matches: Vec<&str> = slur_regex.find_iter(test).map(|mat| mat.as_str()).collect();
+pub(crate) fn slur_check<'a>(
+  test: &'a str,
+  slur_regex: &'a Option<Regex>,
+) -> Result<(), Vec<&'a str>> {
+  if let Some(slur_regex) = slur_regex {
+    let mut matches: Vec<&str> = slur_regex.find_iter(test).map(|mat| mat.as_str()).collect();
 
-  // Unique
-  matches.sort_unstable();
-  matches.dedup();
+    // Unique
+    matches.sort_unstable();
+    matches.dedup();
 
-  if matches.is_empty() {
-    Ok(())
+    if matches.is_empty() {
+      Ok(())
+    } else {
+      Err(matches)
+    }
   } else {
-    Err(matches)
+    Ok(())
   }
 }
 
-pub fn check_slurs(text: &str, slur_regex: &Regex) -> Result<(), ApiError> {
-  slur_check(text, slur_regex)
-    .map_err(|slurs| ApiError::err_plain(&slurs_vec_to_str(slurs.clone())))
+pub fn check_slurs(text: &str, slur_regex: &Option<Regex>) -> Result<(), ApiError> {
+  if let Err(slurs) = slur_check(text, slur_regex) {
+    Err(ApiError::err_plain(&slurs_vec_to_str(slurs)))
+  } else {
+    Ok(())
+  }
 }
 
-pub fn check_slurs_opt(text: &Option<String>, slur_regex: &Regex) -> Result<(), ApiError> {
+pub fn check_slurs_opt(text: &Option<String>, slur_regex: &Option<Regex>) -> Result<(), ApiError> {
   match text {
     Some(t) => check_slurs(t, slur_regex),
     None => Ok(()),
index 4c42d8aea3008a8ead39980e3a729d852dcc09d2..54975feca8337dec93fd6de8e9505c48ebc34176 100644 (file)
     # maximum number of active sql connections
     pool_size: 5
   }
+  slur_filter:
+    '''
+    (fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|ni((g{2,}|q)+|[gq]{2,})[e3r]+(s|z)?|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)
+    '''
 #  # optional: email sending configuration
 #  email: {
 #    # hostname and port of the smtp server