1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::{build_comment_response, send_local_notifs},
5 comment::{CommentResponse, EditComment},
7 utils::{check_community_ban, local_site_to_slur_regex, local_user_view_from_jwt},
11 actor_language::CommunityLanguage,
12 comment::{Comment, CommentUpdateForm},
13 local_site::LocalSite,
18 use lemmy_db_views::structs::CommentView;
20 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
22 mention::scrape_text_for_mentions,
24 validation::is_valid_body_field,
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for EditComment {
30 type Response = CommentResponse;
32 #[tracing::instrument(skip(context))]
33 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
34 let data: &EditComment = self;
35 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
36 let local_site = LocalSite::read(&mut context.pool()).await?;
38 let comment_id = data.comment_id;
39 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
42 local_user_view.person.id,
43 orig_comment.community.id,
48 // Verify that only the creator can edit
49 if local_user_view.person.id != orig_comment.creator.id {
50 return Err(LemmyErrorType::NoCommentEditAllowed)?;
53 let language_id = self.language_id;
54 CommunityLanguage::is_allowed_community_language(
57 orig_comment.community.id,
62 let content_slurs_removed = data
65 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
67 is_valid_body_field(&content_slurs_removed, false)?;
69 let comment_id = data.comment_id;
70 let form = CommentUpdateForm::builder()
71 .content(content_slurs_removed)
72 .language_id(data.language_id)
73 .updated(Some(Some(naive_now())))
75 let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)
77 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
79 // Do the mentions / recipients
80 let updated_comment_content = updated_comment.content.clone();
81 let mentions = scrape_text_for_mentions(&updated_comment_content);
82 let recipient_ids = send_local_notifs(
85 &local_user_view.person,
92 build_comment_response(
95 Some(local_user_view),