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},
8 objects::{community::ApubCommunity, person::ApubPerson},
9 protocol::activities::community::{add_mod::AddMod, remove_mod::RemoveMod},
11 use lemmy_db_schema::{
13 community::{Community, CommunityModerator, CommunityModeratorForm},
14 moderator::{ModAddCommunity, ModAddCommunityForm},
17 traits::{Crud, Joinable},
19 use lemmy_db_views_actor::structs::CommunityModeratorView;
20 use lemmy_utils::{error::LemmyError, ConnectionId};
21 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
23 #[async_trait::async_trait(?Send)]
24 impl Perform for AddModToCommunity {
25 type Response = AddModToCommunityResponse;
27 #[tracing::instrument(skip(context, websocket_id))]
30 context: &Data<LemmyContext>,
31 websocket_id: Option<ConnectionId>,
32 ) -> Result<AddModToCommunityResponse, LemmyError> {
33 let data: &AddModToCommunity = self;
35 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37 let community_id = data.community_id;
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)
45 if local_user_view.person.admin && !community.local {
46 return Err(LemmyError::from_message("not_a_moderator"));
49 // Update in local database
50 let community_moderator_form = CommunityModeratorForm {
51 community_id: data.community_id,
52 person_id: data.person_id,
55 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
56 blocking(context.pool(), join)
58 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
60 let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
61 blocking(context.pool(), leave)
63 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
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),
73 blocking(context.pool(), move |conn| {
74 ModAddCommunity::create(conn, &form)
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)
85 let community: ApubCommunity = community.into();
90 &local_user_view.person.into(),
98 &local_user_view.person.into(),
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)
112 let res = AddModToCommunityResponse { moderators };
113 context.chat_server().do_send(SendCommunityRoomMessage {
114 op: UserOperation::AddModToCommunity,
115 response: res.clone(),