]> Untitled Git - lemmy.git/commitdiff
When banning a user, remove communities they've created (#1700)
authorDessalines <dessalines@users.noreply.github.com>
Fri, 13 Aug 2021 17:39:56 +0000 (13:39 -0400)
committerGitHub <noreply@github.com>
Fri, 13 Aug 2021 17:39:56 +0000 (17:39 +0000)
- Fixes #1659

crates/api/src/local_user.rs
crates/db_views_actor/src/community_moderator_view.rs

index 54fe6d76beadd7bbf463ebd90b0e9e91e7a52ba0..8eee3c1c4599b8cd52c2bb13ba82d891c4a99f9d 100644 (file)
@@ -19,6 +19,7 @@ use lemmy_db_queries::{
   from_opt_str_to_opt_enum,
   source::{
     comment::Comment_,
+    community::Community_,
     local_user::LocalUser_,
     password_reset_request::PasswordResetRequest_,
     person::Person_,
@@ -33,6 +34,7 @@ use lemmy_db_schema::{
   naive_now,
   source::{
     comment::Comment,
+    community::Community,
     local_user::{LocalUser, LocalUserForm},
     moderator::*,
     password_reset_request::*,
@@ -51,6 +53,7 @@ use lemmy_db_views::{
 };
 use lemmy_db_views_actor::{
   community_follower_view::CommunityFollowerView,
+  community_moderator_view::CommunityModeratorView,
   person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
   person_view::PersonViewSafe,
 };
@@ -408,8 +411,24 @@ impl Perform for BanPerson {
 
       // Communities
       // Remove all communities where they're the top mod
-      // TODO couldn't get group by's working in diesel,
       // for now, remove the communities manually
+      let first_mod_communities = blocking(context.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(context.pool(), move |conn: &'_ _| {
+          Community::update_removed(conn, first_mod_community.community.id, true)
+        })
+        .await??;
+      }
 
       // Comments
       blocking(context.pool(), move |conn: &'_ _| {
index 274f682430af057f20f6e80e4789c2bb701d2f9e..9ea1079356d0b9bc2bb86fa58dcdc4b5ba7d2f85 100644 (file)
@@ -49,6 +49,28 @@ impl CommunityModeratorView {
 
     Ok(Self::from_tuple_to_vec(res))
   }
+
+  /// Finds all communities first mods / creators
+  /// Ideally this should be a group by, but diesel doesn't support it yet
+  pub fn get_community_first_mods(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+    let res = community_moderator::table
+      .inner_join(community::table)
+      .inner_join(person::table)
+      .select((
+        Community::safe_columns_tuple(),
+        Person::safe_columns_tuple(),
+      ))
+      // A hacky workaround instead of group_bys
+      // https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres
+      .distinct_on(community_moderator::community_id)
+      .order_by((
+        community_moderator::community_id,
+        community_moderator::person_id,
+      ))
+      .load::<CommunityModeratorViewTuple>(conn)?;
+
+    Ok(Self::from_tuple_to_vec(res))
+  }
 }
 
 impl ViewToVec for CommunityModeratorView {