2022-06-13 19:15:04 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-06-13 19:15:04 +00:00
|
|
|
request::purge_image_from_pictrs,
|
|
|
|
site::{PurgeCommunity, PurgeItemResponse},
|
2023-06-26 08:47:39 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt, purge_image_posts_for_community},
|
2022-06-13 19:15:04 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::Community,
|
|
|
|
moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for PurgeCommunity {
|
|
|
|
type Response = PurgeItemResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
|
2022-06-13 19:15:04 +00:00
|
|
|
let data: &Self = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-06-26 08:47:39 +00:00
|
|
|
// Only let admin purge an item
|
|
|
|
is_admin(&local_user_view)?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
let community_id = data.community_id;
|
|
|
|
|
|
|
|
// Read the community to get its images
|
2023-07-11 13:09:59 +00:00
|
|
|
let community = Community::read(&mut context.pool(), community_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
if let Some(banner) = community.banner {
|
2022-06-22 20:24:54 +00:00
|
|
|
purge_image_from_pictrs(context.client(), context.settings(), &banner)
|
2022-06-13 19:15:04 +00:00
|
|
|
.await
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(icon) = community.icon {
|
2022-06-22 20:24:54 +00:00
|
|
|
purge_image_from_pictrs(context.client(), context.settings(), &icon)
|
2022-06-13 19:15:04 +00:00
|
|
|
.await
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
purge_image_posts_for_community(
|
|
|
|
community_id,
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-06-22 20:24:54 +00:00
|
|
|
context.settings(),
|
2022-06-13 19:15:04 +00:00
|
|
|
context.client(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::delete(&mut context.pool(), community_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
// Mod tables
|
2022-11-19 04:33:54 +00:00
|
|
|
let reason = data.reason.clone();
|
2022-06-13 19:15:04 +00:00
|
|
|
let form = AdminPurgeCommunityForm {
|
|
|
|
admin_person_id: local_user_view.person.id,
|
|
|
|
reason,
|
|
|
|
};
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
AdminPurgeCommunity::create(&mut context.pool(), &form).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
Ok(PurgeItemResponse { success: true })
|
|
|
|
}
|
|
|
|
}
|