]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/update.rs
Split api crate into api_structs and api
[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, UserOperation};
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       name: data.name.trim().to_owned(),
58       url: data_url.map(|u| u.to_owned().into()),
59       body: data.body.to_owned(),
60       nsfw: data.nsfw,
61       creator_id: orig_post.creator_id.to_owned(),
62       community_id: orig_post.community_id,
63       removed: Some(orig_post.removed),
64       deleted: Some(orig_post.deleted),
65       locked: Some(orig_post.locked),
66       stickied: Some(orig_post.stickied),
67       updated: Some(naive_now()),
68       embed_title: iframely_title,
69       embed_description: iframely_description,
70       embed_html: iframely_html,
71       thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
72       ap_id: Some(orig_post.ap_id),
73       local: orig_post.local,
74       published: None,
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     updated_post
97       .send_update(&local_user_view.person, context)
98       .await?;
99
100     let post_id = data.post_id;
101     let post_view = blocking(context.pool(), move |conn| {
102       PostView::read(conn, post_id, Some(local_user_view.person.id))
103     })
104     .await??;
105
106     let res = PostResponse { post_view };
107
108     context.chat_server().do_send(SendPost {
109       op: UserOperation::EditPost,
110       post: res.clone(),
111       websocket_id,
112     });
113
114     Ok(res)
115   }
116 }