]> Untitled Git - lemmy.git/blob - crates/api/src/community/transfer.rs
Dont return error in case optional auth is invalid (#2879)
[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, 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 = local_user_view_from_jwt(&data.auth, context).await?;
33
34     // Fetch the community mods
35     let community_id = data.community_id;
36     let mut community_mods =
37       CommunityModeratorView::for_community(context.pool(), community_id).await?;
38
39     // Make sure transferrer is either the top community mod, or an admin
40     if !(is_top_mod(&local_user_view, &community_mods).is_ok()
41       || is_admin(&local_user_view).is_ok())
42     {
43       return Err(LemmyError::from_message("not_an_admin"));
44     }
45
46     // You have to re-do the community_moderator table, reordering it.
47     // Add the transferee to the top
48     let creator_index = community_mods
49       .iter()
50       .position(|r| r.moderator.id == data.person_id)
51       .context(location_info!())?;
52     let creator_person = community_mods.remove(creator_index);
53     community_mods.insert(0, creator_person);
54
55     // Delete all the mods
56     let community_id = data.community_id;
57
58     CommunityModerator::delete_for_community(context.pool(), community_id).await?;
59
60     // TODO: this should probably be a bulk operation
61     // Re-add the mods, in the new order
62     for cmod in &community_mods {
63       let community_moderator_form = CommunityModeratorForm {
64         community_id: cmod.community.id,
65         person_id: cmod.moderator.id,
66       };
67
68       CommunityModerator::join(context.pool(), &community_moderator_form)
69         .await
70         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
71     }
72
73     // Mod tables
74     let form = ModTransferCommunityForm {
75       mod_person_id: local_user_view.person.id,
76       other_person_id: data.person_id,
77       community_id: data.community_id,
78     };
79
80     ModTransferCommunity::create(context.pool(), &form).await?;
81
82     let community_id = data.community_id;
83     let person_id = local_user_view.person.id;
84     let community_view = CommunityView::read(context.pool(), community_id, Some(person_id), None)
85       .await
86       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
87
88     let community_id = data.community_id;
89     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
90       .await
91       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
92
93     // Return the jwt
94     Ok(GetCommunityResponse {
95       community_view,
96       site: None,
97       moderators,
98       online: 0,
99       discussion_languages: vec![],
100     })
101   }
102 }