2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2022-06-13 19:15:04 +00:00
|
|
|
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-07-26 18:01:15 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt, purge_image_posts_for_community, sanitize_html_opt},
|
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
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn purge_community(
|
|
|
|
data: Json<PurgeCommunity>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<PurgeItemResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Only let admin purge an item
|
|
|
|
is_admin(&local_user_view)?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
let community_id = data.community_id;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Read the community to get its images
|
|
|
|
let community = Community::read(&mut context.pool(), community_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
if let Some(banner) = community.banner {
|
|
|
|
purge_image_from_pictrs(&banner, &context).await.ok();
|
|
|
|
}
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
if let Some(icon) = community.icon {
|
|
|
|
purge_image_from_pictrs(&icon, &context).await.ok();
|
|
|
|
}
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
purge_image_posts_for_community(community_id, &context).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
Community::delete(&mut context.pool(), community_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Mod tables
|
|
|
|
let reason = sanitize_html_opt(&data.reason);
|
|
|
|
let form = AdminPurgeCommunityForm {
|
|
|
|
admin_person_id: local_user_view.person.id,
|
|
|
|
reason,
|
|
|
|
};
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
AdminPurgeCommunity::create(&mut context.pool(), &form).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
Ok(Json(PurgeItemResponse { success: true }))
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|