]> Untitled Git - lemmy.git/blob - crates/api/src/community/add_mod.rs
Add diesel_async, get rid of blocking function (#2510)
[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::{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 = Community::read(context.pool(), community_id).await?;
42     if local_user_view.person.admin && !community.local {
43       return Err(LemmyError::from_message("not_a_moderator"));
44     }
45
46     // Update in local database
47     let community_moderator_form = CommunityModeratorForm {
48       community_id: data.community_id,
49       person_id: data.person_id,
50     };
51     if data.added {
52       CommunityModerator::join(context.pool(), &community_moderator_form)
53         .await
54         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
55     } else {
56       CommunityModerator::leave(context.pool(), &community_moderator_form)
57         .await
58         .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
59     }
60
61     // Mod tables
62     let form = ModAddCommunityForm {
63       mod_person_id: local_user_view.person.id,
64       other_person_id: data.person_id,
65       community_id: data.community_id,
66       removed: Some(!data.added),
67     };
68
69     ModAddCommunity::create(context.pool(), &form).await?;
70
71     // Send to federated instances
72     let updated_mod_id = data.person_id;
73     let updated_mod: ApubPerson = Person::read(context.pool(), updated_mod_id).await?.into();
74     let community: ApubCommunity = community.into();
75     if data.added {
76       AddMod::send(
77         &community,
78         &updated_mod,
79         &local_user_view.person.into(),
80         context,
81       )
82       .await?;
83     } else {
84       RemoveMod::send(
85         &community,
86         &updated_mod,
87         &local_user_view.person.into(),
88         context,
89       )
90       .await?;
91     }
92
93     // Note: in case a remote mod is added, this returns the old moderators list, it will only get
94     //       updated once we receive an activity from the community (like `Announce/Add/Moderator`)
95     let community_id = data.community_id;
96     let moderators = CommunityModeratorView::for_community(context.pool(), community_id).await?;
97
98     let res = AddModToCommunityResponse { moderators };
99     context.chat_server().do_send(SendCommunityRoomMessage {
100       op: UserOperation::AddModToCommunity,
101       response: res.clone(),
102       community_id,
103       websocket_id,
104     });
105     Ok(res)
106   }
107 }