Move slur check into helper functions

This commit is contained in:
Felix Ableitner 2020-08-03 15:15:44 +02:00
parent c75ee7bbfd
commit 7d1dd78f86
5 changed files with 52 additions and 75 deletions

View file

@ -16,8 +16,6 @@ use lemmy_utils::{
is_valid_community_name, is_valid_community_name,
make_apub_endpoint, make_apub_endpoint,
naive_from_unix, naive_from_unix,
slur_check,
slurs_vec_to_str,
EndpointType, EndpointType,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -227,19 +225,9 @@ impl Perform for Oper<CreateCommunity> {
let data: &CreateCommunity = &self.data; let data: &CreateCommunity = &self.data;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Err(slurs) = slur_check(&data.name) { check_slurs(&data.name)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); check_slurs(&data.title)?;
} check_slurs_opt(&data.description)?;
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());
}
}
if !is_valid_community_name(&data.name) { if !is_valid_community_name(&data.name) {
return Err(APIError::err("invalid_community_name").into()); return Err(APIError::err("invalid_community_name").into());
@ -327,15 +315,8 @@ impl Perform for Oper<EditCommunity> {
let data: &EditCommunity = &self.data; let data: &EditCommunity = &self.data;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Err(slurs) = slur_check(&data.title) { check_slurs(&data.title)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); check_slurs_opt(&data.description)?;
}
if let Some(description) = &data.description {
if let Err(slurs) = slur_check(description) {
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
}
}
// Verify its a mod (only mods can edit it) // Verify its a mod (only mods can edit it)
let edit_id = data.edit_id; let edit_id = data.edit_id;

View file

@ -9,6 +9,7 @@ use lemmy_db::{
user_view::*, user_view::*,
Crud, Crud,
}; };
use lemmy_utils::{slur_check, slurs_vec_to_str};
use thiserror::Error; use thiserror::Error;
pub mod claims; pub mod claims;
@ -102,3 +103,17 @@ pub(in crate::api) async fn get_user_from_jwt_opt(
None => Ok(None), 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<String>) -> Result<(), APIError> {
match text {
Some(t) => check_slurs(t),
None => Ok(()),
}
}

View file

@ -1,5 +1,14 @@
use crate::{ 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}, apub::{ApubLikeableType, ApubObjectType},
blocking, blocking,
fetch_iframely_and_pictrs_data, fetch_iframely_and_pictrs_data,
@ -25,13 +34,7 @@ use lemmy_db::{
Saveable, Saveable,
SortType, SortType,
}; };
use lemmy_utils::{ use lemmy_utils::{is_valid_post_title, make_apub_endpoint, EndpointType};
is_valid_post_title,
make_apub_endpoint,
slur_check,
slurs_vec_to_str,
EndpointType,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
use url::Url; use url::Url;
@ -147,15 +150,8 @@ impl Perform for Oper<CreatePost> {
let data: &CreatePost = &self.data; let data: &CreatePost = &self.data;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Err(slurs) = slur_check(&data.name) { check_slurs(&data.name)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); check_slurs_opt(&data.body)?;
}
if let Some(body) = &data.body {
if let Err(slurs) = slur_check(body) {
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
}
}
if !is_valid_post_title(&data.name) { if !is_valid_post_title(&data.name) {
return Err(APIError::err("invalid_post_title").into()); return Err(APIError::err("invalid_post_title").into());
@ -499,15 +495,8 @@ impl Perform for Oper<EditPost> {
let data: &EditPost = &self.data; let data: &EditPost = &self.data;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Err(slurs) = slur_check(&data.name) { check_slurs(&data.name)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); check_slurs_opt(&data.body)?;
}
if let Some(body) = &data.body {
if let Err(slurs) = slur_check(body) {
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
}
}
if !is_valid_post_title(&data.name) { if !is_valid_post_title(&data.name) {
return Err(APIError::err("invalid_post_title").into()); return Err(APIError::err("invalid_post_title").into());

View file

@ -1,6 +1,15 @@
use super::user::Register; use super::user::Register;
use crate::{ 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, apub::fetcher::search_by_apub_id,
blocking, blocking,
version, version,
@ -24,7 +33,7 @@ use lemmy_db::{
SearchType, SearchType,
SortType, SortType,
}; };
use lemmy_utils::{settings::Settings, slur_check, slurs_vec_to_str}; use lemmy_utils::settings::Settings;
use log::{debug, info}; use log::{debug, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
@ -245,15 +254,8 @@ impl Perform for Oper<CreateSite> {
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Err(slurs) = slur_check(&data.name) { check_slurs(&data.name)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); check_slurs_opt(&data.description)?;
}
if let Some(description) = &data.description {
if let Err(slurs) = slur_check(description) {
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
}
}
// Make sure user is an admin // Make sure user is an admin
is_admin(pool, user.id).await?; is_admin(pool, user.id).await?;
@ -290,15 +292,8 @@ impl Perform for Oper<EditSite> {
let data: &EditSite = &self.data; let data: &EditSite = &self.data;
let user = get_user_from_jwt(&data.auth, pool).await?; let user = get_user_from_jwt(&data.auth, pool).await?;
if let Err(slurs) = slur_check(&data.name) { check_slurs(&data.name)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into()); check_slurs_opt(&data.description)?;
}
if let Some(description) = &data.description {
if let Err(slurs) = slur_check(description) {
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
}
}
// Make sure user is an admin // Make sure user is an admin
is_admin(pool, user.id).await?; is_admin(pool, user.id).await?;

View file

@ -1,5 +1,6 @@
use crate::{ use crate::{
api::{ api::{
check_slurs,
claims::Claims, claims::Claims,
get_user_from_jwt, get_user_from_jwt,
get_user_from_jwt_opt, get_user_from_jwt_opt,
@ -55,8 +56,6 @@ use lemmy_utils::{
remove_slurs, remove_slurs,
send_email, send_email,
settings::Settings, settings::Settings,
slur_check,
slurs_vec_to_str,
EndpointType, EndpointType,
}; };
use log::error; use log::error;
@ -374,9 +373,7 @@ impl Perform for Oper<Register> {
}; };
} }
if let Err(slurs) = slur_check(&data.username) { check_slurs(&data.username)?;
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
}
// Make sure there are no admins // Make sure there are no admins
let any_admins = blocking(pool, move |conn| { let any_admins = blocking(pool, move |conn| {