]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/post.rs
Sanitize html (#3708)
[lemmy.git] / crates / api / src / site / purge / post.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   request::purge_image_from_pictrs,
6   site::{PurgeItemResponse, PurgePost},
7   utils::{is_admin, local_user_view_from_jwt, sanitize_html_opt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     moderator::{AdminPurgePost, AdminPurgePostForm},
12     post::Post,
13   },
14   traits::Crud,
15 };
16 use lemmy_utils::error::LemmyError;
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for PurgePost {
20   type Response = PurgeItemResponse;
21
22   #[tracing::instrument(skip(context))]
23   async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
24     let data: &Self = self;
25     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
26
27     // Only let admin purge an item
28     is_admin(&local_user_view)?;
29
30     let post_id = data.post_id;
31
32     // Read the post to get the community_id
33     let post = Post::read(&mut context.pool(), post_id).await?;
34
35     // Purge image
36     if let Some(url) = post.url {
37       purge_image_from_pictrs(context.client(), context.settings(), &url)
38         .await
39         .ok();
40     }
41     // Purge thumbnail
42     if let Some(thumbnail_url) = post.thumbnail_url {
43       purge_image_from_pictrs(context.client(), context.settings(), &thumbnail_url)
44         .await
45         .ok();
46     }
47
48     let community_id = post.community_id;
49
50     Post::delete(&mut context.pool(), post_id).await?;
51
52     // Mod tables
53     let reason = sanitize_html_opt(&data.reason);
54     let form = AdminPurgePostForm {
55       admin_person_id: local_user_view.person.id,
56       reason,
57       community_id,
58     };
59
60     AdminPurgePost::create(&mut context.pool(), &form).await?;
61
62     Ok(PurgeItemResponse { success: true })
63   }
64 }