]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/comment.rs
Dont return error in case optional auth is invalid (#2879)
[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   context::LemmyContext,
5   site::{PurgeComment, PurgeItemResponse},
6   utils::{is_top_admin, local_user_view_from_jwt},
7 };
8 use lemmy_db_schema::{
9   source::{
10     comment::Comment,
11     moderator::{AdminPurgeComment, AdminPurgeCommentForm},
12   },
13   traits::Crud,
14 };
15 use lemmy_utils::{error::LemmyError, ConnectionId};
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 = local_user_view_from_jwt(&data.auth, context).await?;
29
30     // Only let the top admin purge an item
31     is_top_admin(context.pool(), local_user_view.person.id).await?;
32
33     let comment_id = data.comment_id;
34
35     // Read the comment to get the post_id
36     let comment = Comment::read(context.pool(), comment_id).await?;
37
38     let post_id = comment.post_id;
39
40     // TODO read comments for pictrs images and purge them
41
42     Comment::delete(context.pool(), comment_id).await?;
43
44     // Mod tables
45     let reason = data.reason.clone();
46     let form = AdminPurgeCommentForm {
47       admin_person_id: local_user_view.person.id,
48       reason,
49       post_id,
50     };
51
52     AdminPurgeComment::create(context.pool(), &form).await?;
53
54     Ok(PurgeItemResponse { success: true })
55   }
56 }