2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 community::{GetCommunityResponse, TransferCommunity},
7 get_local_user_view_from_jwt,
11 community::{CommunityModerator, CommunityModeratorForm},
12 moderator::{ModTransferCommunity, ModTransferCommunityForm},
14 traits::{Crud, Joinable},
16 use lemmy_db_views_actor::{
17 community_moderator_view::CommunityModeratorView,
18 community_view::CommunityView,
19 person_view::PersonViewSafe,
21 use lemmy_utils::{location_info, ConnectionId, LemmyError};
22 use lemmy_websocket::LemmyContext;
24 // TODO: we dont do anything for federation here, it should be updated the next time the community
25 // gets fetched. i hope we can get rid of the community creator role soon.
26 #[async_trait::async_trait(?Send)]
27 impl Perform for TransferCommunity {
28 type Response = GetCommunityResponse;
30 #[tracing::instrument(skip(context, _websocket_id))]
33 context: &Data<LemmyContext>,
34 _websocket_id: Option<ConnectionId>,
35 ) -> Result<GetCommunityResponse, LemmyError> {
36 let data: &TransferCommunity = self;
38 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
40 let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
42 // Fetch the community mods
43 let community_id = data.community_id;
44 let mut community_mods = blocking(context.pool(), move |conn| {
45 CommunityModeratorView::for_community(conn, community_id)
49 // Make sure transferrer is either the top community mod, or an admin
50 if local_user_view.person.id != community_mods[0].moderator.id
54 .any(|x| x == local_user_view.person.id)
56 return Err(LemmyError::from_message("not_an_admin"));
59 // You have to re-do the community_moderator table, reordering it.
60 // Add the transferee to the top
61 let creator_index = community_mods
63 .position(|r| r.moderator.id == data.person_id)
64 .context(location_info!())?;
65 let creator_person = community_mods.remove(creator_index);
66 community_mods.insert(0, creator_person);
68 // Delete all the mods
69 let community_id = data.community_id;
70 blocking(context.pool(), move |conn| {
71 CommunityModerator::delete_for_community(conn, community_id)
75 // TODO: this should probably be a bulk operation
76 // Re-add the mods, in the new order
77 for cmod in &community_mods {
78 let community_moderator_form = CommunityModeratorForm {
79 community_id: cmod.community.id,
80 person_id: cmod.moderator.id,
83 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
84 blocking(context.pool(), join)
86 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
90 let form = ModTransferCommunityForm {
91 mod_person_id: local_user_view.person.id,
92 other_person_id: data.person_id,
93 community_id: data.community_id,
96 blocking(context.pool(), move |conn| {
97 ModTransferCommunity::create(conn, &form)
101 let community_id = data.community_id;
102 let person_id = local_user_view.person.id;
103 let community_view = blocking(context.pool(), move |conn| {
104 CommunityView::read(conn, community_id, Some(person_id))
107 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
109 let community_id = data.community_id;
110 let moderators = blocking(context.pool(), move |conn| {
111 CommunityModeratorView::for_community(conn, community_id)
114 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
117 Ok(GetCommunityResponse {