]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/comment.rs
Adding admin purging of DB items and pictures. #904 #1331 (#1809)
[lemmy.git] / crates / api / src / site / purge / comment.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   site::{PurgeComment, PurgeItemResponse},
5   utils::{blocking, get_local_user_view_from_jwt, is_admin},
6 };
7 use lemmy_db_schema::{
8   source::{
9     comment::Comment,
10     moderator::{AdminPurgeComment, AdminPurgeCommentForm},
11   },
12   traits::Crud,
13 };
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::LemmyContext;
16
17 #[async_trait::async_trait(?Send)]
18 impl Perform for PurgeComment {
19   type Response = PurgeItemResponse;
20
21   #[tracing::instrument(skip(context, _websocket_id))]
22   async fn perform(
23     &self,
24     context: &Data<LemmyContext>,
25     _websocket_id: Option<ConnectionId>,
26   ) -> Result<Self::Response, LemmyError> {
27     let data: &Self = self;
28     let local_user_view =
29       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
30
31     // Only let admins purge an item
32     is_admin(&local_user_view)?;
33
34     let comment_id = data.comment_id;
35
36     // Read the comment to get the post_id
37     let comment = blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
38
39     let post_id = comment.post_id;
40
41     // TODO read comments for pictrs images and purge them
42
43     blocking(context.pool(), move |conn| {
44       Comment::delete(conn, comment_id)
45     })
46     .await??;
47
48     // Mod tables
49     let reason = data.reason.to_owned();
50     let form = AdminPurgeCommentForm {
51       admin_person_id: local_user_view.person.id,
52       reason,
53       post_id,
54     };
55
56     blocking(context.pool(), move |conn| {
57       AdminPurgeComment::create(conn, &form)
58     })
59     .await??;
60
61     Ok(PurgeItemResponse { success: true })
62   }
63 }