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 @@ -46,16 +45,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
65 changes: 3 additions & 62 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,21 +695,6 @@ pub async fn purge_post_images(
}
}

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 {
purge_post_images(post.url, post.thumbnail_url, context).await;
}

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(local_user) = LocalUserView::read_person(&mut context.pool(), person_id).await {
Expand All @@ -727,21 +712,6 @@ 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 {
purge_post_images(post.url, post.thumbnail_url, context).await;
}

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

Ok(())
}

/// Removes or restores user data.
pub async fn remove_or_restore_user_data(
mod_person_id: PersonId,
Expand All @@ -752,16 +722,9 @@ pub async fn remove_or_restore_user_data(
) -> LemmyResult<()> {
let pool = &mut context.pool();

// Only these actions are possible when removing, not restoring
// These actions are only possible when removing, not restoring
if removed {
// Purge user images
let person = Person::read(pool, banned_person_id).await?;
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();
}
delete_local_user_images(banned_person_id, context).await?;

// Update the fields to None
Person::update(
Expand All @@ -776,9 +739,6 @@ pub async fn remove_or_restore_user_data(
)
.await?;

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

// Communities
// Remove all communities where they're the top mod
// for now, remove the communities manually
Expand All @@ -802,13 +762,6 @@ pub async fn remove_or_restore_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 @@ -959,21 +912,9 @@ pub async fn remove_or_restore_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?;

// 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
14 changes: 4 additions & 10 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ use lemmy_api_common::{
context::LemmyContext,
plugins::{plugin_hook_after, plugin_hook_before},
request::generate_post_link_metadata,
utils::{
check_nsfw_allowed,
get_url_blocklist,
process_markdown_opt,
purge_post_images,
slur_regex,
},
utils::{check_nsfw_allowed, get_url_blocklist, process_markdown_opt, slur_regex},
};
use lemmy_db_schema::{
source::{
Expand Down Expand Up @@ -241,9 +235,9 @@ impl Object for ApubPost {
// posts that get updated to be NSFW
let block_for_nsfw = check_nsfw_allowed(page.sensitive, local_site.as_ref());
if let Err(e) = block_for_nsfw {
let url = url.clone().map(std::convert::Into::into);
let thumbnail_url = page.image.map(|i| i.url.into());
purge_post_images(url, thumbnail_url, context).await;
// TODO: Remove locally generated thumbnail if one exists, depends on
// https://github.com/LemmyNet/lemmy/issues/5564 to be implemented to be able to
// safely do this.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll comment in the linked issue.

Post::delete_from_apub_id(&mut context.pool(), page.id.inner().clone()).await?;
Err(e)?
}
Expand Down
70 changes: 0 additions & 70 deletions crates/db_schema/src/impls/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use diesel::{
NullableExpressionMethods,
OptionalExtension,
QueryDsl,
TextExpressionMethods,
};
use diesel_async::RunQueryDsl;
use lemmy_utils::{
Expand Down Expand Up @@ -210,75 +209,6 @@ impl Post {
.await
}

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
}

pub async fn user_scheduled_post_count(
person_id: PersonId,
pool: &mut DbPool<'_>,
Expand Down