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