Skip to content

Commit

Permalink
Delete a person's local images on delete account. (#4506)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dessalines committed Mar 27, 2024
1 parent 85ee89f commit a632a86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
19 changes: 3 additions & 16 deletions crates/api/src/site/purge/person.rs
Expand Up @@ -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},
},
Expand All @@ -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,
Expand All @@ -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(),
Expand Down
37 changes: 30 additions & 7 deletions 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};
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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?;

Expand Down

0 comments on commit a632a86

Please sign in to comment.