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