]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/community.rs
5bdf62e75bb154187da9d9d646281bec1296aeeb
[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_top_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, ConnectionId};
17
18 #[async_trait::async_trait(?Send)]
19 impl Perform for PurgeCommunity {
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 = local_user_view_from_jwt(&data.auth, context).await?;
30
31     // Only let the top admin purge an item
32     is_top_admin(context.pool(), local_user_view.person.id).await?;
33
34     let community_id = data.community_id;
35
36     // Read the community to get its images
37     let community = Community::read(context.pool(), community_id).await?;
38
39     if let Some(banner) = community.banner {
40       purge_image_from_pictrs(context.client(), context.settings(), &banner)
41         .await
42         .ok();
43     }
44
45     if let Some(icon) = community.icon {
46       purge_image_from_pictrs(context.client(), context.settings(), &icon)
47         .await
48         .ok();
49     }
50
51     purge_image_posts_for_community(
52       community_id,
53       context.pool(),
54       context.settings(),
55       context.client(),
56     )
57     .await?;
58
59     Community::delete(context.pool(), community_id).await?;
60
61     // Mod tables
62     let reason = data.reason.clone();
63     let form = AdminPurgeCommunityForm {
64       admin_person_id: local_user_view.person.id,
65       reason,
66     };
67
68     AdminPurgeCommunity::create(context.pool(), &form).await?;
69
70     Ok(PurgeItemResponse { success: true })
71   }
72 }