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