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,
28 use lemmy_db_views::structs::CommentView;
31 utils::{remove_slurs, scrape_text_for_mentions},
35 #[async_trait::async_trait(?Send)]
36 impl PerformCrud for EditComment {
37 type Response = CommentResponse;
39 #[tracing::instrument(skip(context, websocket_id))]
42 context: &Data<LemmyContext>,
43 websocket_id: Option<ConnectionId>,
44 ) -> Result<CommentResponse, LemmyError> {
45 let data: &EditComment = self;
47 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
48 let local_site = LocalSite::read(context.pool()).await?;
50 let comment_id = data.comment_id;
51 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
53 // TODO is this necessary? It should really only need to check on create
55 local_user_view.person.id,
56 orig_comment.community.id,
60 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
61 check_post_deleted_or_removed(&orig_comment.post)?;
63 // Verify that only the creator can edit
64 if local_user_view.person.id != orig_comment.creator.id {
65 return Err(LemmyError::from_message("no_comment_edit_allowed"));
68 if data.distinguished.is_some() {
69 // Verify that only a mod or admin can distinguish a comment
72 local_user_view.person.id,
73 orig_comment.community.id,
78 let language_id = self.language_id;
79 CommunityLanguage::is_allowed_community_language(
82 orig_comment.community.id,
87 let content_slurs_removed = data
90 .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
91 let comment_id = data.comment_id;
92 let form = CommentUpdateForm::builder()
93 .content(content_slurs_removed)
94 .distinguished(data.distinguished)
95 .language_id(data.language_id)
96 .updated(Some(Some(naive_now())))
98 let updated_comment = Comment::update(context.pool(), comment_id, &form)
100 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
102 // Do the mentions / recipients
103 let updated_comment_content = updated_comment.content.clone();
104 let mentions = scrape_text_for_mentions(&updated_comment_content);
105 let recipient_ids = send_local_notifs(
108 &local_user_view.person,
115 send_comment_ws_message(
117 UserOperationCrud::EditComment,
119 data.form_id.clone(),