]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/update.rs
Cleaning optional post bodies. Fixes #2039 (#2043)
[lemmy.git] / crates / api_crud / src / post / update.rs
1 use actix_web::web::Data;
2
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   check_community_deleted_or_removed,
7   get_local_user_view_from_jwt,
8   post::*,
9 };
10 use lemmy_apub::protocol::activities::{
11   create_or_update::post::CreateOrUpdatePost,
12   CreateOrUpdateType,
13 };
14 use lemmy_db_schema::{
15   naive_now,
16   source::post::{Post, PostForm},
17   traits::Crud,
18 };
19 use lemmy_utils::{
20   request::fetch_site_data,
21   utils::{check_slurs_opt, clean_optional_text, clean_url_params, is_valid_post_title},
22   ConnectionId,
23   LemmyError,
24 };
25 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
26
27 use crate::PerformCrud;
28
29 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for EditPost {
31   type Response = PostResponse;
32
33   #[tracing::instrument(skip(context, websocket_id))]
34   async fn perform(
35     &self,
36     context: &Data<LemmyContext>,
37     websocket_id: Option<ConnectionId>,
38   ) -> Result<PostResponse, LemmyError> {
39     let data: &EditPost = self;
40     let local_user_view =
41       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
42
43     let slur_regex = &context.settings().slur_regex();
44     check_slurs_opt(&data.name, slur_regex)?;
45     check_slurs_opt(&data.body, slur_regex)?;
46
47     if let Some(name) = &data.name {
48       if !is_valid_post_title(name) {
49         return Err(LemmyError::from_message("invalid_post_title"));
50       }
51     }
52
53     let post_id = data.post_id;
54     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
55
56     check_community_ban(
57       local_user_view.person.id,
58       orig_post.community_id,
59       context.pool(),
60     )
61     .await?;
62     check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
63
64     // Verify that only the creator can edit
65     if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
66       return Err(LemmyError::from_message("no_post_edit_allowed"));
67     }
68
69     // Fetch post links and Pictrs cached image
70     let data_url = data.url.as_ref();
71     let (metadata_res, pictrs_thumbnail) =
72       fetch_site_data(context.client(), &context.settings(), data_url).await;
73     let (embed_title, embed_description, embed_html) = metadata_res
74       .map(|u| (u.title, u.description, u.html))
75       .unwrap_or((None, None, None));
76
77     let post_form = PostForm {
78       creator_id: orig_post.creator_id.to_owned(),
79       community_id: orig_post.community_id,
80       name: data.name.to_owned().unwrap_or(orig_post.name),
81       url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
82       body: clean_optional_text(&data.body),
83       nsfw: data.nsfw,
84       updated: Some(naive_now()),
85       embed_title,
86       embed_description,
87       embed_html,
88       thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
89       ..PostForm::default()
90     };
91
92     let post_id = data.post_id;
93     let res = blocking(context.pool(), move |conn| {
94       Post::update(conn, post_id, &post_form)
95     })
96     .await?;
97     let updated_post: Post = match res {
98       Ok(post) => post,
99       Err(e) => {
100         let err_type = if e.to_string() == "value too long for type character varying(200)" {
101           "post_title_too_long"
102         } else {
103           "couldnt_update_post"
104         };
105
106         return Err(LemmyError::from(e).with_message(err_type));
107       }
108     };
109
110     // Send apub update
111     CreateOrUpdatePost::send(
112       updated_post.into(),
113       &local_user_view.person.clone().into(),
114       CreateOrUpdateType::Update,
115       context,
116     )
117     .await?;
118
119     send_post_ws_message(
120       data.post_id,
121       UserOperationCrud::EditPost,
122       websocket_id,
123       Some(local_user_view.person.id),
124       context,
125     )
126     .await
127   }
128 }