2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 community::{AddModToCommunity, AddModToCommunityResponse},
6 get_local_user_view_from_jwt,
10 objects::{community::ApubCommunity, person::ApubPerson},
11 protocol::activities::community::{add_mod::AddMod, remove_mod::RemoveMod},
13 use lemmy_db_schema::{
15 community::{Community, CommunityModerator, CommunityModeratorForm},
16 moderator::{ModAddCommunity, ModAddCommunityForm},
19 traits::{Crud, Joinable},
21 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
22 use lemmy_utils::{ConnectionId, LemmyError};
23 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
25 #[async_trait::async_trait(?Send)]
26 impl Perform for AddModToCommunity {
27 type Response = AddModToCommunityResponse;
29 #[tracing::instrument(skip(context, websocket_id))]
32 context: &Data<LemmyContext>,
33 websocket_id: Option<ConnectionId>,
34 ) -> Result<AddModToCommunityResponse, LemmyError> {
35 let data: &AddModToCommunity = self;
37 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
39 let community_id = data.community_id;
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)
47 if local_user_view.person.admin && !community.local {
48 return Err(LemmyError::from_message("not_a_moderator"));
51 // Update in local database
52 let community_moderator_form = CommunityModeratorForm {
53 community_id: data.community_id,
54 person_id: data.person_id,
57 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
58 blocking(context.pool(), join)
60 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
62 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
63 blocking(context.pool(), leave)
65 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
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),
75 blocking(context.pool(), move |conn| {
76 ModAddCommunity::create(conn, &form)
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)
87 let community: ApubCommunity = community.into();
92 &local_user_view.person.into(),
100 &local_user_view.person.into(),
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)
114 let res = AddModToCommunityResponse { moderators };
115 context.chat_server().do_send(SendCommunityRoomMessage {
116 op: UserOperation::AddModToCommunity,
117 response: res.clone(),