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_schema::{
13 source::post::{Post, PostForm},
17 request::fetch_site_data,
18 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
23 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for EditPost {
27 type Response = PostResponse;
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<PostResponse, LemmyError> {
34 let data: &EditPost = self;
36 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 let slur_regex = &context.settings().slur_regex();
39 check_slurs_opt(&data.name, slur_regex)?;
40 check_slurs_opt(&data.body, slur_regex)?;
42 if let Some(name) = &data.name {
43 if !is_valid_post_title(name) {
44 return Err(ApiError::err_plain("invalid_post_title").into());
48 let post_id = data.post_id;
49 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
52 local_user_view.person.id,
53 orig_post.community_id,
57 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
59 // Verify that only the creator can edit
60 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
61 return Err(ApiError::err_plain("no_post_edit_allowed").into());
64 // Fetch post links and Pictrs cached image
65 let data_url = data.url.as_ref();
66 let (metadata_res, pictrs_thumbnail) =
67 fetch_site_data(context.client(), &context.settings(), data_url).await;
68 let (embed_title, embed_description, embed_html) = metadata_res
69 .map(|u| (u.title, u.description, u.html))
70 .unwrap_or((None, None, None));
72 let post_form = PostForm {
73 creator_id: orig_post.creator_id.to_owned(),
74 community_id: orig_post.community_id,
75 name: data.name.to_owned().unwrap_or(orig_post.name),
76 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
77 body: data.body.to_owned(),
79 updated: Some(naive_now()),
83 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
87 let post_id = data.post_id;
88 let res = blocking(context.pool(), move |conn| {
89 Post::update(conn, post_id, &post_form)
92 let updated_post: Post = match res {
95 let err_type = if e.to_string() == "value too long for type character varying(200)" {
101 return Err(ApiError::err(err_type, e).into());
106 CreateOrUpdatePost::send(
108 &local_user_view.person,
109 CreateOrUpdateType::Update,
114 send_post_ws_message(
116 UserOperationCrud::EditPost,
118 Some(local_user_view.person.id),