1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{EditPost, PostResponse},
5 request::fetch_site_data,
9 check_community_deleted_or_removed,
10 get_local_user_view_from_jwt,
13 use lemmy_apub::protocol::activities::{
14 create_or_update::post::CreateOrUpdatePost,
17 use lemmy_db_schema::{
19 actor_language::CommunityLanguage,
20 post::{Post, PostForm},
23 utils::{diesel_option_overwrite, naive_now},
27 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
30 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
32 #[async_trait::async_trait(?Send)]
33 impl PerformCrud for EditPost {
34 type Response = PostResponse;
36 #[tracing::instrument(skip(context, websocket_id))]
39 context: &Data<LemmyContext>,
40 websocket_id: Option<ConnectionId>,
41 ) -> Result<PostResponse, LemmyError> {
42 let data: &EditPost = self;
44 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
46 let data_url = data.url.as_ref();
48 // TODO No good way to handle a clear.
49 // Issue link: https://github.com/LemmyNet/lemmy/issues/2287
50 let url = Some(data_url.map(clean_url_params).map(Into::into));
51 let body = diesel_option_overwrite(&data.body);
53 let slur_regex = &context.settings().slur_regex();
54 check_slurs_opt(&data.name, slur_regex)?;
55 check_slurs_opt(&data.body, slur_regex)?;
57 if let Some(name) = &data.name {
58 if !is_valid_post_title(name) {
59 return Err(LemmyError::from_message("invalid_post_title"));
63 let post_id = data.post_id;
64 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
67 local_user_view.person.id,
68 orig_post.community_id,
72 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
74 // Verify that only the creator can edit
75 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
76 return Err(LemmyError::from_message("no_post_edit_allowed"));
79 // Fetch post links and Pictrs cached image
80 let data_url = data.url.as_ref();
81 let (metadata_res, thumbnail_url) =
82 fetch_site_data(context.client(), context.settings(), data_url).await;
83 let (embed_title, embed_description, embed_video_url) = metadata_res
84 .map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url)))
87 let language_id = self.language_id;
88 blocking(context.pool(), move |conn| {
89 CommunityLanguage::is_allowed_community_language(conn, language_id, orig_post.community_id)
93 let post_form = PostForm {
94 creator_id: orig_post.creator_id.to_owned(),
95 community_id: orig_post.community_id,
96 name: data.name.to_owned().unwrap_or(orig_post.name),
100 updated: Some(naive_now()),
104 language_id: data.language_id,
105 thumbnail_url: Some(thumbnail_url),
106 ..PostForm::default()
109 let post_id = data.post_id;
110 let res = blocking(context.pool(), move |conn| {
111 Post::update(conn, post_id, &post_form)
114 let updated_post: Post = match res {
117 let err_type = if e.to_string() == "value too long for type character varying(200)" {
118 "post_title_too_long"
120 "couldnt_update_post"
123 return Err(LemmyError::from_error_message(e, err_type));
128 CreateOrUpdatePost::send(
130 &local_user_view.person.clone().into(),
131 CreateOrUpdateType::Update,
136 send_post_ws_message(
138 UserOperationCrud::EditPost,
140 Some(local_user_view.person.id),