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_url_params, is_valid_post_title},
26 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
28 use crate::PerformCrud;
30 #[async_trait::async_trait(?Send)]
31 impl PerformCrud for EditPost {
32 type Response = PostResponse;
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(ApiError::err_plain("invalid_post_title").into());
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(ApiError::err_plain("no_post_edit_allowed").into());
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: data.body.to_owned(),
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(ApiError::err(err_type, e).into());
111 CreateOrUpdatePost::send(
112 &updated_post.into(),
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),