1 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 check_post_deleted_or_removed,
9 get_local_user_view_from_jwt,
11 use lemmy_apub::protocol::activities::{
12 create_or_update::comment::CreateOrUpdateComment,
15 use lemmy_db_schema::source::comment::Comment;
16 use lemmy_db_views::comment_view::CommentView;
18 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;
37 context: &Data<LemmyContext>,
38 websocket_id: Option<ConnectionId>,
39 ) -> Result<CommentResponse, LemmyError> {
40 let data: &EditComment = self;
42 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
44 let comment_id = data.comment_id;
45 let orig_comment = blocking(context.pool(), move |conn| {
46 CommentView::read(conn, comment_id, None)
50 // TODO is this necessary? It should really only need to check on create
52 local_user_view.person.id,
53 orig_comment.community.id,
57 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
58 check_post_deleted_or_removed(&orig_comment.post)?;
60 // Verify that only the creator can edit
61 if local_user_view.person.id != orig_comment.creator.id {
62 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
66 let content_slurs_removed =
67 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
68 let comment_id = data.comment_id;
69 let updated_comment = blocking(context.pool(), move |conn| {
70 Comment::update_content(conn, comment_id, &content_slurs_removed)
73 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
75 // Do the mentions / recipients
76 let updated_comment_content = updated_comment.content.to_owned();
77 let mentions = scrape_text_for_mentions(&updated_comment_content);
78 let recipient_ids = send_local_notifs(
81 &local_user_view.person,
88 // Send the apub update
89 CreateOrUpdateComment::send(
90 updated_comment.into(),
91 &local_user_view.person.into(),
92 CreateOrUpdateType::Update,
98 send_comment_ws_message(
100 UserOperationCrud::EditComment,
102 data.form_id.to_owned(),