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