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},
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 = 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"));
46 // Update in local database
47 let community_moderator_form = CommunityModeratorForm {
48 community_id: data.community_id,
49 person_id: data.person_id,
52 CommunityModerator::join(context.pool(), &community_moderator_form)
54 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
56 CommunityModerator::leave(context.pool(), &community_moderator_form)
58 .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
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),
69 ModAddCommunity::create(context.pool(), &form).await?;
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();
79 &local_user_view.person.into(),
87 &local_user_view.person.into(),
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?;
98 let res = AddModToCommunityResponse { moderators };
99 context.chat_server().do_send(SendCommunityRoomMessage {
100 op: UserOperation::AddModToCommunity,
101 response: res.clone(),