1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, DeleteComment},
5 utils::{check_community_ban, get_local_user_view_from_jwt},
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
10 comment::{Comment, CommentUpdateForm},
16 use lemmy_db_views::structs::CommentView;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{
19 send::{send_comment_ws_message, send_local_notifs},
24 #[async_trait::async_trait(?Send)]
25 impl PerformCrud for DeleteComment {
26 type Response = CommentResponse;
28 #[tracing::instrument(skip(context, websocket_id))]
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<CommentResponse, LemmyError> {
34 let data: &DeleteComment = self;
36 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 let comment_id = data.comment_id;
39 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
41 // Dont delete it if its already been deleted.
42 if orig_comment.comment.deleted == data.deleted {
43 return Err(LemmyError::from_message("couldnt_update_comment"));
47 local_user_view.person.id,
48 orig_comment.community.id,
53 // Verify that only the creator can delete
54 if local_user_view.person.id != orig_comment.creator.id {
55 return Err(LemmyError::from_message("no_comment_edit_allowed"));
59 let deleted = data.deleted;
60 let updated_comment = Comment::update(
63 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
66 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
68 let post_id = updated_comment.post_id;
69 let post = Post::read(context.pool(), post_id).await?;
70 let recipient_ids = send_local_notifs(
73 &local_user_view.person,
80 let res = send_comment_ws_message(
82 UserOperationCrud::DeleteComment,
84 None, // TODO a comment delete might clear forms?
85 Some(local_user_view.person.id),
91 // Send the apub message
92 let community = Community::read(context.pool(), orig_comment.post.community_id).await?;
93 let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
94 send_apub_delete_in_community(
95 local_user_view.person,