2 use actix_web::web::Data;
4 use lemmy_api_common::{
5 community::{GetCommunityResponse, TransferCommunity},
7 utils::{is_admin, is_top_mod, local_user_view_from_jwt},
11 community::{CommunityModerator, CommunityModeratorForm},
12 moderator::{ModTransferCommunity, ModTransferCommunityForm},
14 traits::{Crud, Joinable},
16 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
17 use lemmy_utils::{error::LemmyError, location_info};
19 // TODO: we dont do anything for federation here, it should be updated the next time the community
20 // gets fetched. i hope we can get rid of the community creator role soon.
21 #[async_trait::async_trait(?Send)]
22 impl Perform for TransferCommunity {
23 type Response = GetCommunityResponse;
25 #[tracing::instrument(skip(context))]
28 context: &Data<LemmyContext>,
29 ) -> Result<GetCommunityResponse, LemmyError> {
30 let data: &TransferCommunity = self;
31 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
33 // Fetch the community mods
34 let community_id = data.community_id;
35 let mut community_mods =
36 CommunityModeratorView::for_community(context.pool(), community_id).await?;
38 // Make sure transferrer is either the top community mod, or an admin
39 if !(is_top_mod(&local_user_view, &community_mods).is_ok()
40 || is_admin(&local_user_view).is_ok())
42 return Err(LemmyError::from_message("not_an_admin"));
45 // You have to re-do the community_moderator table, reordering it.
46 // Add the transferee to the top
47 let creator_index = community_mods
49 .position(|r| r.moderator.id == data.person_id)
50 .context(location_info!())?;
51 let creator_person = community_mods.remove(creator_index);
52 community_mods.insert(0, creator_person);
54 // Delete all the mods
55 let community_id = data.community_id;
57 CommunityModerator::delete_for_community(context.pool(), community_id).await?;
59 // TODO: this should probably be a bulk operation
60 // Re-add the mods, in the new order
61 for cmod in &community_mods {
62 let community_moderator_form = CommunityModeratorForm {
63 community_id: cmod.community.id,
64 person_id: cmod.moderator.id,
67 CommunityModerator::join(context.pool(), &community_moderator_form)
69 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
73 let form = ModTransferCommunityForm {
74 mod_person_id: local_user_view.person.id,
75 other_person_id: data.person_id,
76 community_id: data.community_id,
79 ModTransferCommunity::create(context.pool(), &form).await?;
81 let community_id = data.community_id;
82 let person_id = local_user_view.person.id;
83 let community_view = CommunityView::read(context.pool(), community_id, Some(person_id), None)
85 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
87 let community_id = data.community_id;
88 let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
90 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
93 Ok(GetCommunityResponse {
97 discussion_languages: vec![],