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