]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/community.rs
56e757176b5bb65c8bfdf1b86b4897ef801dfce0
[lemmy.git] / crates / api / src / site / purge / community.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::{PurgeCommunity, PurgeItemResponse},
7   utils::{is_admin, local_user_view_from_jwt, purge_image_posts_for_community},
8 };
9 use lemmy_db_schema::{
10   source::{
11     community::Community,
12     moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm},
13   },
14   traits::Crud,
15 };
16 use lemmy_utils::error::LemmyError;
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for PurgeCommunity {
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 community_id = data.community_id;
31
32     // Read the community to get its images
33     let community = Community::read(&mut context.pool(), community_id).await?;
34
35     if let Some(banner) = community.banner {
36       purge_image_from_pictrs(context.client(), context.settings(), &banner)
37         .await
38         .ok();
39     }
40
41     if let Some(icon) = community.icon {
42       purge_image_from_pictrs(context.client(), context.settings(), &icon)
43         .await
44         .ok();
45     }
46
47     purge_image_posts_for_community(
48       community_id,
49       &mut context.pool(),
50       context.settings(),
51       context.client(),
52     )
53     .await?;
54
55     Community::delete(&mut context.pool(), community_id).await?;
56
57     // Mod tables
58     let reason = data.reason.clone();
59     let form = AdminPurgeCommunityForm {
60       admin_person_id: local_user_view.person.id,
61       reason,
62     };
63
64     AdminPurgeCommunity::create(&mut context.pool(), &form).await?;
65
66     Ok(PurgeItemResponse { success: true })
67   }
68 }