1 use crate::PerformCrud;
2 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::activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType};
11 use lemmy_db_queries::{source::post::Post_, Crud};
12 use lemmy_db_schema::{naive_now, source::post::*};
14 request::fetch_site_data,
15 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
20 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for EditPost {
24 type Response = PostResponse;
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<PostResponse, LemmyError> {
31 let data: &EditPost = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35 let slur_regex = &context.settings().slur_regex();
36 check_slurs_opt(&data.name, slur_regex)?;
37 check_slurs_opt(&data.body, slur_regex)?;
39 if let Some(name) = &data.name {
40 if !is_valid_post_title(name) {
41 return Err(ApiError::err_plain("invalid_post_title").into());
45 let post_id = data.post_id;
46 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
49 local_user_view.person.id,
50 orig_post.community_id,
54 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
56 // Verify that only the creator can edit
57 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
58 return Err(ApiError::err_plain("no_post_edit_allowed").into());
61 // Fetch post links and Pictrs cached image
62 let data_url = data.url.as_ref();
63 let (metadata_res, pictrs_thumbnail) =
64 fetch_site_data(context.client(), &context.settings(), data_url).await;
65 let (embed_title, embed_description, embed_html) = metadata_res
66 .map(|u| (u.title, u.description, u.html))
67 .unwrap_or((None, None, None));
69 let post_form = PostForm {
70 creator_id: orig_post.creator_id.to_owned(),
71 community_id: orig_post.community_id,
72 name: data.name.to_owned().unwrap_or(orig_post.name),
73 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
74 body: data.body.to_owned(),
76 updated: Some(naive_now()),
80 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
84 let post_id = data.post_id;
85 let res = blocking(context.pool(), move |conn| {
86 Post::update(conn, post_id, &post_form)
89 let updated_post: Post = match res {
92 let err_type = if e.to_string() == "value too long for type character varying(200)" {
98 return Err(ApiError::err(err_type, e).into());
103 CreateOrUpdatePost::send(
105 &local_user_view.person,
106 CreateOrUpdateType::Update,
111 send_post_ws_message(
113 UserOperationCrud::EditPost,
115 Some(local_user_view.person.id),