]> Untitled Git - lemmy.git/blob - crates/api/src/community/transfer.rs
Replace Option<bool> with bool for PostQuery and CommentQuery (#3819) (#3857)
[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::{
18   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
19   location_info,
20 };
21
22 // TODO: we dont do anything for federation here, it should be updated the next time the community
23 //       gets fetched. i hope we can get rid of the community creator role soon.
24 #[async_trait::async_trait(?Send)]
25 impl Perform for TransferCommunity {
26   type Response = GetCommunityResponse;
27
28   #[tracing::instrument(skip(context))]
29   async fn perform(
30     &self,
31     context: &Data<LemmyContext>,
32   ) -> Result<GetCommunityResponse, LemmyError> {
33     let data: &TransferCommunity = self;
34     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
35
36     // Fetch the community mods
37     let community_id = data.community_id;
38     let mut community_mods =
39       CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
40
41     // Make sure transferrer is either the top community mod, or an admin
42     if !(is_top_mod(&local_user_view, &community_mods).is_ok()
43       || is_admin(&local_user_view).is_ok())
44     {
45       return Err(LemmyErrorType::NotAnAdmin)?;
46     }
47
48     // You have to re-do the community_moderator table, reordering it.
49     // Add the transferee to the top
50     let creator_index = community_mods
51       .iter()
52       .position(|r| r.moderator.id == data.person_id)
53       .context(location_info!())?;
54     let creator_person = community_mods.remove(creator_index);
55     community_mods.insert(0, creator_person);
56
57     // Delete all the mods
58     let community_id = data.community_id;
59
60     CommunityModerator::delete_for_community(&mut context.pool(), community_id).await?;
61
62     // TODO: this should probably be a bulk operation
63     // Re-add the mods, in the new order
64     for cmod in &community_mods {
65       let community_moderator_form = CommunityModeratorForm {
66         community_id: cmod.community.id,
67         person_id: cmod.moderator.id,
68       };
69
70       CommunityModerator::join(&mut context.pool(), &community_moderator_form)
71         .await
72         .with_lemmy_type(LemmyErrorType::CommunityModeratorAlreadyExists)?;
73     }
74
75     // Mod tables
76     let form = ModTransferCommunityForm {
77       mod_person_id: local_user_view.person.id,
78       other_person_id: data.person_id,
79       community_id: data.community_id,
80     };
81
82     ModTransferCommunity::create(&mut context.pool(), &form).await?;
83
84     let community_id = data.community_id;
85     let person_id = local_user_view.person.id;
86     let community_view =
87       CommunityView::read(&mut context.pool(), community_id, Some(person_id), false)
88         .await
89         .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
90
91     let community_id = data.community_id;
92     let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)
93       .await
94       .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
95
96     // Return the jwt
97     Ok(GetCommunityResponse {
98       community_view,
99       site: None,
100       moderators,
101       discussion_languages: vec![],
102     })
103   }
104 }