]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/delete.rs
Moving settings to Database. (#2492)
[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::{
10     comment::{Comment, CommentUpdateForm},
11     community::Community,
12     post::Post,
13   },
14   traits::Crud,
15 };
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},
20   LemmyContext,
21   UserOperationCrud,
22 };
23
24 #[async_trait::async_trait(?Send)]
25 impl PerformCrud for DeleteComment {
26   type Response = CommentResponse;
27
28   #[tracing::instrument(skip(context, websocket_id))]
29   async fn perform(
30     &self,
31     context: &Data<LemmyContext>,
32     websocket_id: Option<ConnectionId>,
33   ) -> Result<CommentResponse, LemmyError> {
34     let data: &DeleteComment = self;
35     let local_user_view =
36       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37
38     let comment_id = data.comment_id;
39     let orig_comment = blocking(context.pool(), move |conn| {
40       CommentView::read(conn, comment_id, None)
41     })
42     .await??;
43
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"));
47     }
48
49     check_community_ban(
50       local_user_view.person.id,
51       orig_comment.community.id,
52       context.pool(),
53     )
54     .await?;
55
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"));
59     }
60
61     // Do the delete
62     let deleted = data.deleted;
63     let updated_comment = blocking(context.pool(), move |conn| {
64       Comment::update(
65         conn,
66         comment_id,
67         &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
68       )
69     })
70     .await?
71     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
72
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(
76       vec![],
77       &updated_comment,
78       &local_user_view.person,
79       &post,
80       false,
81       context,
82     )
83     .await?;
84
85     let res = send_comment_ws_message(
86       data.comment_id,
87       UserOperationCrud::DeleteComment,
88       websocket_id,
89       None, // TODO a comment delete might clear forms?
90       Some(local_user_view.person.id),
91       recipient_ids,
92       context,
93     )
94     .await?;
95
96     // Send the apub message
97     let community = blocking(context.pool(), move |conn| {
98       Community::read(conn, orig_comment.post.community_id)
99     })
100     .await??;
101     let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
102     send_apub_delete_in_community(
103       local_user_view.person,
104       community,
105       deletable,
106       None,
107       deleted,
108       context,
109     )
110     .await?;
111
112     Ok(res)
113   }
114 }