Delete a person's local images on delete account. (#4506)

* Delete a person's local images on delete account.

* Rename purge function to delete.

* Use purge_user_account instead of Person::delete_account in purge person.

* Fixing clippy
This commit is contained in:
Dessalines 2024-03-27 10:28:02 -04:00 committed by GitHub
parent 85ee89f4e8
commit a632a86852
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 23 deletions

View file

@ -3,15 +3,13 @@ use activitypub_federation::config::Data;
use actix_web::web::Json; use actix_web::web::Json;
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
request::delete_image_from_pictrs,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
site::PurgePerson, site::PurgePerson,
utils::is_admin, utils::{is_admin, purge_user_account},
SuccessResponse, SuccessResponse,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
images::LocalImage,
moderator::{AdminPurgePerson, AdminPurgePersonForm}, moderator::{AdminPurgePerson, AdminPurgePersonForm},
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
}, },
@ -29,18 +27,6 @@ pub async fn purge_person(
// Only let admin purge an item // Only let admin purge an item
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
// Read the local user to get their images, and delete them
if let Ok(local_user) = LocalUserView::read_person(&mut context.pool(), data.person_id).await {
let pictrs_uploads =
LocalImage::get_all_by_local_user_id(&mut context.pool(), local_user.local_user.id).await?;
for upload in pictrs_uploads {
delete_image_from_pictrs(&upload.pictrs_alias, &upload.pictrs_delete_token, &context)
.await
.ok();
}
}
let person = Person::read(&mut context.pool(), data.person_id).await?; let person = Person::read(&mut context.pool(), data.person_id).await?;
ban_nonlocal_user_from_local_communities( ban_nonlocal_user_from_local_communities(
&local_user_view, &local_user_view,
@ -54,7 +40,8 @@ pub async fn purge_person(
.await?; .await?;
// Clear profile data. // Clear profile data.
Person::delete_account(&mut context.pool(), data.person_id).await?; purge_user_account(data.person_id, &context).await?;
// Keep person record, but mark as banned to prevent login or refetching from home instance. // Keep person record, but mark as banned to prevent login or refetching from home instance.
let person = Person::update( let person = Person::update(
&mut context.pool(), &mut context.pool(),

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
context::LemmyContext, context::LemmyContext,
request::purge_image_from_pictrs, request::{delete_image_from_pictrs, purge_image_from_pictrs},
site::{FederatedInstances, InstanceWithFederationState}, site::{FederatedInstances, InstanceWithFederationState},
}; };
use chrono::{DateTime, Days, Local, TimeZone, Utc}; use chrono::{DateTime, Days, Local, TimeZone, Utc};
@ -12,7 +12,7 @@ use lemmy_db_schema::{
community::{Community, CommunityModerator, CommunityUpdateForm}, community::{Community, CommunityModerator, CommunityUpdateForm},
community_block::CommunityBlock, community_block::CommunityBlock,
email_verification::{EmailVerification, EmailVerificationForm}, email_verification::{EmailVerification, EmailVerificationForm},
images::RemoteImage, images::{LocalImage, RemoteImage},
instance::Instance, instance::Instance,
instance_block::InstanceBlock, instance_block::InstanceBlock,
local_site::LocalSite, local_site::LocalSite,
@ -663,6 +663,25 @@ pub async fn purge_image_posts_for_person(
Ok(()) Ok(())
} }
/// Delete a local_user's images
async fn delete_local_user_images(
person_id: PersonId,
context: &LemmyContext,
) -> Result<(), LemmyError> {
if let Ok(local_user) = LocalUserView::read_person(&mut context.pool(), person_id).await {
let pictrs_uploads =
LocalImage::get_all_by_local_user_id(&mut context.pool(), local_user.local_user.id).await?;
// Delete their images
for upload in pictrs_uploads {
delete_image_from_pictrs(&upload.pictrs_alias, &upload.pictrs_delete_token, context)
.await
.ok();
}
}
Ok(())
}
pub async fn purge_image_posts_for_community( pub async fn purge_image_posts_for_community(
banned_community_id: CommunityId, banned_community_id: CommunityId,
context: &LemmyContext, context: &LemmyContext,
@ -804,15 +823,22 @@ pub async fn purge_user_account(
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let pool = &mut context.pool(); let pool = &mut context.pool();
// Delete their images
let person = Person::read(pool, person_id).await?; let person = Person::read(pool, person_id).await?;
// Delete their local images, if they're a local user
delete_local_user_images(person_id, context).await.ok();
// No need to update avatar and banner, those are handled in Person::delete_account
if let Some(avatar) = person.avatar { if let Some(avatar) = person.avatar {
purge_image_from_pictrs(&avatar, context).await.ok(); purge_image_from_pictrs(&avatar, context).await.ok();
} }
if let Some(banner) = person.banner { if let Some(banner) = person.banner {
purge_image_from_pictrs(&banner, context).await.ok(); purge_image_from_pictrs(&banner, context).await.ok();
} }
// No need to update avatar and banner, those are handled in Person::delete_account
// Purge image posts
purge_image_posts_for_person(person_id, context).await.ok();
// Comments // Comments
Comment::permadelete_for_creator(pool, person_id) Comment::permadelete_for_creator(pool, person_id)
@ -824,9 +850,6 @@ pub async fn purge_user_account(
.await .await
.with_lemmy_type(LemmyErrorType::CouldntUpdatePost)?; .with_lemmy_type(LemmyErrorType::CouldntUpdatePost)?;
// Purge image posts
purge_image_posts_for_person(person_id, context).await?;
// Leave communities they mod // Leave communities they mod
CommunityModerator::leave_all_communities(pool, person_id).await?; CommunityModerator::leave_all_communities(pool, person_id).await?;