]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/update.rs
Swap out iframely (#1706)
[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 = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
27
28     check_slurs_opt(&data.name)?;
29     check_slurs_opt(&data.body)?;
30
31     if let Some(name) = &data.name {
32       if !is_valid_post_title(name) {
33         return Err(ApiError::err("invalid_post_title").into());
34       }
35     }
36
37     let post_id = data.post_id;
38     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
39
40     check_community_ban(
41       local_user_view.person.id,
42       orig_post.community_id,
43       context.pool(),
44     )
45     .await?;
46
47     // Verify that only the creator can edit
48     if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
49       return Err(ApiError::err("no_post_edit_allowed").into());
50     }
51
52     // Fetch post links and Pictrs cached image
53     let data_url = data.url.as_ref();
54     let (metadata_res, pictrs_thumbnail) = fetch_site_data(context.client(), data_url).await;
55     let (embed_title, embed_description, embed_html) = metadata_res
56       .map(|u| (u.title, u.description, u.html))
57       .unwrap_or((None, None, None));
58
59     let post_form = PostForm {
60       creator_id: orig_post.creator_id.to_owned(),
61       community_id: orig_post.community_id,
62       name: data.name.to_owned().unwrap_or(orig_post.name),
63       url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
64       body: data.body.to_owned(),
65       nsfw: data.nsfw,
66       updated: Some(naive_now()),
67       embed_title,
68       embed_description,
69       embed_html,
70       thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
71       ..PostForm::default()
72     };
73
74     let post_id = data.post_id;
75     let res = blocking(context.pool(), move |conn| {
76       Post::update(conn, post_id, &post_form)
77     })
78     .await?;
79     let updated_post: Post = match res {
80       Ok(post) => post,
81       Err(e) => {
82         let err_type = if e.to_string() == "value too long for type character varying(200)" {
83           "post_title_too_long"
84         } else {
85           "couldnt_update_post"
86         };
87
88         return Err(ApiError::err(err_type).into());
89       }
90     };
91
92     // Send apub update
93     CreateOrUpdatePost::send(
94       &updated_post,
95       &local_user_view.person,
96       CreateOrUpdateType::Update,
97       context,
98     )
99     .await?;
100
101     send_post_ws_message(
102       data.post_id,
103       UserOperationCrud::EditPost,
104       websocket_id,
105       Some(local_user_view.person.id),
106       context,
107     )
108     .await
109   }
110 }