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