Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions crates/api/src/site/purge/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
context::LemmyContext,
request::purge_image_from_pictrs,
send_activity::{ActivityChannel, SendActivityData},
site::PurgeCommunity,
utils::{is_admin, purge_image_posts_for_community},
utils::is_admin,
SuccessResponse,
};
use lemmy_db_schema::{
Expand Down Expand Up @@ -50,16 +49,6 @@ pub async fn purge_community(
)
.await?;

if let Some(banner) = &community.banner {
purge_image_from_pictrs(banner, &context).await.ok();
}

if let Some(icon) = &community.icon {
purge_image_from_pictrs(icon, &context).await.ok();
}

purge_image_posts_for_community(data.community_id, &context).await?;

Community::delete(&mut context.pool(), data.community_id).await?;

// Mod tables
Expand Down
82 changes: 3 additions & 79 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::{
context::LemmyContext,
request::{
delete_image_from_pictrs,
fetch_pictrs_proxied_image_details,
purge_image_from_pictrs,
},
request::{delete_image_from_pictrs, fetch_pictrs_proxied_image_details},
site::{FederatedInstances, InstanceWithFederationState},
};
use chrono::{DateTime, Days, Local, TimeZone, Utc};
Expand Down Expand Up @@ -628,26 +624,6 @@ pub async fn read_site_for_actor(
Ok(site)
}

pub async fn purge_image_posts_for_person(
banned_person_id: PersonId,
context: &LemmyContext,
) -> LemmyResult<()> {
let pool = &mut context.pool();
let posts = Post::fetch_pictrs_posts_for_creator(pool, banned_person_id).await?;
for post in posts {
if let Some(url) = post.url {
purge_image_from_pictrs(&url, context).await.ok();
}
if let Some(thumbnail_url) = post.thumbnail_url {
purge_image_from_pictrs(&thumbnail_url, context).await.ok();
}
}

Post::remove_pictrs_post_images_and_thumbnails_for_creator(pool, banned_person_id).await?;

Ok(())
}

/// Delete a local_user's images
async fn delete_local_user_images(person_id: PersonId, context: &LemmyContext) -> LemmyResult<()> {
if let Ok(Some(local_user)) = LocalUserView::read_person(&mut context.pool(), person_id).await {
Expand All @@ -669,41 +645,11 @@ async fn delete_local_user_images(person_id: PersonId, context: &LemmyContext) -
Ok(())
}

pub async fn purge_image_posts_for_community(
banned_community_id: CommunityId,
context: &LemmyContext,
) -> LemmyResult<()> {
let pool = &mut context.pool();
let posts = Post::fetch_pictrs_posts_for_community(pool, banned_community_id).await?;
for post in posts {
if let Some(url) = post.url {
purge_image_from_pictrs(&url, context).await.ok();
}
if let Some(thumbnail_url) = post.thumbnail_url {
purge_image_from_pictrs(&thumbnail_url, context).await.ok();
}
}

Post::remove_pictrs_post_images_and_thumbnails_for_community(pool, banned_community_id).await?;

Ok(())
}

pub async fn remove_user_data(
banned_person_id: PersonId,
context: &LemmyContext,
) -> LemmyResult<()> {
let pool = &mut context.pool();
// Purge user images
let person = Person::read(pool, banned_person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?;
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();
}

// Update the fields to None
Person::update(
Expand All @@ -721,8 +667,7 @@ pub async fn remove_user_data(
// Posts
Post::update_removed_for_creator(pool, banned_person_id, None, true).await?;

// Purge image posts
purge_image_posts_for_person(banned_person_id, context).await?;
delete_local_user_images(banned_person_id, context).await?;

// Communities
// Remove all communities where they're the top mod
Expand All @@ -747,13 +692,6 @@ pub async fn remove_user_data(
)
.await?;

// Delete the community images
if let Some(icon) = first_mod_community.community.icon {
purge_image_from_pictrs(&icon, context).await.ok();
}
if let Some(banner) = first_mod_community.community.banner {
purge_image_from_pictrs(&banner, context).await.ok();
}
// Update the fields to None
Community::update(
pool,
Expand Down Expand Up @@ -813,23 +751,9 @@ pub async fn remove_user_data_in_community(
pub async fn purge_user_account(person_id: PersonId, context: &LemmyContext) -> LemmyResult<()> {
let pool = &mut context.pool();

let person = Person::read(pool, person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?;

// 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();
}

// Purge image posts
purge_image_posts_for_person(person_id, context).await.ok();
delete_local_user_images(person_id, context).await.ok();

// Comments
Comment::permadelete_for_creator(pool, person_id)
Expand Down
78 changes: 1 addition & 77 deletions crates/db_schema/src/impls/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ use crate::{
};
use ::url::Url;
use chrono::{DateTime, Utc};
use diesel::{
dsl::insert_into,
result::Error,
DecoratableTarget,
ExpressionMethods,
QueryDsl,
TextExpressionMethods,
};
use diesel::{dsl::insert_into, result::Error, DecoratableTarget, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use std::collections::HashSet;

Expand Down Expand Up @@ -173,75 +166,6 @@ impl Post {
.await
.optional()
}

pub async fn fetch_pictrs_posts_for_creator(
pool: &mut DbPool<'_>,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let pictrs_search = "%pictrs/image%";

post::table
.filter(post::creator_id.eq(for_creator_id))
.filter(post::url.like(pictrs_search))
.load::<Self>(conn)
.await
}

/// Sets the url and thumbnails fields to None
pub async fn remove_pictrs_post_images_and_thumbnails_for_creator(
pool: &mut DbPool<'_>,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let pictrs_search = "%pictrs/image%";

diesel::update(
post::table
.filter(post::creator_id.eq(for_creator_id))
.filter(post::url.like(pictrs_search)),
)
.set((
post::url.eq::<Option<String>>(None),
post::thumbnail_url.eq::<Option<String>>(None),
))
.get_results::<Self>(conn)
.await
}

pub async fn fetch_pictrs_posts_for_community(
pool: &mut DbPool<'_>,
for_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let pictrs_search = "%pictrs/image%";
post::table
.filter(post::community_id.eq(for_community_id))
.filter(post::url.like(pictrs_search))
.load::<Self>(conn)
.await
}

/// Sets the url and thumbnails fields to None
pub async fn remove_pictrs_post_images_and_thumbnails_for_community(
pool: &mut DbPool<'_>,
for_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let pictrs_search = "%pictrs/image%";

diesel::update(
post::table
.filter(post::community_id.eq(for_community_id))
.filter(post::url.like(pictrs_search)),
)
.set((
post::url.eq::<Option<String>>(None),
post::thumbnail_url.eq::<Option<String>>(None),
))
.get_results::<Self>(conn)
.await
}
}

#[async_trait]
Expand Down