2 use actix_web::web::Data;
4 use lemmy_api_common::{
5 community::{GetCommunityResponse, TransferCommunity},
7 utils::{get_local_user_view_from_jwt, is_admin, is_top_mod},
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, ConnectionId};
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, _websocket_id))]
28 context: &Data<LemmyContext>,
29 _websocket_id: Option<ConnectionId>,
30 ) -> Result<GetCommunityResponse, LemmyError> {
31 let data: &TransferCommunity = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35 // Fetch the community mods
36 let community_id = data.community_id;
37 let mut community_mods =
38 CommunityModeratorView::for_community(context.pool(), community_id).await?;
40 // Make sure transferrer is either the top community mod, or an admin
41 if !(is_top_mod(&local_user_view, &community_mods).is_ok()
42 || is_admin(&local_user_view).is_ok())
44 return Err(LemmyError::from_message("not_an_admin"));
47 // You have to re-do the community_moderator table, reordering it.
48 // Add the transferee to the top
49 let creator_index = community_mods
51 .position(|r| r.moderator.id == data.person_id)
52 .context(location_info!())?;
53 let creator_person = community_mods.remove(creator_index);
54 community_mods.insert(0, creator_person);
56 // Delete all the mods
57 let community_id = data.community_id;
59 CommunityModerator::delete_for_community(context.pool(), community_id).await?;
61 // TODO: this should probably be a bulk operation
62 // Re-add the mods, in the new order
63 for cmod in &community_mods {
64 let community_moderator_form = CommunityModeratorForm {
65 community_id: cmod.community.id,
66 person_id: cmod.moderator.id,
69 CommunityModerator::join(context.pool(), &community_moderator_form)
71 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
75 let form = ModTransferCommunityForm {
76 mod_person_id: local_user_view.person.id,
77 other_person_id: data.person_id,
78 community_id: data.community_id,
81 ModTransferCommunity::create(context.pool(), &form).await?;
83 let community_id = data.community_id;
84 let person_id = local_user_view.person.id;
85 let community_view = CommunityView::read(context.pool(), community_id, Some(person_id), None)
87 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
89 let community_id = data.community_id;
90 let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
92 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
95 Ok(GetCommunityResponse {
100 discussion_languages: vec![],
101 default_post_language: None,