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,
11 local_site_to_slur_regex,
14 use lemmy_apub::protocol::activities::{
15 create_or_update::post::CreateOrUpdatePost,
18 use lemmy_db_schema::{
20 actor_language::CommunityLanguage,
21 local_site::LocalSite,
22 post::{Post, PostUpdateForm},
25 utils::diesel_option_overwrite,
29 utils::{check_slurs_opt, clean_url_params, is_valid_post_title},
32 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
34 #[async_trait::async_trait(?Send)]
35 impl PerformCrud for EditPost {
36 type Response = PostResponse;
38 #[tracing::instrument(skip(context, websocket_id))]
41 context: &Data<LemmyContext>,
42 websocket_id: Option<ConnectionId>,
43 ) -> Result<PostResponse, LemmyError> {
44 let data: &EditPost = self;
46 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
47 let local_site = blocking(context.pool(), LocalSite::read).await??;
49 let data_url = data.url.as_ref();
51 // TODO No good way to handle a clear.
52 // Issue link: https://github.com/LemmyNet/lemmy/issues/2287
53 let url = Some(data_url.map(clean_url_params).map(Into::into));
54 let body = diesel_option_overwrite(&data.body);
56 let slur_regex = local_site_to_slur_regex(&local_site);
57 check_slurs_opt(&data.name, &slur_regex)?;
58 check_slurs_opt(&data.body, &slur_regex)?;
60 if let Some(name) = &data.name {
61 if !is_valid_post_title(name) {
62 return Err(LemmyError::from_message("invalid_post_title"));
66 let post_id = data.post_id;
67 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
70 local_user_view.person.id,
71 orig_post.community_id,
75 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
77 // Verify that only the creator can edit
78 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
79 return Err(LemmyError::from_message("no_post_edit_allowed"));
82 // Fetch post links and Pictrs cached image
83 let data_url = data.url.as_ref();
84 let (metadata_res, thumbnail_url) =
85 fetch_site_data(context.client(), context.settings(), data_url).await;
86 let (embed_title, embed_description, embed_video_url) = metadata_res
87 .map(|u| (Some(u.title), Some(u.description), Some(u.embed_video_url)))
90 let language_id = self.language_id;
91 blocking(context.pool(), move |conn| {
92 CommunityLanguage::is_allowed_community_language(conn, language_id, orig_post.community_id)
96 let post_form = PostUpdateForm::builder()
97 .name(data.name.to_owned())
101 .embed_title(embed_title)
102 .embed_description(embed_description)
103 .embed_video_url(embed_video_url)
104 .language_id(data.language_id)
105 .thumbnail_url(Some(thumbnail_url))
108 let post_id = data.post_id;
109 let res = blocking(context.pool(), move |conn| {
110 Post::update(conn, post_id, &post_form)
113 let updated_post: Post = match res {
116 let err_type = if e.to_string() == "value too long for type character varying(200)" {
117 "post_title_too_long"
119 "couldnt_update_post"
122 return Err(LemmyError::from_error_message(e, err_type));
127 CreateOrUpdatePost::send(
129 &local_user_view.person.clone().into(),
130 CreateOrUpdateType::Update,
135 send_post_ws_message(
137 UserOperationCrud::EditPost,
139 Some(local_user_view.person.id),