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