1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, RemoveComment},
5 utils::{blocking, 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 = blocking(context.pool(), move |conn| {
41 CommentView::read(conn, comment_id, None)
46 local_user_view.person.id,
47 orig_comment.community.id,
52 // Verify that only a mod or admin can remove
55 local_user_view.person.id,
56 orig_comment.community.id,
61 let removed = data.removed;
62 let updated_comment = blocking(context.pool(), move |conn| {
66 &CommentUpdateForm::builder().removed(Some(removed)).build(),
70 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
73 let form = ModRemoveCommentForm {
74 mod_person_id: local_user_view.person.id,
75 comment_id: data.comment_id,
76 removed: Some(removed),
77 reason: data.reason.to_owned(),
79 blocking(context.pool(), move |conn| {
80 ModRemoveComment::create(conn, &form)
84 let post_id = updated_comment.post_id;
85 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
86 let recipient_ids = send_local_notifs(
89 &local_user_view.person.clone(),
96 let res = send_comment_ws_message(
98 UserOperationCrud::RemoveComment,
100 None, // TODO maybe this might clear other forms
101 Some(local_user_view.person.id),
107 // Send the apub message
108 let community = blocking(context.pool(), move |conn| {
109 Community::read(conn, orig_comment.post.community_id)
112 let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
113 send_apub_delete_in_community(
114 local_user_view.person,
117 data.reason.clone().or_else(|| Some("".to_string())),