diff --git a/server/src/api/community.rs b/server/src/api/community.rs index beb51f1b5..904dfe53c 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -16,8 +16,6 @@ use lemmy_utils::{ is_valid_community_name, make_apub_endpoint, naive_from_unix, - slur_check, - slurs_vec_to_str, EndpointType, }; use serde::{Deserialize, Serialize}; @@ -227,19 +225,9 @@ impl Perform for Oper { let data: &CreateCommunity = &self.data; let user = get_user_from_jwt(&data.auth, pool).await?; - if let Err(slurs) = slur_check(&data.name) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Err(slurs) = slur_check(&data.title) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Some(description) = &data.description { - if let Err(slurs) = slur_check(description) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - } + check_slurs(&data.name)?; + check_slurs(&data.title)?; + check_slurs_opt(&data.description)?; if !is_valid_community_name(&data.name) { return Err(APIError::err("invalid_community_name").into()); @@ -327,15 +315,8 @@ impl Perform for Oper { let data: &EditCommunity = &self.data; let user = get_user_from_jwt(&data.auth, pool).await?; - if let Err(slurs) = slur_check(&data.title) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Some(description) = &data.description { - if let Err(slurs) = slur_check(description) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - } + check_slurs(&data.title)?; + check_slurs_opt(&data.description)?; // Verify its a mod (only mods can edit it) let edit_id = data.edit_id; diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index f97a50c25..6e4e67f27 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -9,6 +9,7 @@ use lemmy_db::{ user_view::*, Crud, }; +use lemmy_utils::{slur_check, slurs_vec_to_str}; use thiserror::Error; pub mod claims; @@ -102,3 +103,17 @@ pub(in crate::api) async fn get_user_from_jwt_opt( None => Ok(None), } } + +pub(in crate::api) fn check_slurs(text: &str) -> Result<(), APIError> { + if let Err(slurs) = slur_check(text) { + Err(APIError::err(&slurs_vec_to_str(slurs))) + } else { + Ok(()) + } +} +pub(in crate::api) fn check_slurs_opt(text: &Option) -> Result<(), APIError> { + match text { + Some(t) => check_slurs(t), + None => Ok(()), + } +} diff --git a/server/src/api/post.rs b/server/src/api/post.rs index a9229fce9..146b1536a 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -1,5 +1,14 @@ use crate::{ - api::{get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, APIError, Oper, Perform}, + api::{ + check_slurs, + check_slurs_opt, + get_user_from_jwt, + get_user_from_jwt_opt, + is_mod_or_admin, + APIError, + Oper, + Perform, + }, apub::{ApubLikeableType, ApubObjectType}, blocking, fetch_iframely_and_pictrs_data, @@ -25,13 +34,7 @@ use lemmy_db::{ Saveable, SortType, }; -use lemmy_utils::{ - is_valid_post_title, - make_apub_endpoint, - slur_check, - slurs_vec_to_str, - EndpointType, -}; +use lemmy_utils::{is_valid_post_title, make_apub_endpoint, EndpointType}; use serde::{Deserialize, Serialize}; use std::str::FromStr; use url::Url; @@ -147,15 +150,8 @@ impl Perform for Oper { let data: &CreatePost = &self.data; let user = get_user_from_jwt(&data.auth, pool).await?; - if let Err(slurs) = slur_check(&data.name) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Some(body) = &data.body { - if let Err(slurs) = slur_check(body) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - } + check_slurs(&data.name)?; + check_slurs_opt(&data.body)?; if !is_valid_post_title(&data.name) { return Err(APIError::err("invalid_post_title").into()); @@ -499,15 +495,8 @@ impl Perform for Oper { let data: &EditPost = &self.data; let user = get_user_from_jwt(&data.auth, pool).await?; - if let Err(slurs) = slur_check(&data.name) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Some(body) = &data.body { - if let Err(slurs) = slur_check(body) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - } + check_slurs(&data.name)?; + check_slurs_opt(&data.body)?; if !is_valid_post_title(&data.name) { return Err(APIError::err("invalid_post_title").into()); diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 7dfdb9ddf..82cad9610 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -1,6 +1,15 @@ use super::user::Register; use crate::{ - api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, APIError, Oper, Perform}, + api::{ + check_slurs, + check_slurs_opt, + get_user_from_jwt, + get_user_from_jwt_opt, + is_admin, + APIError, + Oper, + Perform, + }, apub::fetcher::search_by_apub_id, blocking, version, @@ -24,7 +33,7 @@ use lemmy_db::{ SearchType, SortType, }; -use lemmy_utils::{settings::Settings, slur_check, slurs_vec_to_str}; +use lemmy_utils::settings::Settings; use log::{debug, info}; use serde::{Deserialize, Serialize}; use std::str::FromStr; @@ -245,15 +254,8 @@ impl Perform for Oper { let user = get_user_from_jwt(&data.auth, pool).await?; - if let Err(slurs) = slur_check(&data.name) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Some(description) = &data.description { - if let Err(slurs) = slur_check(description) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - } + check_slurs(&data.name)?; + check_slurs_opt(&data.description)?; // Make sure user is an admin is_admin(pool, user.id).await?; @@ -290,15 +292,8 @@ impl Perform for Oper { let data: &EditSite = &self.data; let user = get_user_from_jwt(&data.auth, pool).await?; - if let Err(slurs) = slur_check(&data.name) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - - if let Some(description) = &data.description { - if let Err(slurs) = slur_check(description) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } - } + check_slurs(&data.name)?; + check_slurs_opt(&data.description)?; // Make sure user is an admin is_admin(pool, user.id).await?; diff --git a/server/src/api/user.rs b/server/src/api/user.rs index e6cc6a37b..d6746c1a8 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -1,5 +1,6 @@ use crate::{ api::{ + check_slurs, claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, @@ -55,8 +56,6 @@ use lemmy_utils::{ remove_slurs, send_email, settings::Settings, - slur_check, - slurs_vec_to_str, EndpointType, }; use log::error; @@ -374,9 +373,7 @@ impl Perform for Oper { }; } - if let Err(slurs) = slur_check(&data.username) { - return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); - } + check_slurs(&data.username)?; // Make sure there are no admins let any_admins = blocking(pool, move |conn| {