]> Untitled Git - lemmy.git/blob - crates/api/src/community/transfer.rs
Remove chatserver (#2919)
[lemmy.git] / crates / api / src / community / transfer.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use anyhow::Context;
4 use lemmy_api_common::{
5   community::{GetCommunityResponse, TransferCommunity},
6   context::LemmyContext,
7   utils::{is_admin, is_top_mod, local_user_view_from_jwt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     community::{CommunityModerator, CommunityModeratorForm},
12     moderator::{ModTransferCommunity, ModTransferCommunityForm},
13   },
14   traits::{Crud, Joinable},
15 };
16 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
17 use lemmy_utils::{error::LemmyError, location_info};
18
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;
24
25   #[tracing::instrument(skip(context))]
26   async fn perform(
27     &self,
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?;
32
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?;
37
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())
41     {
42       return Err(LemmyError::from_message("not_an_admin"));
43     }
44
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
48       .iter()
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);
53
54     // Delete all the mods
55     let community_id = data.community_id;
56
57     CommunityModerator::delete_for_community(context.pool(), community_id).await?;
58
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,
65       };
66
67       CommunityModerator::join(context.pool(), &community_moderator_form)
68         .await
69         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
70     }
71
72     // Mod tables
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,
77     };
78
79     ModTransferCommunity::create(context.pool(), &form).await?;
80
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)
84       .await
85       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
86
87     let community_id = data.community_id;
88     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
89       .await
90       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
91
92     // Return the jwt
93     Ok(GetCommunityResponse {
94       community_view,
95       site: None,
96       moderators,
97       discussion_languages: vec![],
98     })
99   }
100 }