From a632a86852292f6828bae5ddb43f3ce5335c9b29 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 27 Mar 2024 10:28:02 -0400 Subject: [PATCH] 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 --- crates/api/src/site/purge/person.rs | 19 +++------------ crates/api_common/src/utils.rs | 37 +++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/crates/api/src/site/purge/person.rs b/crates/api/src/site/purge/person.rs index 658846eab..6023d7b41 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -3,15 +3,13 @@ use activitypub_federation::config::Data; use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, - request::delete_image_from_pictrs, send_activity::{ActivityChannel, SendActivityData}, site::PurgePerson, - utils::is_admin, + utils::{is_admin, purge_user_account}, SuccessResponse, }; use lemmy_db_schema::{ source::{ - images::LocalImage, moderator::{AdminPurgePerson, AdminPurgePersonForm}, person::{Person, PersonUpdateForm}, }, @@ -29,18 +27,6 @@ pub async fn purge_person( // Only let admin purge an item 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?; ban_nonlocal_user_from_local_communities( &local_user_view, @@ -54,7 +40,8 @@ pub async fn purge_person( .await?; // 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. let person = Person::update( &mut context.pool(), diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index e4ab91b08..73333c3a0 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -1,6 +1,6 @@ use crate::{ context::LemmyContext, - request::purge_image_from_pictrs, + request::{delete_image_from_pictrs, purge_image_from_pictrs}, site::{FederatedInstances, InstanceWithFederationState}, }; use chrono::{DateTime, Days, Local, TimeZone, Utc}; @@ -12,7 +12,7 @@ use lemmy_db_schema::{ community::{Community, CommunityModerator, CommunityUpdateForm}, community_block::CommunityBlock, email_verification::{EmailVerification, EmailVerificationForm}, - images::RemoteImage, + images::{LocalImage, RemoteImage}, instance::Instance, instance_block::InstanceBlock, local_site::LocalSite, @@ -663,6 +663,25 @@ pub async fn purge_image_posts_for_person( 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( banned_community_id: CommunityId, context: &LemmyContext, @@ -804,15 +823,22 @@ pub async fn purge_user_account( context: &LemmyContext, ) -> Result<(), LemmyError> { let pool = &mut context.pool(); - // Delete their images + 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 { purge_image_from_pictrs(&avatar, context).await.ok(); } if let Some(banner) = person.banner { 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 Comment::permadelete_for_creator(pool, person_id) @@ -824,9 +850,6 @@ pub async fn purge_user_account( .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePost)?; - // Purge image posts - purge_image_posts_for_person(person_id, context).await?; - // Leave communities they mod CommunityModerator::leave_all_communities(pool, person_id).await?;