1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, EditComment},
8 get_local_user_view_from_jwt,
10 local_site_to_slur_regex,
13 send::{send_comment_ws_message, send_local_notifs},
17 use lemmy_db_schema::{
19 actor_language::CommunityLanguage,
20 comment::{Comment, CommentUpdateForm},
21 local_site::LocalSite,
26 use lemmy_db_views::structs::CommentView;
29 utils::{mention::scrape_text_for_mentions, slurs::remove_slurs},
33 #[async_trait::async_trait(?Send)]
34 impl PerformCrud for EditComment {
35 type Response = CommentResponse;
37 #[tracing::instrument(skip(context, websocket_id))]
40 context: &Data<LemmyContext>,
41 websocket_id: Option<ConnectionId>,
42 ) -> Result<CommentResponse, LemmyError> {
43 let data: &EditComment = self;
45 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
46 let local_site = LocalSite::read(context.pool()).await?;
48 let comment_id = data.comment_id;
49 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
52 local_user_view.person.id,
53 orig_comment.community.id,
58 // Verify that only the creator can edit
59 if local_user_view.person.id != orig_comment.creator.id {
60 return Err(LemmyError::from_message("no_comment_edit_allowed"));
63 if data.distinguished.is_some() {
64 // Verify that only a mod or admin can distinguish a comment
67 local_user_view.person.id,
68 orig_comment.community.id,
73 let language_id = self.language_id;
74 CommunityLanguage::is_allowed_community_language(
77 orig_comment.community.id,
82 let content_slurs_removed = data
85 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
86 let comment_id = data.comment_id;
87 let form = CommentUpdateForm::builder()
88 .content(content_slurs_removed)
89 .distinguished(data.distinguished)
90 .language_id(data.language_id)
91 .updated(Some(Some(naive_now())))
93 let updated_comment = Comment::update(context.pool(), comment_id, &form)
95 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
97 // Do the mentions / recipients
98 let updated_comment_content = updated_comment.content.clone();
99 let mentions = scrape_text_for_mentions(&updated_comment_content);
100 let recipient_ids = send_local_notifs(
103 &local_user_view.person,
110 send_comment_ws_message(
112 UserOperationCrud::EditComment,
114 data.form_id.clone(),