2 use actix_web::web::Data;
4 use lemmy_api_common::{
5 community::{GetCommunityResponse, TransferCommunity},
6 utils::{blocking, get_local_user_view_from_jwt},
10 community::{CommunityModerator, CommunityModeratorForm},
11 moderator::{ModTransferCommunity, ModTransferCommunityForm},
13 traits::{Crud, Joinable},
15 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe};
16 use lemmy_utils::{error::LemmyError, location_info, ConnectionId};
17 use lemmy_websocket::LemmyContext;
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 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
37 // Fetch the community mods
38 let community_id = data.community_id;
39 let mut community_mods = blocking(context.pool(), move |conn| {
40 CommunityModeratorView::for_community(conn, community_id)
44 // Make sure transferrer is either the top community mod, or an admin
45 if local_user_view.person.id != community_mods[0].moderator.id
49 .any(|x| x == local_user_view.person.id)
51 return Err(LemmyError::from_message("not_an_admin"));
54 // You have to re-do the community_moderator table, reordering it.
55 // Add the transferee to the top
56 let creator_index = community_mods
58 .position(|r| r.moderator.id == data.person_id)
59 .context(location_info!())?;
60 let creator_person = community_mods.remove(creator_index);
61 community_mods.insert(0, creator_person);
63 // Delete all the mods
64 let community_id = data.community_id;
65 blocking(context.pool(), move |conn| {
66 CommunityModerator::delete_for_community(conn, community_id)
70 // TODO: this should probably be a bulk operation
71 // Re-add the mods, in the new order
72 for cmod in &community_mods {
73 let community_moderator_form = CommunityModeratorForm {
74 community_id: cmod.community.id,
75 person_id: cmod.moderator.id,
78 let join = move |conn: &mut _| CommunityModerator::join(conn, &community_moderator_form);
79 blocking(context.pool(), join)
81 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
85 let form = ModTransferCommunityForm {
86 mod_person_id: local_user_view.person.id,
87 other_person_id: data.person_id,
88 community_id: data.community_id,
91 blocking(context.pool(), move |conn| {
92 ModTransferCommunity::create(conn, &form)
96 let community_id = data.community_id;
97 let person_id = local_user_view.person.id;
98 let community_view = blocking(context.pool(), move |conn| {
99 CommunityView::read(conn, community_id, Some(person_id))
102 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
104 let community_id = data.community_id;
105 let moderators = blocking(context.pool(), move |conn| {
106 CommunityModeratorView::for_community(conn, community_id)
109 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
112 Ok(GetCommunityResponse {
117 discussion_languages: vec![],
118 default_post_language: None,