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