]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/post.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[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::{get_local_user_view_from_jwt, is_admin},
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, ConnectionId};
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for PurgePost {
20   type Response = PurgeItemResponse;
21
22   #[tracing::instrument(skip(context, _websocket_id))]
23   async fn perform(
24     &self,
25     context: &Data<LemmyContext>,
26     _websocket_id: Option<ConnectionId>,
27   ) -> Result<Self::Response, LemmyError> {
28     let data: &Self = self;
29     let local_user_view =
30       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
31
32     // Only let admins purge an item
33     is_admin(&local_user_view)?;
34
35     let post_id = data.post_id;
36
37     // Read the post to get the community_id
38     let post = Post::read(context.pool(), post_id).await?;
39
40     // Purge image
41     if let Some(url) = post.url {
42       purge_image_from_pictrs(context.client(), context.settings(), &url)
43         .await
44         .ok();
45     }
46     // Purge thumbnail
47     if let Some(thumbnail_url) = post.thumbnail_url {
48       purge_image_from_pictrs(context.client(), context.settings(), &thumbnail_url)
49         .await
50         .ok();
51     }
52
53     let community_id = post.community_id;
54
55     Post::delete(context.pool(), post_id).await?;
56
57     // Mod tables
58     let reason = data.reason.clone();
59     let form = AdminPurgePostForm {
60       admin_person_id: local_user_view.person.id,
61       reason,
62       community_id,
63     };
64
65     AdminPurgePost::create(context.pool(), &form).await?;
66
67     Ok(PurgeItemResponse { success: true })
68   }
69 }