* Cleaning optional post bodies. Fixes #2039
* Only trim once.
* Using .map() instead.
};
use lemmy_utils::{
request::fetch_site_data,
};
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,
};
ConnectionId,
LemmyError,
};
let post_form = PostForm {
name: data.name.trim().to_owned(),
url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
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,
community_id: data.community_id,
creator_id: local_user_view.person.id,
nsfw: data.nsfw,
};
use lemmy_utils::{
request::fetch_site_data,
};
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,
};
ConnectionId,
LemmyError,
};
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()),
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,
nsfw: data.nsfw,
updated: Some(naive_now()),
embed_title,
+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};
#[cfg(test)]
mod tests {
use crate::utils::{clean_url_params, is_valid_post_title};