Improve readability

pull/3009/head
dullbananas 2023-06-13 05:12:48 +00:00
parent 6b4c3ec5f6
commit 1cd4003bdc
16 changed files with 27 additions and 25 deletions

View File

@ -42,7 +42,7 @@ impl Perform for BanFromCommunity {
// Verify that only mods or admins can ban
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
is_valid_body_field(data.reason.as_deref())?;
is_valid_body_field(&data.reason)?;
let community_user_ban_form = CommunityPersonBanForm {
community_id: data.community_id,

View File

@ -30,7 +30,7 @@ impl Perform for BanPerson {
// Make sure user is an admin
is_admin(&local_user_view)?;
is_valid_body_field(data.reason.as_deref())?;
is_valid_body_field(&data.reason)?;
let ban = data.ban;
let banned_person_id = data.person_id;

View File

@ -46,7 +46,7 @@ impl PerformCrud for CreateComment {
let local_site = LocalSite::read(context.pool()).await?;
let content_slurs_removed = remove_slurs(&data.content, &local_site_to_slur_regex(&local_site));
is_valid_body_field(Some(&content_slurs_removed))?;
is_valid_body_field(&Some(&*content_slurs_removed))?;
let mentions = scrape_text_for_mentions(&content_slurs_removed);

View File

@ -24,6 +24,7 @@ use lemmy_utils::{
validation::is_valid_body_field,
},
};
use std::borrow::Cow;
#[async_trait::async_trait(?Send)]
impl PerformCrud for EditComment {
@ -64,11 +65,11 @@ impl PerformCrud for EditComment {
.as_ref()
.map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
is_valid_body_field(content_slurs_removed.as_deref())?;
is_valid_body_field(&content_slurs_removed)?;
let comment_id = data.comment_id;
let form = CommentUpdateForm::builder()
.content(content_slurs_removed.map(std::borrow::Cow::into_owned))
.content(content_slurs_removed.map(Cow::into_owned))
.language_id(data.language_id)
.updated(Some(Some(naive_now())))
.build();

View File

@ -67,7 +67,7 @@ impl PerformCrud for CreateCommunity {
check_slurs_opt(&data.description, &slur_regex)?;
is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
is_valid_body_field(data.description.as_deref())?;
is_valid_body_field(&data.description)?;
// Double check for duplicate community actor_ids
let community_actor_id = generate_local_apub_endpoint(

View File

@ -38,7 +38,7 @@ impl PerformCrud for EditCommunity {
let slur_regex = local_site_to_slur_regex(&local_site);
check_slurs_opt(&data.title, &slur_regex)?;
check_slurs_opt(&data.description, &slur_regex)?;
is_valid_body_field(data.description.as_deref())?;
is_valid_body_field(&data.description)?;
// Verify its a mod (only mods can edit it)
let community_id = data.community_id;

View File

@ -57,7 +57,7 @@ impl PerformCrud for CreatePost {
let url = data_url.map(clean_url_params).map(Into::into); // TODO no good way to handle a "clear"
is_valid_post_title(&data.name)?;
is_valid_body_field(data.body.as_deref())?;
is_valid_body_field(&data.body)?;
check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
check_community_deleted_or_removed(data.community_id, context.pool()).await?;

View File

@ -49,7 +49,7 @@ impl PerformCrud for EditPost {
is_valid_post_title(name)?;
}
is_valid_body_field(data.body.as_deref())?;
is_valid_body_field(&data.body)?;
let post_id = data.post_id;
let orig_post = Post::read(context.pool(), post_id).await?;

View File

@ -40,7 +40,7 @@ impl PerformCrud for CreatePrivateMessage {
let local_site = LocalSite::read(context.pool()).await?;
let content_slurs_removed = remove_slurs(&data.content, &local_site_to_slur_regex(&local_site));
is_valid_body_field(Some(&content_slurs_removed))?;
is_valid_body_field(&Some(&*content_slurs_removed))?;
check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;

View File

@ -41,7 +41,7 @@ impl PerformCrud for EditPrivateMessage {
// Doing the update
let content_slurs_removed = remove_slurs(&data.content, &local_site_to_slur_regex(&local_site));
is_valid_body_field(Some(&content_slurs_removed))?;
is_valid_body_field(&Some(&*content_slurs_removed))?;
let private_message_id = data.private_message_id;
PrivateMessage::update(

View File

@ -66,7 +66,7 @@ impl PerformCrud for CreateSite {
site_description_length_check(desc)?;
}
is_valid_body_field(data.sidebar.as_deref())?;
is_valid_body_field(&data.sidebar)?;
let application_question = diesel_option_overwrite(&data.application_question);
check_application_question(

View File

@ -57,7 +57,7 @@ impl PerformCrud for EditSite {
site_description_length_check(desc)?;
}
is_valid_body_field(data.sidebar.as_deref())?;
is_valid_body_field(&data.sidebar)?;
let application_question = diesel_option_overwrite(&data.application_question);
check_application_question(

View File

@ -41,10 +41,12 @@ impl Collection for ApubCommunityOutbox {
owner: &Self::Owner,
data: &Data<Self::DataType>,
) -> Result<Self::Kind, LemmyError> {
let post_list: Vec<Post> = Post::list_for_community(data.pool(), owner.id).await?;
let post_list = Post::list_for_community(data.pool(), owner.id)
.await?
.into_iter()
.map(ApubPost::from);
let mut ordered_items = Vec::with_capacity(post_list.len());
for post in post_list {
let post = ApubPost::from(post);
let person = Person::read(data.pool(), post.creator_id).await?.into();
let create =
CreateOrUpdatePage::new(post, &person, owner, CreateOrUpdateType::Create, data).await?;

View File

@ -132,14 +132,11 @@ pub(crate) fn check_apub_id_valid_with_strictness(
if is_strict && !local_site_data.allowed_instances.is_empty() {
// need to allow this explicitly because apub receive might contain objects from our local
// instance.
let allowed = local_site_data.allowed_instances.iter().map(|i| &i.domain);
let local_instance = settings
.get_hostname_without_port()
.expect("local hostname is valid");
let mut allowed_and_local = local_site_data
.allowed_instances
.iter()
.map(|i| &i.domain)
.chain([&local_instance]);
let mut allowed_and_local = allowed.chain([&local_instance]);
let domain = apub_id.domain().expect("apud id has domain").to_string();
if !allowed_and_local.any(|i| i == &domain) {

View File

@ -211,14 +211,15 @@ impl Object for ApubPost {
let local_site = LocalSite::read(context.pool()).await.ok();
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
let body = read_from_string_or_source_opt(&page.content, &page.media_type, &page.source);
let body_slurs_removed = body.as_deref().map(|s| remove_slurs(s, slur_regex));
let body_slurs_removed =
read_from_string_or_source_opt(&page.content, &page.media_type, &page.source)
.map(|s| remove_slurs(&s, slur_regex).into_owned());
let language_id = LanguageTag::to_language_id_single(page.language, context.pool()).await?;
PostInsertForm {
name,
url: url.map(Into::into),
body: body_slurs_removed.map(std::borrow::Cow::into_owned),
body: body_slurs_removed,
creator_id: creator.id,
community_id: community.id,
removed: None,

View File

@ -2,6 +2,7 @@ use crate::error::{LemmyError, LemmyResult};
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use std::borrow::Borrow;
use totp_rs::{Secret, TOTP};
use url::Url;
@ -68,8 +69,8 @@ pub fn is_valid_post_title(title: &str) -> LemmyResult<()> {
}
/// This could be post bodies, comments, or any description field
pub fn is_valid_body_field(body: Option<&str>) -> LemmyResult<()> {
if let Some(body) = body {
pub fn is_valid_body_field<S: Borrow<str>>(body: &Option<S>) -> LemmyResult<()> {
if let Some(body) = body.as_ref().map(Borrow::borrow) {
let check = body.chars().count() <= BODY_MAX_LENGTH;
if !check {
Err(LemmyError::from_message("invalid_body_field"))