+
+pub async fn remove_user_data(banned_person_id: PersonId, pool: &DbPool) -> Result<(), LemmyError> {
+ // Posts
+ blocking(pool, move |conn: &'_ _| {
+ Post::update_removed_for_creator(conn, banned_person_id, None, true)
+ })
+ .await??;
+
+ // Communities
+ // Remove all communities where they're the top mod
+ // for now, remove the communities manually
+ let first_mod_communities = blocking(pool, move |conn: &'_ _| {
+ CommunityModeratorView::get_community_first_mods(conn)
+ })
+ .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 {
+ blocking(pool, move |conn: &'_ _| {
+ Community::update_removed(conn, first_mod_community.community.id, true)
+ })
+ .await??;
+ }
+
+ // Comments
+ blocking(pool, move |conn: &'_ _| {
+ Comment::update_removed_for_creator(conn, banned_person_id, true)
+ })
+ .await??;
+
+ Ok(())
+}
+
+pub async fn remove_user_data_in_community(
+ community_id: CommunityId,
+ banned_person_id: PersonId,
+ pool: &DbPool,
+) -> Result<(), LemmyError> {
+ // Posts
+ blocking(pool, move |conn| {
+ Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
+ })
+ .await??;
+
+ // Comments
+ // TODO Diesel doesn't allow updates with joins, so this has to be a loop
+ let comments = blocking(pool, move |conn| {
+ CommentQueryBuilder::create(conn)
+ .creator_id(banned_person_id)
+ .community_id(community_id)
+ .limit(std::i64::MAX)
+ .list()
+ })
+ .await??;
+
+ for comment_view in &comments {
+ let comment_id = comment_view.comment.id;
+ blocking(pool, move |conn| {
+ Comment::update_removed(conn, comment_id, true)
+ })
+ .await??;
+ }
+
+ Ok(())
+}