]> Untitled Git - lemmy.git/blob - crates/api/src/community/transfer.rs
implement language tags for site/community in db and api (#2434)
[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::{blocking, get_local_user_view_from_jwt},
7 };
8 use lemmy_db_schema::{
9   source::{
10     community::{CommunityModerator, CommunityModeratorForm},
11     moderator::{ModTransferCommunity, ModTransferCommunityForm},
12   },
13   traits::{Crud, Joinable},
14 };
15 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe};
16 use lemmy_utils::{error::LemmyError, location_info, ConnectionId};
17 use lemmy_websocket::LemmyContext;
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 = blocking(context.pool(), PersonViewSafe::admins).await??;
36
37     // Fetch the community mods
38     let community_id = data.community_id;
39     let mut community_mods = blocking(context.pool(), move |conn| {
40       CommunityModeratorView::for_community(conn, community_id)
41     })
42     .await??;
43
44     // Make sure transferrer is either the top community mod, or an admin
45     if local_user_view.person.id != community_mods[0].moderator.id
46       && !admins
47         .iter()
48         .map(|a| a.person.id)
49         .any(|x| x == local_user_view.person.id)
50     {
51       return Err(LemmyError::from_message("not_an_admin"));
52     }
53
54     // You have to re-do the community_moderator table, reordering it.
55     // Add the transferee to the top
56     let creator_index = community_mods
57       .iter()
58       .position(|r| r.moderator.id == data.person_id)
59       .context(location_info!())?;
60     let creator_person = community_mods.remove(creator_index);
61     community_mods.insert(0, creator_person);
62
63     // Delete all the mods
64     let community_id = data.community_id;
65     blocking(context.pool(), move |conn| {
66       CommunityModerator::delete_for_community(conn, community_id)
67     })
68     .await??;
69
70     // TODO: this should probably be a bulk operation
71     // Re-add the mods, in the new order
72     for cmod in &community_mods {
73       let community_moderator_form = CommunityModeratorForm {
74         community_id: cmod.community.id,
75         person_id: cmod.moderator.id,
76       };
77
78       let join = move |conn: &mut _| CommunityModerator::join(conn, &community_moderator_form);
79       blocking(context.pool(), join)
80         .await?
81         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
82     }
83
84     // Mod tables
85     let form = ModTransferCommunityForm {
86       mod_person_id: local_user_view.person.id,
87       other_person_id: data.person_id,
88       community_id: data.community_id,
89       removed: Some(false),
90     };
91     blocking(context.pool(), move |conn| {
92       ModTransferCommunity::create(conn, &form)
93     })
94     .await??;
95
96     let community_id = data.community_id;
97     let person_id = local_user_view.person.id;
98     let community_view = blocking(context.pool(), move |conn| {
99       CommunityView::read(conn, community_id, Some(person_id))
100     })
101     .await?
102     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
103
104     let community_id = data.community_id;
105     let moderators = blocking(context.pool(), move |conn| {
106       CommunityModeratorView::for_community(conn, community_id)
107     })
108     .await?
109     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
110
111     // Return the jwt
112     Ok(GetCommunityResponse {
113       community_view,
114       site: None,
115       moderators,
116       online: 0,
117       discussion_languages: vec![],
118       default_post_language: None,
119     })
120   }
121 }