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};
9 source::{comment::Comment, community::Community, post::Post},
12 use lemmy_db_views::structs::CommentView;
13 use lemmy_utils::{error::LemmyError, ConnectionId};
14 use lemmy_websocket::{
15 send::{send_comment_ws_message, send_local_notifs},
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for DeleteComment {
22 type Response = CommentResponse;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<CommentResponse, LemmyError> {
30 let data: &DeleteComment = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 let comment_id = data.comment_id;
35 let orig_comment = blocking(context.pool(), move |conn| {
36 CommentView::read(conn, comment_id, None)
40 // Dont delete it if its already been deleted.
41 if orig_comment.comment.deleted == data.deleted {
42 return Err(LemmyError::from_message("couldnt_update_comment"));
46 local_user_view.person.id,
47 orig_comment.community.id,
52 // Verify that only the creator can delete
53 if local_user_view.person.id != orig_comment.creator.id {
54 return Err(LemmyError::from_message("no_comment_edit_allowed"));
58 let deleted = data.deleted;
59 let updated_comment = blocking(context.pool(), move |conn| {
60 Comment::update_deleted(conn, comment_id, deleted)
63 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
65 let post_id = updated_comment.post_id;
66 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
67 let recipient_ids = send_local_notifs(
70 &local_user_view.person,
77 let res = send_comment_ws_message(
79 UserOperationCrud::DeleteComment,
81 None, // TODO a comment delete might clear forms?
82 Some(local_user_view.person.id),
88 // Send the apub message
89 let community = blocking(context.pool(), move |conn| {
90 Community::read(conn, orig_comment.post.community_id)
93 let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
94 send_apub_delete_in_community(
95 local_user_view.person,