]> Untitled Git - lemmy.git/commitdiff
Cleaning optional post bodies. Fixes #2039 (#2043)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 20 Jan 2022 14:04:54 +0000 (09:04 -0500)
committerGitHub <noreply@github.com>
Thu, 20 Jan 2022 14:04:54 +0000 (14:04 +0000)
* Cleaning optional post bodies. Fixes #2039

* Only trim once.

* Using .map() instead.

crates/api_crud/src/post/create.rs
crates/api_crud/src/post/update.rs
crates/utils/src/utils.rs

index 276504be32f361f188c9a771dd96fb6947c5719b..81a12fc54403c91a1e15d574a409aeb0711d1f6d 100644 (file)
@@ -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,
index 97dab1cbefb3ed26029816f71dcb1c82c0fb352a..7a85f8638df159ce8a325c28f5b9722f758ef026 100644 (file)
@@ -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,
index 90f780e268a2ec5ca81143186d4a293121838928..2b28ae0eaa3dfe847574e8b0dc409a8097a9383e 100644 (file)
@@ -175,6 +175,10 @@ pub fn clean_url_params(mut url: Url) -> Url {
   url
 }
 
+pub fn clean_optional_text(text: &Option<String>) -> Option<String> {
+  text.as_ref().map(|t| t.trim().to_string())
+}
+
 #[cfg(test)]
 mod tests {
   use crate::utils::{clean_url_params, is_valid_post_title};