1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, check_community_ban, get_local_user_view_from_jwt, post::*};
4 use lemmy_apub::activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType};
5 use lemmy_db_queries::{source::post::Post_, Crud};
6 use lemmy_db_schema::{naive_now, source::post::*};
8 request::fetch_site_data,
9 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
14 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for EditPost {
18 type Response = PostResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PostResponse, LemmyError> {
25 let data: &EditPost = self;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 let slur_regex = &context.settings().slur_regex();
30 check_slurs_opt(&data.name, slur_regex)?;
31 check_slurs_opt(&data.body, slur_regex)?;
33 if let Some(name) = &data.name {
34 if !is_valid_post_title(name) {
35 return Err(ApiError::err_plain("invalid_post_title").into());
39 let post_id = data.post_id;
40 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
43 local_user_view.person.id,
44 orig_post.community_id,
49 // Verify that only the creator can edit
50 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
51 return Err(ApiError::err_plain("no_post_edit_allowed").into());
54 // Fetch post links and Pictrs cached image
55 let data_url = data.url.as_ref();
56 let (metadata_res, pictrs_thumbnail) =
57 fetch_site_data(context.client(), &context.settings(), data_url).await;
58 let (embed_title, embed_description, embed_html) = metadata_res
59 .map(|u| (u.title, u.description, u.html))
60 .unwrap_or((None, None, None));
62 let post_form = PostForm {
63 creator_id: orig_post.creator_id.to_owned(),
64 community_id: orig_post.community_id,
65 name: data.name.to_owned().unwrap_or(orig_post.name),
66 url: data_url.map(|u| clean_url_params(u.to_owned()).into()),
67 body: data.body.to_owned(),
69 updated: Some(naive_now()),
73 thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
77 let post_id = data.post_id;
78 let res = blocking(context.pool(), move |conn| {
79 Post::update(conn, post_id, &post_form)
82 let updated_post: Post = match res {
85 let err_type = if e.to_string() == "value too long for type character varying(200)" {
91 return Err(ApiError::err(err_type, e).into());
96 CreateOrUpdatePost::send(
98 &local_user_view.person,
99 CreateOrUpdateType::Update,
104 send_post_ws_message(
106 UserOperationCrud::EditPost,
108 Some(local_user_view.person.id),