1 use actix_web::web::Data;
2 use lemmy_api_common::{
3 comment::{CommentResponse, EditComment},
7 check_community_deleted_or_removed,
8 check_post_deleted_or_removed,
9 get_local_user_view_from_jwt,
13 use lemmy_apub::protocol::activities::{
14 create_or_update::comment::CreateOrUpdateComment,
17 use lemmy_db_schema::{
18 source::comment::{Comment, CommentForm},
21 use lemmy_db_views::structs::CommentView;
24 utils::{remove_slurs, scrape_text_for_mentions},
27 use lemmy_websocket::{
28 send::{send_comment_ws_message, send_local_notifs},
33 use crate::PerformCrud;
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?;
49 let comment_id = data.comment_id;
50 let orig_comment = blocking(context.pool(), move |conn| {
51 CommentView::read(conn, comment_id, None)
55 // TODO is this necessary? It should really only need to check on create
57 local_user_view.person.id,
58 orig_comment.community.id,
62 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
63 check_post_deleted_or_removed(&orig_comment.post)?;
65 // Verify that only the creator can edit
66 if local_user_view.person.id != orig_comment.creator.id {
67 return Err(LemmyError::from_message("no_comment_edit_allowed"));
70 if data.distinguished.is_some() {
71 // Verify that only a mod or admin can distinguish a comment
74 local_user_view.person.id,
75 orig_comment.community.id,
81 let content_slurs_removed = data
84 .map(|c| remove_slurs(c, &context.settings().slur_regex()));
85 let comment_id = data.comment_id;
86 let form = CommentForm {
87 creator_id: orig_comment.comment.creator_id,
88 post_id: orig_comment.comment.post_id,
89 content: content_slurs_removed.unwrap_or(orig_comment.comment.content),
90 distinguished: data.distinguished,
91 language_id: data.language_id,
94 let updated_comment = blocking(context.pool(), move |conn| {
95 Comment::update(conn, 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.to_owned();
102 let mentions = scrape_text_for_mentions(&updated_comment_content);
103 let recipient_ids = send_local_notifs(
106 &local_user_view.person,
113 // Send the apub update
114 CreateOrUpdateComment::send(
115 updated_comment.into(),
116 &local_user_view.person.into(),
117 CreateOrUpdateType::Update,
123 send_comment_ws_message(
125 UserOperationCrud::EditComment,
127 data.form_id.to_owned(),