1 use crate::PerformCrud;
2 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::activities::{
12 comment::create_or_update::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 #[async_trait::async_trait(?Send)]
30 impl PerformCrud for EditComment {
31 type Response = CommentResponse;
35 context: &Data<LemmyContext>,
36 websocket_id: Option<ConnectionId>,
37 ) -> Result<CommentResponse, LemmyError> {
38 let data: &EditComment = self;
40 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
42 let comment_id = data.comment_id;
43 let orig_comment = blocking(context.pool(), move |conn| {
44 CommentView::read(conn, comment_id, None)
48 // TODO is this necessary? It should really only need to check on create
50 local_user_view.person.id,
51 orig_comment.community.id,
55 check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;
56 check_post_deleted_or_removed(&orig_comment.post)?;
58 // Verify that only the creator can edit
59 if local_user_view.person.id != orig_comment.creator.id {
60 return Err(ApiError::err_plain("no_comment_edit_allowed").into());
64 let content_slurs_removed =
65 remove_slurs(&data.content.to_owned(), &context.settings().slur_regex());
66 let comment_id = data.comment_id;
67 let updated_comment = blocking(context.pool(), move |conn| {
68 Comment::update_content(conn, comment_id, &content_slurs_removed)
71 .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
73 // Send the apub update
74 CreateOrUpdateComment::send(
75 &updated_comment.clone().into(),
76 &local_user_view.person.clone().into(),
77 CreateOrUpdateType::Update,
82 // Do the mentions / recipients
83 let updated_comment_content = updated_comment.content.to_owned();
84 let mentions = scrape_text_for_mentions(&updated_comment_content);
85 let recipient_ids = send_local_notifs(
88 &local_user_view.person,
95 send_comment_ws_message(
97 UserOperationCrud::EditComment,
99 data.form_id.to_owned(),