]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/delete.rs
Rework error handling (fixes #1714) (#2135)
[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   blocking,
5   check_community_ban,
6   comment::*,
7   get_local_user_view_from_jwt,
8   is_mod_or_admin,
9 };
10 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
11 use lemmy_db_schema::{
12   source::{
13     comment::Comment,
14     community::Community,
15     moderator::{ModRemoveComment, ModRemoveCommentForm},
16     post::Post,
17   },
18   traits::Crud,
19 };
20 use lemmy_db_views::comment_view::CommentView;
21 use lemmy_utils::{ConnectionId, LemmyError};
22 use lemmy_websocket::{
23   send::{send_comment_ws_message, send_local_notifs},
24   LemmyContext,
25   UserOperationCrud,
26 };
27
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for DeleteComment {
30   type Response = CommentResponse;
31
32   #[tracing::instrument(skip(context, websocket_id))]
33   async fn perform(
34     &self,
35     context: &Data<LemmyContext>,
36     websocket_id: Option<ConnectionId>,
37   ) -> Result<CommentResponse, LemmyError> {
38     let data: &DeleteComment = self;
39     let local_user_view =
40       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
41
42     let comment_id = data.comment_id;
43     let orig_comment = blocking(context.pool(), move |conn| {
44       CommentView::read(conn, comment_id, None)
45     })
46     .await??;
47
48     // Dont delete it if its already been deleted.
49     if orig_comment.comment.deleted == data.deleted {
50       return Err(LemmyError::from_message("couldnt_update_comment"));
51     }
52
53     check_community_ban(
54       local_user_view.person.id,
55       orig_comment.community.id,
56       context.pool(),
57     )
58     .await?;
59
60     // Verify that only the creator can delete
61     if local_user_view.person.id != orig_comment.creator.id {
62       return Err(LemmyError::from_message("no_comment_edit_allowed"));
63     }
64
65     // Do the delete
66     let deleted = data.deleted;
67     let updated_comment = blocking(context.pool(), move |conn| {
68       Comment::update_deleted(conn, comment_id, deleted)
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 }
115
116 #[async_trait::async_trait(?Send)]
117 impl PerformCrud for RemoveComment {
118   type Response = CommentResponse;
119
120   #[tracing::instrument(skip(context, websocket_id))]
121   async fn perform(
122     &self,
123     context: &Data<LemmyContext>,
124     websocket_id: Option<ConnectionId>,
125   ) -> Result<CommentResponse, LemmyError> {
126     let data: &RemoveComment = self;
127     let local_user_view =
128       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
129
130     let comment_id = data.comment_id;
131     let orig_comment = blocking(context.pool(), move |conn| {
132       CommentView::read(conn, comment_id, None)
133     })
134     .await??;
135
136     check_community_ban(
137       local_user_view.person.id,
138       orig_comment.community.id,
139       context.pool(),
140     )
141     .await?;
142
143     // Verify that only a mod or admin can remove
144     is_mod_or_admin(
145       context.pool(),
146       local_user_view.person.id,
147       orig_comment.community.id,
148     )
149     .await?;
150
151     // Do the remove
152     let removed = data.removed;
153     let updated_comment = blocking(context.pool(), move |conn| {
154       Comment::update_removed(conn, comment_id, removed)
155     })
156     .await?
157     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
158
159     // Mod tables
160     let form = ModRemoveCommentForm {
161       mod_person_id: local_user_view.person.id,
162       comment_id: data.comment_id,
163       removed: Some(removed),
164       reason: data.reason.to_owned(),
165     };
166     blocking(context.pool(), move |conn| {
167       ModRemoveComment::create(conn, &form)
168     })
169     .await??;
170
171     let post_id = updated_comment.post_id;
172     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
173     let recipient_ids = send_local_notifs(
174       vec![],
175       &updated_comment,
176       &local_user_view.person.clone(),
177       &post,
178       false,
179       context,
180     )
181     .await?;
182
183     let res = send_comment_ws_message(
184       data.comment_id,
185       UserOperationCrud::RemoveComment,
186       websocket_id,
187       None, // TODO maybe this might clear other forms
188       Some(local_user_view.person.id),
189       recipient_ids,
190       context,
191     )
192     .await?;
193
194     // Send the apub message
195     let community = blocking(context.pool(), move |conn| {
196       Community::read(conn, orig_comment.post.community_id)
197     })
198     .await??;
199     let deletable = DeletableObjects::Comment(Box::new(updated_comment.clone().into()));
200     send_apub_delete_in_community(
201       local_user_view.person,
202       community,
203       deletable,
204       data.reason.clone().or_else(|| Some("".to_string())),
205       removed,
206       context,
207     )
208     .await?;
209
210     Ok(res)
211   }
212 }