]> Untitled Git - lemmy.git/blob - crates/api/src/community/add_mod.rs
Split apart api files (#2216)
[lemmy.git] / crates / api / src / community / add_mod.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   community::{AddModToCommunity, AddModToCommunityResponse},
6   get_local_user_view_from_jwt,
7   is_mod_or_admin,
8 };
9 use lemmy_apub::{
10   objects::{community::ApubCommunity, person::ApubPerson},
11   protocol::activities::community::{add_mod::AddMod, remove_mod::RemoveMod},
12 };
13 use lemmy_db_schema::{
14   source::{
15     community::{Community, CommunityModerator, CommunityModeratorForm},
16     moderator::{ModAddCommunity, ModAddCommunityForm},
17     person::Person,
18   },
19   traits::{Crud, Joinable},
20 };
21 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
22 use lemmy_utils::{ConnectionId, LemmyError};
23 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
24
25 #[async_trait::async_trait(?Send)]
26 impl Perform for AddModToCommunity {
27   type Response = AddModToCommunityResponse;
28
29   #[tracing::instrument(skip(context, websocket_id))]
30   async fn perform(
31     &self,
32     context: &Data<LemmyContext>,
33     websocket_id: Option<ConnectionId>,
34   ) -> Result<AddModToCommunityResponse, LemmyError> {
35     let data: &AddModToCommunity = self;
36     let local_user_view =
37       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38
39     let community_id = data.community_id;
40
41     // Verify that only mods or admins can add mod
42     is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
43     let community = blocking(context.pool(), move |conn| {
44       Community::read(conn, community_id)
45     })
46     .await??;
47     if local_user_view.person.admin && !community.local {
48       return Err(LemmyError::from_message("not_a_moderator"));
49     }
50
51     // Update in local database
52     let community_moderator_form = CommunityModeratorForm {
53       community_id: data.community_id,
54       person_id: data.person_id,
55     };
56     if data.added {
57       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
58       blocking(context.pool(), join)
59         .await?
60         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
61     } else {
62       let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
63       blocking(context.pool(), leave)
64         .await?
65         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
66     }
67
68     // Mod tables
69     let form = ModAddCommunityForm {
70       mod_person_id: local_user_view.person.id,
71       other_person_id: data.person_id,
72       community_id: data.community_id,
73       removed: Some(!data.added),
74     };
75     blocking(context.pool(), move |conn| {
76       ModAddCommunity::create(conn, &form)
77     })
78     .await??;
79
80     // Send to federated instances
81     let updated_mod_id = data.person_id;
82     let updated_mod: ApubPerson = blocking(context.pool(), move |conn| {
83       Person::read(conn, updated_mod_id)
84     })
85     .await??
86     .into();
87     let community: ApubCommunity = community.into();
88     if data.added {
89       AddMod::send(
90         &community,
91         &updated_mod,
92         &local_user_view.person.into(),
93         context,
94       )
95       .await?;
96     } else {
97       RemoveMod::send(
98         &community,
99         &updated_mod,
100         &local_user_view.person.into(),
101         context,
102       )
103       .await?;
104     }
105
106     // Note: in case a remote mod is added, this returns the old moderators list, it will only get
107     //       updated once we receive an activity from the community (like `Announce/Add/Moderator`)
108     let community_id = data.community_id;
109     let moderators = blocking(context.pool(), move |conn| {
110       CommunityModeratorView::for_community(conn, community_id)
111     })
112     .await??;
113
114     let res = AddModToCommunityResponse { moderators };
115     context.chat_server().do_send(SendCommunityRoomMessage {
116       op: UserOperation::AddModToCommunity,
117       response: res.clone(),
118       community_id,
119       websocket_id,
120     });
121     Ok(res)
122   }
123 }