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,
12 use lemmy_apub::protocol::activities::{
13 create_or_update::comment::CreateOrUpdateComment,
16 use lemmy_db_schema::source::comment::Comment;
17 use lemmy_db_views::structs::CommentView;
19 utils::{remove_slurs, scrape_text_for_mentions},
23 use lemmy_websocket::{
24 send::{send_comment_ws_message, send_local_notifs},
29 use crate::PerformCrud;
31 #[async_trait::async_trait(?Send)]
32 impl PerformCrud for EditComment {
33 type Response = CommentResponse;
35 #[tracing::instrument(skip(context, websocket_id))]
38 context: &Data<LemmyContext>,
39 websocket_id: Option<ConnectionId>,
40 ) -> Result<CommentResponse, LemmyError> {
41 let data: &EditComment = self;
43 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
45 let comment_id = data.comment_id;
46 let orig_comment = blocking(context.pool(), move |conn| {
47 CommentView::read(conn, comment_id, None)
51 // TODO is this necessary? It should really only need to check on create
53 local_user_view.person.id,
54 orig_comment.community.id,
58 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
59 check_post_deleted_or_removed(&orig_comment.post)?;
61 // Verify that only the creator can edit
62 if local_user_view.person.id != orig_comment.creator.id {
63 return Err(LemmyError::from_message("no_comment_edit_allowed"));
67 let content_slurs_removed =
68 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
69 let comment_id = data.comment_id;
70 let updated_comment = blocking(context.pool(), move |conn| {
71 Comment::update_content(conn, comment_id, &content_slurs_removed)
74 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
76 // Do the mentions / recipients
77 let updated_comment_content = updated_comment.content.to_owned();
78 let mentions = scrape_text_for_mentions(&updated_comment_content);
79 let recipient_ids = send_local_notifs(
82 &local_user_view.person,
89 // Send the apub update
90 CreateOrUpdateComment::send(
91 updated_comment.into(),
92 &local_user_view.person.into(),
93 CreateOrUpdateType::Update,
99 send_comment_ws_message(
101 UserOperationCrud::EditComment,
103 data.form_id.to_owned(),