1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, EditComment},
6 utils::{check_community_ban, get_local_user_view_from_jwt, local_site_to_slur_regex},
7 websocket::UserOperationCrud,
11 actor_language::CommunityLanguage,
12 comment::{Comment, CommentUpdateForm},
13 local_site::LocalSite,
18 use lemmy_db_views::structs::CommentView;
22 mention::scrape_text_for_mentions,
24 validation::is_valid_body_field,
29 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for EditComment {
31 type Response = CommentResponse;
33 #[tracing::instrument(skip(context, websocket_id))]
36 context: &Data<LemmyContext>,
37 websocket_id: Option<ConnectionId>,
38 ) -> Result<CommentResponse, LemmyError> {
39 let data: &EditComment = self;
41 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
42 let local_site = LocalSite::read(context.pool()).await?;
44 let comment_id = data.comment_id;
45 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
48 local_user_view.person.id,
49 orig_comment.community.id,
54 // Verify that only the creator can edit
55 if local_user_view.person.id != orig_comment.creator.id {
56 return Err(LemmyError::from_message("no_comment_edit_allowed"));
59 let language_id = self.language_id;
60 CommunityLanguage::is_allowed_community_language(
63 orig_comment.community.id,
68 let content_slurs_removed = data
71 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
73 is_valid_body_field(&content_slurs_removed)?;
75 let comment_id = data.comment_id;
76 let form = CommentUpdateForm::builder()
77 .content(content_slurs_removed)
78 .language_id(data.language_id)
79 .updated(Some(Some(naive_now())))
81 let updated_comment = Comment::update(context.pool(), comment_id, &form)
83 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
85 // Do the mentions / recipients
86 let updated_comment_content = updated_comment.content.clone();
87 let mentions = scrape_text_for_mentions(&updated_comment_content);
88 let recipient_ids = context
92 &local_user_view.person,
99 .send_comment_ws_message(
100 &UserOperationCrud::EditComment,
103 data.form_id.clone(),