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};
12 moderator::{ModRemoveComment, ModRemoveCommentForm},
17 use lemmy_db_views::structs::CommentView;
18 use lemmy_utils::{ConnectionId, LemmyError};
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| {
63 Comment::update_removed(conn, comment_id, removed)
66 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
69 let form = ModRemoveCommentForm {
70 mod_person_id: local_user_view.person.id,
71 comment_id: data.comment_id,
72 removed: Some(removed),
73 reason: data.reason.to_owned(),
75 blocking(context.pool(), move |conn| {
76 ModRemoveComment::create(conn, &form)
80 let post_id = updated_comment.post_id;
81 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
82 let recipient_ids = send_local_notifs(
85 &local_user_view.person.clone(),
92 let res = send_comment_ws_message(
94 UserOperationCrud::RemoveComment,
96 None, // TODO maybe this might clear other forms
97 Some(local_user_view.person.id),
103 // Send the apub message
104 let community = blocking(context.pool(), move |conn| {
105 Community::read(conn, orig_comment.post.community_id)
108 let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
109 send_apub_delete_in_community(
110 local_user_view.person,
113 data.reason.clone().or_else(|| Some("".to_string())),