1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, RemoveComment},
5 utils::{check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin},
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
10 comment::{Comment, CommentUpdateForm},
12 moderator::{ModRemoveComment, ModRemoveCommentForm},
17 use lemmy_db_views::structs::CommentView;
18 use lemmy_utils::{error::LemmyError, ConnectionId};
19 use lemmy_websocket::{
20 send::{send_comment_ws_message, send_local_notifs},
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for RemoveComment {
27 type Response = CommentResponse;
29 #[tracing::instrument(skip(context, websocket_id))]
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<CommentResponse, LemmyError> {
35 let data: &RemoveComment = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 let comment_id = data.comment_id;
40 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
43 local_user_view.person.id,
44 orig_comment.community.id,
49 // Verify that only a mod or admin can remove
52 local_user_view.person.id,
53 orig_comment.community.id,
58 let removed = data.removed;
59 let updated_comment = Comment::update(
62 &CommentUpdateForm::builder().removed(Some(removed)).build(),
65 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
68 let form = ModRemoveCommentForm {
69 mod_person_id: local_user_view.person.id,
70 comment_id: data.comment_id,
71 removed: Some(removed),
72 reason: data.reason.to_owned(),
74 ModRemoveComment::create(context.pool(), &form).await?;
76 let post_id = updated_comment.post_id;
77 let post = Post::read(context.pool(), post_id).await?;
78 let recipient_ids = send_local_notifs(
81 &local_user_view.person.clone(),
88 let res = send_comment_ws_message(
90 UserOperationCrud::RemoveComment,
92 None, // TODO maybe this might clear other forms
93 Some(local_user_view.person.id),
99 // Send the apub message
100 let community = Community::read(context.pool(), orig_comment.post.community_id).await?;
101 let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
102 send_apub_delete_in_community(
103 local_user_view.person,
106 data.reason.clone().or_else(|| Some("".to_string())),