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