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::source::comment::Comment;
18 use lemmy_db_views::structs::CommentView;
21 utils::{remove_slurs, scrape_text_for_mentions},
24 use lemmy_websocket::{
25 send::{send_comment_ws_message, send_local_notifs},
30 use crate::PerformCrud;
32 #[async_trait::async_trait(?Send)]
33 impl PerformCrud for EditComment {
34 type Response = CommentResponse;
36 #[tracing::instrument(skip(context, websocket_id))]
39 context: &Data<LemmyContext>,
40 websocket_id: Option<ConnectionId>,
41 ) -> Result<CommentResponse, LemmyError> {
42 let data: &EditComment = self;
44 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
46 let comment_id = data.comment_id;
47 let orig_comment = blocking(context.pool(), move |conn| {
48 CommentView::read(conn, comment_id, None)
51 let mut updated_comment = orig_comment.comment.clone();
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 let Some(distinguished) = data.distinguished {
69 // Verify that only a mod or admin can distinguish a comment
72 local_user_view.person.id,
73 orig_comment.community.id,
77 updated_comment = blocking(context.pool(), move |conn| {
78 Comment::update_distinguished(conn, comment_id, distinguished)
81 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
85 if let Some(content) = &data.content {
86 let content_slurs_removed = remove_slurs(content, &context.settings().slur_regex());
87 let comment_id = data.comment_id;
88 updated_comment = blocking(context.pool(), move |conn| {
89 Comment::update_content(conn, comment_id, &content_slurs_removed)
92 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
95 // Do the mentions / recipients
96 let updated_comment_content = updated_comment.content.to_owned();
97 let mentions = scrape_text_for_mentions(&updated_comment_content);
98 let recipient_ids = send_local_notifs(
101 &local_user_view.person,
108 // Send the apub update
109 CreateOrUpdateComment::send(
110 updated_comment.into(),
111 &local_user_view.person.into(),
112 CreateOrUpdateType::Update,
118 send_comment_ws_message(
120 UserOperationCrud::EditComment,
122 data.form_id.to_owned(),