]> Untitled Git - lemmy.git/blob - crates/api/src/site/purge/community.rs
Remove update and read site config. Fixes #2306 (#2329)
[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   request::purge_image_from_pictrs,
5   site::{PurgeCommunity, PurgeItemResponse},
6   utils::{blocking, get_local_user_view_from_jwt, is_admin, purge_image_posts_for_community},
7 };
8 use lemmy_db_schema::{
9   source::{
10     community::Community,
11     moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm},
12   },
13   traits::Crud,
14 };
15 use lemmy_utils::{error::LemmyError, ConnectionId};
16 use lemmy_websocket::LemmyContext;
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 =
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 community_id = data.community_id;
36
37     // Read the community to get its images
38     let community = blocking(context.pool(), move |conn| {
39       Community::read(conn, community_id)
40     })
41     .await??;
42
43     if let Some(banner) = community.banner {
44       purge_image_from_pictrs(context.client(), context.settings(), &banner)
45         .await
46         .ok();
47     }
48
49     if let Some(icon) = community.icon {
50       purge_image_from_pictrs(context.client(), context.settings(), &icon)
51         .await
52         .ok();
53     }
54
55     purge_image_posts_for_community(
56       community_id,
57       context.pool(),
58       context.settings(),
59       context.client(),
60     )
61     .await?;
62
63     blocking(context.pool(), move |conn| {
64       Community::delete(conn, community_id)
65     })
66     .await??;
67
68     // Mod tables
69     let reason = data.reason.to_owned();
70     let form = AdminPurgeCommunityForm {
71       admin_person_id: local_user_view.person.id,
72       reason,
73     };
74
75     blocking(context.pool(), move |conn| {
76       AdminPurgeCommunity::create(conn, &form)
77     })
78     .await??;
79
80     Ok(PurgeItemResponse { success: true })
81   }
82 }