mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-10-01 01:36:12 -04:00
Combining remove and restore functions.
This commit is contained in:
parent
b3639924f3
commit
7c43f4e86f
@ -5,7 +5,7 @@ use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
person::{BanPerson, BanPersonResponse},
|
||||
send_activity::{ActivityChannel, SendActivityData},
|
||||
utils::{check_expire_time, is_admin, remove_user_data, restore_user_data},
|
||||
utils::{check_expire_time, is_admin, remove_or_restore_user_data},
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
@ -66,11 +66,15 @@ pub async fn ban_from_site(
|
||||
|
||||
// Remove their data if that's desired
|
||||
if data.remove_or_restore_data.unwrap_or(false) {
|
||||
if data.ban {
|
||||
remove_user_data(local_user_view.person.id, person.id, &data.reason, &context).await?;
|
||||
} else {
|
||||
restore_user_data(local_user_view.person.id, person.id, &data.reason, &context).await?;
|
||||
}
|
||||
let removed = data.ban;
|
||||
remove_or_restore_user_data(
|
||||
local_user_view.person.id,
|
||||
person.id,
|
||||
removed,
|
||||
&data.reason,
|
||||
&context,
|
||||
)
|
||||
.await?;
|
||||
};
|
||||
|
||||
// Mod tables
|
||||
|
@ -668,139 +668,106 @@ pub async fn purge_image_posts_for_community(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn remove_user_data(
|
||||
/// Removes or restores user data.
|
||||
pub async fn remove_or_restore_user_data(
|
||||
mod_person_id: PersonId,
|
||||
banned_person_id: PersonId,
|
||||
removed: bool,
|
||||
reason: &Option<String>,
|
||||
context: &LemmyContext,
|
||||
) -> LemmyResult<()> {
|
||||
let pool = &mut context.pool();
|
||||
let removed = true;
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
// Update the fields to None
|
||||
Person::update(
|
||||
pool,
|
||||
banned_person_id,
|
||||
&PersonUpdateForm {
|
||||
avatar: Some(None),
|
||||
banner: Some(None),
|
||||
bio: Some(None),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Posts
|
||||
let removed_posts =
|
||||
Post::update_removed_for_creator(pool, banned_person_id, None, removed).await?;
|
||||
create_modlog_entries_for_removed_or_restored_posts(
|
||||
pool,
|
||||
mod_person_id,
|
||||
removed_posts.iter().map(|r| r.id).collect(),
|
||||
removed,
|
||||
reason,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Purge image posts
|
||||
purge_image_posts_for_person(banned_person_id, context).await?;
|
||||
|
||||
// Comments
|
||||
let removed_comments =
|
||||
Comment::update_removed_for_creator(pool, banned_person_id, removed).await?;
|
||||
create_modlog_entries_for_removed_or_restored_comments(
|
||||
pool,
|
||||
mod_person_id,
|
||||
removed_comments.iter().map(|r| r.id).collect(),
|
||||
removed,
|
||||
reason,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Communities
|
||||
// Remove all communities where they're the top mod
|
||||
// for now, remove the communities manually
|
||||
let first_mod_communities = CommunityModeratorView::get_community_first_mods(pool).await?;
|
||||
|
||||
// Filter to only this banned users top communities
|
||||
let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
|
||||
.into_iter()
|
||||
.filter(|fmc| fmc.moderator.id == banned_person_id)
|
||||
.collect();
|
||||
|
||||
for first_mod_community in banned_user_first_communities {
|
||||
let community_id = first_mod_community.community.id;
|
||||
Community::update(
|
||||
pool,
|
||||
community_id,
|
||||
&CommunityUpdateForm {
|
||||
removed: Some(removed),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Delete the community images
|
||||
if let Some(icon) = first_mod_community.community.icon {
|
||||
purge_image_from_pictrs(&icon, context).await.ok();
|
||||
// Only these actions are 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) = first_mod_community.community.banner {
|
||||
if let Some(banner) = person.banner {
|
||||
purge_image_from_pictrs(&banner, context).await.ok();
|
||||
}
|
||||
|
||||
// Update the fields to None
|
||||
Community::update(
|
||||
Person::update(
|
||||
pool,
|
||||
community_id,
|
||||
&CommunityUpdateForm {
|
||||
icon: Some(None),
|
||||
banned_person_id,
|
||||
&PersonUpdateForm {
|
||||
avatar: Some(None),
|
||||
banner: Some(None),
|
||||
bio: Some(None),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.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
|
||||
let first_mod_communities = CommunityModeratorView::get_community_first_mods(pool).await?;
|
||||
|
||||
// Filter to only this banned users top communities
|
||||
let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
|
||||
.into_iter()
|
||||
.filter(|fmc| fmc.moderator.id == banned_person_id)
|
||||
.collect();
|
||||
|
||||
for first_mod_community in banned_user_first_communities {
|
||||
let community_id = first_mod_community.community.id;
|
||||
Community::update(
|
||||
pool,
|
||||
community_id,
|
||||
&CommunityUpdateForm {
|
||||
removed: Some(removed),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.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,
|
||||
community_id,
|
||||
&CommunityUpdateForm {
|
||||
icon: Some(None),
|
||||
banner: Some(None),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// We can't restore their images, but we can unremove their posts and comments
|
||||
pub async fn restore_user_data(
|
||||
mod_person_id: PersonId,
|
||||
banned_person_id: PersonId,
|
||||
reason: &Option<String>,
|
||||
context: &LemmyContext,
|
||||
) -> LemmyResult<()> {
|
||||
let pool = &mut context.pool();
|
||||
let removed = false;
|
||||
|
||||
// Posts
|
||||
let restored_posts =
|
||||
let removed_or_restored_posts =
|
||||
Post::update_removed_for_creator(pool, banned_person_id, None, removed).await?;
|
||||
create_modlog_entries_for_removed_or_restored_posts(
|
||||
pool,
|
||||
mod_person_id,
|
||||
restored_posts.iter().map(|r| r.id).collect(),
|
||||
removed_or_restored_posts.iter().map(|r| r.id).collect(),
|
||||
removed,
|
||||
reason,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Comments
|
||||
let restored_comments =
|
||||
let removed_or_restored_comments =
|
||||
Comment::update_removed_for_creator(pool, banned_person_id, removed).await?;
|
||||
create_modlog_entries_for_removed_or_restored_comments(
|
||||
pool,
|
||||
mod_person_id,
|
||||
restored_comments.iter().map(|r| r.id).collect(),
|
||||
removed_or_restored_comments.iter().map(|r| r.id).collect(),
|
||||
removed,
|
||||
reason,
|
||||
)
|
||||
@ -813,7 +780,7 @@ async fn create_modlog_entries_for_removed_or_restored_posts(
|
||||
pool: &mut DbPool<'_>,
|
||||
mod_person_id: PersonId,
|
||||
post_ids: Vec<PostId>,
|
||||
new_removed: bool,
|
||||
removed: bool,
|
||||
reason: &Option<String>,
|
||||
) -> LemmyResult<()> {
|
||||
// Build the forms
|
||||
@ -822,7 +789,7 @@ async fn create_modlog_entries_for_removed_or_restored_posts(
|
||||
.map(|&post_id| ModRemovePostForm {
|
||||
mod_person_id,
|
||||
post_id,
|
||||
removed: Some(new_removed),
|
||||
removed: Some(removed),
|
||||
reason: reason.clone(),
|
||||
})
|
||||
.collect();
|
||||
@ -836,7 +803,7 @@ async fn create_modlog_entries_for_removed_or_restored_comments(
|
||||
pool: &mut DbPool<'_>,
|
||||
mod_person_id: PersonId,
|
||||
comment_ids: Vec<CommentId>,
|
||||
new_removed: bool,
|
||||
removed: bool,
|
||||
reason: &Option<String>,
|
||||
) -> LemmyResult<()> {
|
||||
// Build the forms
|
||||
@ -845,7 +812,7 @@ async fn create_modlog_entries_for_removed_or_restored_comments(
|
||||
.map(|&comment_id| ModRemoveCommentForm {
|
||||
mod_person_id,
|
||||
comment_id,
|
||||
removed: Some(new_removed),
|
||||
removed: Some(removed),
|
||||
reason: reason.clone(),
|
||||
})
|
||||
.collect();
|
||||
@ -1316,9 +1283,10 @@ mod tests {
|
||||
let _inserted_comment_2 = Comment::create(pool, &comment_form_2, None).await?;
|
||||
|
||||
// Remove the user data
|
||||
remove_user_data(
|
||||
remove_or_restore_user_data(
|
||||
inserted_mod.id,
|
||||
inserted_person.id,
|
||||
true,
|
||||
&Some("a remove reason".to_string()),
|
||||
&context,
|
||||
)
|
||||
@ -1369,9 +1337,10 @@ mod tests {
|
||||
assert_eq!(vec![true, true], removed_comments);
|
||||
|
||||
// Now restore the content, and make sure it got appended
|
||||
restore_user_data(
|
||||
remove_or_restore_user_data(
|
||||
inserted_mod.id,
|
||||
inserted_person.id,
|
||||
false,
|
||||
&Some("a restore reason".to_string()),
|
||||
&context,
|
||||
)
|
||||
|
@ -23,7 +23,7 @@ use anyhow::anyhow;
|
||||
use chrono::{DateTime, Utc};
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
utils::{remove_or_restore_user_data_in_community, remove_user_data},
|
||||
utils::{remove_or_restore_user_data, remove_or_restore_user_data_in_community},
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
@ -174,7 +174,8 @@ impl ActivityHandler for BlockUser {
|
||||
)
|
||||
.await?;
|
||||
if self.remove_data.unwrap_or(false) {
|
||||
remove_user_data(mod_person.id, blocked_person.id, &reason, context).await?;
|
||||
remove_or_restore_user_data(mod_person.id, blocked_person.id, true, &reason, context)
|
||||
.await?;
|
||||
}
|
||||
|
||||
// write mod log
|
||||
|
@ -19,7 +19,7 @@ use activitypub_federation::{
|
||||
};
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
utils::{remove_or_restore_user_data_in_community, restore_user_data},
|
||||
utils::{remove_or_restore_user_data, remove_or_restore_user_data_in_community},
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
@ -120,7 +120,8 @@ impl ActivityHandler for UndoBlockUser {
|
||||
.await?;
|
||||
|
||||
if self.restore_data.unwrap_or(false) {
|
||||
restore_user_data(mod_person.id, blocked_person.id, &None, context).await?;
|
||||
remove_or_restore_user_data(mod_person.id, blocked_person.id, false, &None, context)
|
||||
.await?;
|
||||
}
|
||||
|
||||
// write mod log
|
||||
|
@ -40,12 +40,12 @@ impl Comment {
|
||||
pub async fn update_removed_for_creator(
|
||||
pool: &mut DbPool<'_>,
|
||||
for_creator_id: PersonId,
|
||||
new_removed: bool,
|
||||
removed: bool,
|
||||
) -> Result<Vec<Self>, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
diesel::update(comment::table.filter(comment::creator_id.eq(for_creator_id)))
|
||||
.set((
|
||||
comment::removed.eq(new_removed),
|
||||
comment::removed.eq(removed),
|
||||
comment::updated.eq(naive_now()),
|
||||
))
|
||||
.get_results::<Self>(conn)
|
||||
|
@ -145,7 +145,7 @@ impl Post {
|
||||
pool: &mut DbPool<'_>,
|
||||
for_creator_id: PersonId,
|
||||
for_community_id: Option<CommunityId>,
|
||||
new_removed: bool,
|
||||
removed: bool,
|
||||
) -> Result<Vec<Self>, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
|
||||
@ -157,7 +157,7 @@ impl Post {
|
||||
}
|
||||
|
||||
update
|
||||
.set((post::removed.eq(new_removed), post::updated.eq(naive_now())))
|
||||
.set((post::removed.eq(removed), post::updated.eq(naive_now())))
|
||||
.get_results::<Self>(conn)
|
||||
.await
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user