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},
9 local_site_to_slur_regex,
10 local_user_view_from_jwt,
14 use lemmy_db_schema::{
16 actor_language::CommunityLanguage,
17 comment::{Comment, CommentUpdateForm},
18 local_site::LocalSite,
23 use lemmy_db_views::structs::CommentView;
25 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
27 mention::scrape_text_for_mentions,
29 validation::is_valid_body_field,
33 #[async_trait::async_trait(?Send)]
34 impl PerformCrud for EditComment {
35 type Response = CommentResponse;
37 #[tracing::instrument(skip(context))]
38 async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
39 let data: &EditComment = self;
40 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
41 let local_site = LocalSite::read(&mut context.pool()).await?;
43 let comment_id = data.comment_id;
44 let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
47 local_user_view.person.id,
48 orig_comment.community.id,
53 // Verify that only the creator can edit
54 if local_user_view.person.id != orig_comment.creator.id {
55 return Err(LemmyErrorType::NoCommentEditAllowed)?;
58 let language_id = self.language_id;
59 CommunityLanguage::is_allowed_community_language(
62 orig_comment.community.id,
70 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
71 is_valid_body_field(&content, false)?;
72 let content = sanitize_html_opt(&content);
74 let comment_id = data.comment_id;
75 let form = CommentUpdateForm::builder()
77 .language_id(data.language_id)
78 .updated(Some(Some(naive_now())))
80 let updated_comment = Comment::update(&mut context.pool(), comment_id, &form)
82 .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
84 // Do the mentions / recipients
85 let updated_comment_content = updated_comment.content.clone();
86 let mentions = scrape_text_for_mentions(&updated_comment_content);
87 let recipient_ids = send_local_notifs(
90 &local_user_view.person,
97 build_comment_response(
100 Some(local_user_view),
101 self.form_id.clone(),