]> Untitled Git - lemmy.git/blob - crates/api/src/community/transfer.rs
Merge websocket crate into api_common
[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   utils::get_local_user_view_from_jwt,
7   LemmyContext,
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, PersonViewSafe};
17 use lemmy_utils::{error::LemmyError, location_info, ConnectionId};
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, _websocket_id))]
26   async fn perform(
27     &self,
28     context: &Data<LemmyContext>,
29     _websocket_id: Option<ConnectionId>,
30   ) -> Result<GetCommunityResponse, LemmyError> {
31     let data: &TransferCommunity = self;
32     let local_user_view =
33       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34
35     let admins = PersonViewSafe::admins(context.pool()).await?;
36
37     // Fetch the community mods
38     let community_id = data.community_id;
39     let mut community_mods =
40       CommunityModeratorView::for_community(context.pool(), community_id).await?;
41
42     // Make sure transferrer is either the top community mod, or an admin
43     if local_user_view.person.id != community_mods[0].moderator.id
44       && !admins
45         .iter()
46         .map(|a| a.person.id)
47         .any(|x| x == local_user_view.person.id)
48     {
49       return Err(LemmyError::from_message("not_an_admin"));
50     }
51
52     // You have to re-do the community_moderator table, reordering it.
53     // Add the transferee to the top
54     let creator_index = community_mods
55       .iter()
56       .position(|r| r.moderator.id == data.person_id)
57       .context(location_info!())?;
58     let creator_person = community_mods.remove(creator_index);
59     community_mods.insert(0, creator_person);
60
61     // Delete all the mods
62     let community_id = data.community_id;
63
64     CommunityModerator::delete_for_community(context.pool(), community_id).await?;
65
66     // TODO: this should probably be a bulk operation
67     // Re-add the mods, in the new order
68     for cmod in &community_mods {
69       let community_moderator_form = CommunityModeratorForm {
70         community_id: cmod.community.id,
71         person_id: cmod.moderator.id,
72       };
73
74       CommunityModerator::join(context.pool(), &community_moderator_form)
75         .await
76         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
77     }
78
79     // Mod tables
80     let form = ModTransferCommunityForm {
81       mod_person_id: local_user_view.person.id,
82       other_person_id: data.person_id,
83       community_id: data.community_id,
84       removed: Some(false),
85     };
86
87     ModTransferCommunity::create(context.pool(), &form).await?;
88
89     let community_id = data.community_id;
90     let person_id = local_user_view.person.id;
91     let community_view = CommunityView::read(context.pool(), community_id, Some(person_id))
92       .await
93       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
94
95     let community_id = data.community_id;
96     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
97       .await
98       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
99
100     // Return the jwt
101     Ok(GetCommunityResponse {
102       community_view,
103       site: None,
104       moderators,
105       online: 0,
106       discussion_languages: vec![],
107       default_post_language: None,
108     })
109   }
110 }