1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, EditComment},
8 check_community_deleted_or_removed,
9 check_post_deleted_or_removed,
10 get_local_user_view_from_jwt,
12 local_site_to_slur_regex,
15 send::{send_comment_ws_message, send_local_notifs},
19 use lemmy_db_schema::{
21 actor_language::CommunityLanguage,
22 comment::{Comment, CommentUpdateForm},
23 local_site::LocalSite,
27 use lemmy_db_views::structs::CommentView;
30 utils::{remove_slurs, scrape_text_for_mentions},
34 #[async_trait::async_trait(?Send)]
35 impl PerformCrud for EditComment {
36 type Response = CommentResponse;
38 #[tracing::instrument(skip(context, websocket_id))]
41 context: &Data<LemmyContext>,
42 websocket_id: Option<ConnectionId>,
43 ) -> Result<CommentResponse, LemmyError> {
44 let data: &EditComment = self;
46 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
47 let local_site = LocalSite::read(context.pool()).await?;
49 let comment_id = data.comment_id;
50 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
52 // TODO is this necessary? It should really only need to check on create
54 local_user_view.person.id,
55 orig_comment.community.id,
59 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
60 check_post_deleted_or_removed(&orig_comment.post)?;
62 // Verify that only the creator can edit
63 if local_user_view.person.id != orig_comment.creator.id {
64 return Err(LemmyError::from_message("no_comment_edit_allowed"));
67 if data.distinguished.is_some() {
68 // Verify that only a mod or admin can distinguish a comment
71 local_user_view.person.id,
72 orig_comment.community.id,
77 let language_id = self.language_id;
78 CommunityLanguage::is_allowed_community_language(
81 orig_comment.community.id,
86 let content_slurs_removed = data
89 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
90 let comment_id = data.comment_id;
91 let form = CommentUpdateForm::builder()
92 .content(content_slurs_removed)
93 .distinguished(data.distinguished)
94 .language_id(data.language_id)
96 let updated_comment = Comment::update(context.pool(), comment_id, &form)
98 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
100 // Do the mentions / recipients
101 let updated_comment_content = updated_comment.content.clone();
102 let mentions = scrape_text_for_mentions(&updated_comment_content);
103 let recipient_ids = send_local_notifs(
106 &local_user_view.person,
113 send_comment_ws_message(
115 UserOperationCrud::EditComment,
117 data.form_id.clone(),