From 1cd4003bdc84e429761afe9d4fb71f7a19b2ce58 Mon Sep 17 00:00:00 2001 From: dullbananas Date: Tue, 13 Jun 2023 05:12:48 +0000 Subject: [PATCH] Improve readability --- crates/api/src/community/ban.rs | 2 +- crates/api/src/local_user/ban_person.rs | 2 +- crates/api_crud/src/comment/create.rs | 2 +- crates/api_crud/src/comment/update.rs | 5 +++-- crates/api_crud/src/community/create.rs | 2 +- crates/api_crud/src/community/update.rs | 2 +- crates/api_crud/src/post/create.rs | 2 +- crates/api_crud/src/post/update.rs | 2 +- crates/api_crud/src/private_message/create.rs | 2 +- crates/api_crud/src/private_message/update.rs | 2 +- crates/api_crud/src/site/create.rs | 2 +- crates/api_crud/src/site/update.rs | 2 +- crates/apub/src/collections/community_outbox.rs | 6 ++++-- crates/apub/src/lib.rs | 7 ++----- crates/apub/src/objects/post.rs | 7 ++++--- crates/utils/src/utils/validation.rs | 5 +++-- 16 files changed, 27 insertions(+), 25 deletions(-) diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index c5d8999ec..a0fd7bf18 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -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, diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index f7dc4525e..452557d2c 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -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; diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index e0e942c3e..82b5e9af8 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -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); diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index 027326d8b..f3ca7792a 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -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(); diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index ee05f6163..850e9f2f5 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -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( diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index fed1393c2..f4e0c8c94 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -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; diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index abc4b7305..cd2cf1c3d 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -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?; diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index f340a5025..af2c63c50 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -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?; diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index b5557ff2e..63f737bf6 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -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?; diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index d997f506b..72434ef73 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -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( diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 09eb457ba..af8540669 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -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( diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 82407939d..b3e865759 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -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( diff --git a/crates/apub/src/collections/community_outbox.rs b/crates/apub/src/collections/community_outbox.rs index fc6309ec2..00cf527cc 100644 --- a/crates/apub/src/collections/community_outbox.rs +++ b/crates/apub/src/collections/community_outbox.rs @@ -41,10 +41,12 @@ impl Collection for ApubCommunityOutbox { owner: &Self::Owner, data: &Data, ) -> Result { - let post_list: Vec = 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?; diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index c8e3f1c87..13a5e7038 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -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) { diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 42dbafff1..09c378068 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -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, diff --git a/crates/utils/src/utils/validation.rs b/crates/utils/src/utils/validation.rs index 9860b0b31..e640a4802 100644 --- a/crates/utils/src/utils/validation.rs +++ b/crates/utils/src/utils/validation.rs @@ -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>(body: &Option) -> 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"))