From 194269fd3ca952100b92308b1468e032c8e557dc Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 17 Jan 2022 11:00:50 -0500 Subject: [PATCH] Cleaning optional post bodies. Fixes #2039 --- crates/api_crud/src/post/create.rs | 10 ++++++++-- crates/api_crud/src/post/update.rs | 4 ++-- crates/apub_lib/src/object_id.rs | 2 +- crates/routes/src/feeds.rs | 6 +----- crates/utils/src/utils.rs | 12 ++++++++++++ 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 276504be3..81a12fc54 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -26,7 +26,13 @@ use lemmy_db_schema::{ }; use lemmy_utils::{ request::fetch_site_data, - utils::{check_slurs, check_slurs_opt, clean_url_params, is_valid_post_title}, + utils::{ + check_slurs, + check_slurs_opt, + clean_optional_text, + clean_url_params, + is_valid_post_title, + }, ConnectionId, LemmyError, }; @@ -72,7 +78,7 @@ impl PerformCrud for CreatePost { let post_form = PostForm { name: data.name.trim().to_owned(), url: data_url.map(|u| clean_url_params(u.to_owned()).into()), - body: data.body.to_owned(), + body: clean_optional_text(&data.body), community_id: data.community_id, creator_id: local_user_view.person.id, nsfw: data.nsfw, diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 97dab1cbe..7a85f8638 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -18,7 +18,7 @@ use lemmy_db_schema::{ }; use lemmy_utils::{ request::fetch_site_data, - utils::{check_slurs_opt, clean_url_params, is_valid_post_title}, + utils::{check_slurs_opt, clean_optional_text, clean_url_params, is_valid_post_title}, ConnectionId, LemmyError, }; @@ -79,7 +79,7 @@ impl PerformCrud for EditPost { community_id: orig_post.community_id, name: data.name.to_owned().unwrap_or(orig_post.name), url: data_url.map(|u| clean_url_params(u.to_owned()).into()), - body: data.body.to_owned(), + body: clean_optional_text(&data.body), nsfw: data.nsfw, updated: Some(naive_now()), embed_title, diff --git a/crates/apub_lib/src/object_id.rs b/crates/apub_lib/src/object_id.rs index b2be92e5a..41b18ef93 100644 --- a/crates/apub_lib/src/object_id.rs +++ b/crates/apub_lib/src/object_id.rs @@ -161,7 +161,7 @@ where #[allow(clippy::to_string_in_display)] fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { // Use to_string here because Url.display is not useful for us - write!(f, "{}", self.0.to_string()) + write!(f, "{}", self.0) } } diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index eaa1041fe..7b668d7eb 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -97,11 +97,7 @@ async fn get_feed_data( let mut channel_builder = ChannelBuilder::default(); channel_builder .namespaces(RSS_NAMESPACE.to_owned()) - .title(&format!( - "{} - {}", - site_view.site.name, - listing_type.to_string() - )) + .title(&format!("{} - {}", site_view.site.name, listing_type)) .link(context.settings().get_protocol_and_hostname()) .items(items); diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index 90f780e26..d80771486 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -175,6 +175,18 @@ pub fn clean_url_params(mut url: Url) -> Url { url } +pub fn clean_optional_text(text: &Option) -> Option { + if let Some(text) = text { + if text.trim().is_empty() { + None + } else { + Some(text.trim().to_owned()) + } + } else { + None + } +} + #[cfg(test)] mod tests { use crate::utils::{clean_url_params, is_valid_post_title};