3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_add_remove_moderator_target,
9 verify_person_in_community,
11 context::lemmy_context,
12 fetcher::object_id::ObjectId,
13 generate_moderators_url,
14 objects::{community::ApubCommunity, person::ApubPerson},
16 use activitystreams::{
17 activity::kind::RemoveType,
19 primitives::OneOrMany,
23 use lemmy_api_common::blocking;
26 traits::{ActivityFields, ActivityHandler, ActorType},
28 use lemmy_db_schema::{
29 source::community::{CommunityModerator, CommunityModeratorForm},
32 use lemmy_utils::LemmyError;
33 use lemmy_websocket::LemmyContext;
34 use serde::{Deserialize, Serialize};
37 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
38 #[serde(rename_all = "camelCase")]
39 pub struct RemoveMod {
40 actor: ObjectId<ApubPerson>,
42 pub(in crate::activities) object: ObjectId<ApubPerson>,
43 cc: [ObjectId<ApubCommunity>; 1],
44 #[serde(rename = "type")]
46 // if target is set, this is means remove mod from community
47 pub(in crate::activities) target: Url,
49 #[serde(rename = "@context")]
50 context: OneOrMany<AnyBase>,
57 community: &ApubCommunity,
58 removed_mod: &ApubPerson,
60 context: &LemmyContext,
61 ) -> Result<(), LemmyError> {
62 let id = generate_activity_id(
64 &context.settings().get_protocol_and_hostname(),
66 let remove = RemoveMod {
67 actor: ObjectId::new(actor.actor_id()),
69 object: ObjectId::new(removed_mod.actor_id()),
70 target: generate_moderators_url(&community.actor_id)?.into(),
72 context: lemmy_context(),
73 cc: [ObjectId::new(community.actor_id())],
74 kind: RemoveType::Remove,
75 unparsed: Default::default(),
78 let activity = AnnouncableActivities::RemoveMod(remove);
79 let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()];
80 send_to_community(activity, &id, actor, community, inboxes, context).await
84 #[async_trait::async_trait(?Send)]
85 impl ActivityHandler for RemoveMod {
86 type DataType = LemmyContext;
89 context: &Data<LemmyContext>,
90 request_counter: &mut i32,
91 ) -> Result<(), LemmyError> {
92 verify_is_public(&self.to)?;
93 verify_activity(self, &context.settings())?;
94 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
95 verify_mod_action(&self.actor, &self.cc[0], context, request_counter).await?;
96 verify_add_remove_moderator_target(&self.target, &self.cc[0])?;
102 context: &Data<LemmyContext>,
103 request_counter: &mut i32,
104 ) -> Result<(), LemmyError> {
105 let community = self.cc[0].dereference(context, request_counter).await?;
106 let remove_mod = self.object.dereference(context, request_counter).await?;
108 let form = CommunityModeratorForm {
109 community_id: community.id,
110 person_id: remove_mod.id,
112 blocking(context.pool(), move |conn| {
113 CommunityModerator::leave(conn, &form)
116 // TODO: send websocket notification about removed mod