1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, DeleteComment},
5 utils::{blocking, 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 = blocking(context.pool(), move |conn| {
40 CommentView::read(conn, comment_id, None)
44 // Dont delete it if its already been deleted.
45 if orig_comment.comment.deleted == data.deleted {
46 return Err(LemmyError::from_message("couldnt_update_comment"));
50 local_user_view.person.id,
51 orig_comment.community.id,
56 // Verify that only the creator can delete
57 if local_user_view.person.id != orig_comment.creator.id {
58 return Err(LemmyError::from_message("no_comment_edit_allowed"));
62 let deleted = data.deleted;
63 let updated_comment = blocking(context.pool(), move |conn| {
67 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
71 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
73 let post_id = updated_comment.post_id;
74 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
75 let recipient_ids = send_local_notifs(
78 &local_user_view.person,
85 let res = send_comment_ws_message(
87 UserOperationCrud::DeleteComment,
89 None, // TODO a comment delete might clear forms?
90 Some(local_user_view.person.id),
96 // Send the apub message
97 let community = blocking(context.pool(), move |conn| {
98 Community::read(conn, orig_comment.post.community_id)
101 let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
102 send_apub_delete_in_community(
103 local_user_view.person,