]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/delete.rs
Extract Activitypub logic into separate library (#2288)
[lemmy.git] / crates / api_crud / src / comment / delete.rs
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},
6 };
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
8 use lemmy_db_schema::{
9   source::{comment::Comment, community::Community, post::Post},
10   traits::Crud,
11 };
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},
16   LemmyContext,
17   UserOperationCrud,
18 };
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for DeleteComment {
22   type Response = CommentResponse;
23
24   #[tracing::instrument(skip(context, websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     websocket_id: Option<ConnectionId>,
29   ) -> Result<CommentResponse, LemmyError> {
30     let data: &DeleteComment = self;
31     let local_user_view =
32       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33
34     let comment_id = data.comment_id;
35     let orig_comment = blocking(context.pool(), move |conn| {
36       CommentView::read(conn, comment_id, None)
37     })
38     .await??;
39
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"));
43     }
44
45     check_community_ban(
46       local_user_view.person.id,
47       orig_comment.community.id,
48       context.pool(),
49     )
50     .await?;
51
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"));
55     }
56
57     // Do the delete
58     let deleted = data.deleted;
59     let updated_comment = blocking(context.pool(), move |conn| {
60       Comment::update_deleted(conn, comment_id, deleted)
61     })
62     .await?
63     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
64
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(
68       vec![],
69       &updated_comment,
70       &local_user_view.person,
71       &post,
72       false,
73       context,
74     )
75     .await?;
76
77     let res = send_comment_ws_message(
78       data.comment_id,
79       UserOperationCrud::DeleteComment,
80       websocket_id,
81       None, // TODO a comment delete might clear forms?
82       Some(local_user_view.person.id),
83       recipient_ids,
84       context,
85     )
86     .await?;
87
88     // Send the apub message
89     let community = blocking(context.pool(), move |conn| {
90       Community::read(conn, orig_comment.post.community_id)
91     })
92     .await??;
93     let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
94     send_apub_delete_in_community(
95       local_user_view.person,
96       community,
97       deletable,
98       None,
99       deleted,
100       context,
101     )
102     .await?;
103
104     Ok(res)
105   }
106 }