1 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 get_local_user_view_from_jwt,
10 use lemmy_apub::protocol::activities::{
11 create_or_update::post::CreateOrUpdatePost,
14 use lemmy_db_schema::{
16 source::post::{Post, PostForm},
20 request::fetch_site_data,
21 utils::{check_slurs_opt, clean_optional_text, clean_url_params, is_valid_post_title},
25 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
27 use crate::PerformCrud;
29 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for EditPost {
31 type Response = PostResponse;
33 #[tracing::instrument(skip(context, websocket_id))]
36 context: &Data<LemmyContext>,
37 websocket_id: Option<ConnectionId>,
38 ) -> Result<PostResponse, LemmyError> {
39 let data: &EditPost = self;
41 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
43 let slur_regex = &context.settings().slur_regex();
44 check_slurs_opt(&data.name, slur_regex)?;
45 check_slurs_opt(&data.body, slur_regex)?;
47 if let Some(name) = &data.name {
48 if !is_valid_post_title(name) {
49 return Err(LemmyError::from_message("invalid_post_title"));
53 let post_id = data.post_id;
54 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
57 local_user_view.person.id,
58 orig_post.community_id,
62 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
64 // Verify that only the creator can edit
65 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
66 return Err(LemmyError::from_message("no_post_edit_allowed"));
69 // Fetch post links and Pictrs cached image
70 let data_url = data.url.as_ref();
71 let (metadata_res, pictrs_thumbnail) =
72 fetch_site_data(context.client(), &context.settings(), data_url).await;
73 let (embed_title, embed_description, embed_html) = metadata_res
74 .map(|u| (u.title, u.description, u.html))
75 .unwrap_or((None, None, None));
77 let post_form = PostForm {
78 creator_id: orig_post.creator_id.to_owned(),
79 community_id: orig_post.community_id,
80 name: data.name.to_owned().unwrap_or(orig_post.name),
81 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
82 body: clean_optional_text(&data.body),
84 updated: Some(naive_now()),
88 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
92 let post_id = data.post_id;
93 let res = blocking(context.pool(), move |conn| {
94 Post::update(conn, post_id, &post_form)
97 let updated_post: Post = match res {
100 let err_type = if e.to_string() == "value too long for type character varying(200)" {
101 "post_title_too_long"
103 "couldnt_update_post"
106 return Err(LemmyError::from_error_message(e, err_type));
111 CreateOrUpdatePost::send(
113 &local_user_view.person.clone().into(),
114 CreateOrUpdateType::Update,
119 send_post_ws_message(
121 UserOperationCrud::EditPost,
123 Some(local_user_view.person.id),