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::{PurgeItemResponse, PurgePerson},
|
2023-07-26 18:01:15 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt, purge_image_posts_for_person, sanitize_html_opt},
|
2022-06-13 19:15:04 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
moderator::{AdminPurgePerson, AdminPurgePersonForm},
|
|
|
|
person::Person,
|
|
|
|
},
|
|
|
|
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 PurgePerson {
|
|
|
|
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
|
|
|
|
|
|
|
// Read the person to get their images
|
|
|
|
let person_id = data.person_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let person = Person::read(&mut context.pool(), person_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
if let Some(banner) = person.banner {
|
2023-08-28 10:23:45 +00:00
|
|
|
purge_image_from_pictrs(&banner, context).await.ok();
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(avatar) = person.avatar {
|
2023-08-28 10:23:45 +00:00
|
|
|
purge_image_from_pictrs(&avatar, context).await.ok();
|
2022-06-13 19:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 10:23:45 +00:00
|
|
|
purge_image_posts_for_person(person_id, context).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
Person::delete(&mut context.pool(), person_id).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
// Mod tables
|
2023-07-26 18:01:15 +00:00
|
|
|
let reason = sanitize_html_opt(&data.reason);
|
2022-06-13 19:15:04 +00:00
|
|
|
let form = AdminPurgePersonForm {
|
|
|
|
admin_person_id: local_user_view.person.id,
|
|
|
|
reason,
|
|
|
|
};
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
AdminPurgePerson::create(&mut context.pool(), &form).await?;
|
2022-06-13 19:15:04 +00:00
|
|
|
|
|
|
|
Ok(PurgeItemResponse { success: true })
|
|
|
|
}
|
|
|
|
}
|