]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/delete.rs
19851f0c0556a093f1d50ab0739182301fca244c
[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, send_apub_remove};
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::{ApiError, 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   async fn perform(
33     &self,
34     context: &Data<LemmyContext>,
35     websocket_id: Option<ConnectionId>,
36   ) -> Result<CommentResponse, LemmyError> {
37     let data: &DeleteComment = self;
38     let local_user_view =
39       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
40
41     let comment_id = data.comment_id;
42     let orig_comment = blocking(context.pool(), move |conn| {
43       CommentView::read(conn, comment_id, None)
44     })
45     .await??;
46
47     check_community_ban(
48       local_user_view.person.id,
49       orig_comment.community.id,
50       context.pool(),
51     )
52     .await?;
53
54     // Verify that only the creator can delete
55     if local_user_view.person.id != orig_comment.creator.id {
56       return Err(ApiError::err_plain("no_comment_edit_allowed").into());
57     }
58
59     // Do the delete
60     let deleted = data.deleted;
61     let updated_comment = blocking(context.pool(), move |conn| {
62       Comment::update_deleted(conn, comment_id, deleted)
63     })
64     .await?
65     .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
66
67     // Send the apub message
68     let community = blocking(context.pool(), move |conn| {
69       Community::read(conn, orig_comment.post.community_id)
70     })
71     .await??;
72     send_apub_delete(
73       &local_user_view.person.clone().into(),
74       &community.clone().into(),
75       updated_comment.ap_id.clone().into(),
76       deleted,
77       context,
78     )
79     .await?;
80
81     let post_id = updated_comment.post_id;
82     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
83     let recipient_ids = send_local_notifs(
84       vec![],
85       &updated_comment,
86       &local_user_view.person,
87       &post,
88       false,
89       context,
90     )
91     .await?;
92
93     send_comment_ws_message(
94       data.comment_id,
95       UserOperationCrud::DeleteComment,
96       websocket_id,
97       None, // TODO a comment delete might clear forms?
98       Some(local_user_view.person.id),
99       recipient_ids,
100       context,
101     )
102     .await
103   }
104 }
105
106 #[async_trait::async_trait(?Send)]
107 impl PerformCrud for RemoveComment {
108   type Response = CommentResponse;
109
110   async fn perform(
111     &self,
112     context: &Data<LemmyContext>,
113     websocket_id: Option<ConnectionId>,
114   ) -> Result<CommentResponse, LemmyError> {
115     let data: &RemoveComment = self;
116     let local_user_view =
117       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
118
119     let comment_id = data.comment_id;
120     let orig_comment = blocking(context.pool(), move |conn| {
121       CommentView::read(conn, comment_id, None)
122     })
123     .await??;
124
125     check_community_ban(
126       local_user_view.person.id,
127       orig_comment.community.id,
128       context.pool(),
129     )
130     .await?;
131
132     // Verify that only a mod or admin can remove
133     is_mod_or_admin(
134       context.pool(),
135       local_user_view.person.id,
136       orig_comment.community.id,
137     )
138     .await?;
139
140     // Do the remove
141     let removed = data.removed;
142     let updated_comment = blocking(context.pool(), move |conn| {
143       Comment::update_removed(conn, comment_id, removed)
144     })
145     .await?
146     .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
147
148     // Mod tables
149     let form = ModRemoveCommentForm {
150       mod_person_id: local_user_view.person.id,
151       comment_id: data.comment_id,
152       removed: Some(removed),
153       reason: data.reason.to_owned(),
154     };
155     blocking(context.pool(), move |conn| {
156       ModRemoveComment::create(conn, &form)
157     })
158     .await??;
159
160     // Send the apub message
161     let community = blocking(context.pool(), move |conn| {
162       Community::read(conn, orig_comment.post.community_id)
163     })
164     .await??;
165     send_apub_remove(
166       &local_user_view.person.clone().into(),
167       &community.into(),
168       updated_comment.ap_id.clone().into(),
169       data.reason.clone().unwrap_or_else(|| "".to_string()),
170       removed,
171       context,
172     )
173     .await?;
174
175     let post_id = updated_comment.post_id;
176     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
177     let recipient_ids = send_local_notifs(
178       vec![],
179       &updated_comment,
180       &local_user_view.person.clone(),
181       &post,
182       false,
183       context,
184     )
185     .await?;
186
187     send_comment_ws_message(
188       data.comment_id,
189       UserOperationCrud::RemoveComment,
190       websocket_id,
191       None, // TODO maybe this might clear other forms
192       Some(local_user_view.person.id),
193       recipient_ids,
194       context,
195     )
196     .await
197   }
198 }