]> Untitled Git - lemmy.git/blobdiff - crates/api/src/community/transfer.rs
Adding diesel enums for SortType and ListingType (#2808)
[lemmy.git] / crates / api / src / community / transfer.rs
index 7ca174e4ccdc802e7ed71c76446c2c221a89fe51..a34dfb40d56f3064cc8e2ea96a4243fcfdf4402d 100644 (file)
@@ -4,7 +4,7 @@ use anyhow::Context;
 use lemmy_api_common::{
   community::{GetCommunityResponse, TransferCommunity},
   context::LemmyContext,
-  utils::get_local_user_view_from_jwt,
+  utils::{get_local_user_view_from_jwt, is_admin, is_top_mod},
 };
 use lemmy_db_schema::{
   source::{
@@ -13,7 +13,7 @@ use lemmy_db_schema::{
   },
   traits::{Crud, Joinable},
 };
-use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe};
+use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
 use lemmy_utils::{error::LemmyError, location_info, ConnectionId};
 
 // TODO: we dont do anything for federation here, it should be updated the next time the community
@@ -32,19 +32,14 @@ impl Perform for TransferCommunity {
     let local_user_view =
       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
-    let admins = PersonViewSafe::admins(context.pool()).await?;
-
     // Fetch the community mods
     let community_id = data.community_id;
     let mut community_mods =
       CommunityModeratorView::for_community(context.pool(), community_id).await?;
 
     // Make sure transferrer is either the top community mod, or an admin
-    if local_user_view.person.id != community_mods[0].moderator.id
-      && !admins
-        .iter()
-        .map(|a| a.person.id)
-        .any(|x| x == local_user_view.person.id)
+    if !(is_top_mod(&local_user_view, &community_mods).is_ok()
+      || is_admin(&local_user_view).is_ok())
     {
       return Err(LemmyError::from_message("not_an_admin"));
     }
@@ -81,14 +76,13 @@ impl Perform for TransferCommunity {
       mod_person_id: local_user_view.person.id,
       other_person_id: data.person_id,
       community_id: data.community_id,
-      removed: Some(false),
     };
 
     ModTransferCommunity::create(context.pool(), &form).await?;
 
     let community_id = data.community_id;
     let person_id = local_user_view.person.id;
-    let community_view = CommunityView::read(context.pool(), community_id, Some(person_id))
+    let community_view = CommunityView::read(context.pool(), community_id, Some(person_id), None)
       .await
       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;