]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/comment.rs
Sanitize html (#3708)
[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_admin, local_user_view_from_jwt, sanitize_html_opt},
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;
16
17 #[async_trait::async_trait(?Send)]
18 impl Perform for PurgeComment {
19   type Response = PurgeItemResponse;
20
21   #[tracing::instrument(skip(context))]
22   async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
23     let data: &Self = self;
24     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
25
26     // Only let admin purge an item
27     is_admin(&local_user_view)?;
28
29     let comment_id = data.comment_id;
30
31     // Read the comment to get the post_id
32     let comment = Comment::read(&mut context.pool(), comment_id).await?;
33
34     let post_id = comment.post_id;
35
36     // TODO read comments for pictrs images and purge them
37
38     Comment::delete(&mut context.pool(), comment_id).await?;
39
40     // Mod tables
41     let reason = sanitize_html_opt(&data.reason);
42     let form = AdminPurgeCommentForm {
43       admin_person_id: local_user_view.person.id,
44       reason,
45       post_id,
46     };
47
48     AdminPurgeComment::create(&mut context.pool(), &form).await?;
49
50     Ok(PurgeItemResponse { success: true })
51   }
52 }