3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_add_remove_moderator_target,
8 verify_person_in_community,
10 context::lemmy_context,
11 fetcher::object_id::ObjectId,
12 generate_moderators_url,
13 objects::{community::ApubCommunity, person::ApubPerson},
15 use activitystreams::{
16 activity::kind::RemoveType,
18 primitives::OneOrMany,
21 use lemmy_api_common::blocking;
24 traits::{ActivityFields, ActivityHandler, ActorType},
27 use lemmy_db_schema::{
28 source::community::{CommunityModerator, CommunityModeratorForm},
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
33 use serde::{Deserialize, Serialize};
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
38 pub struct RemoveMod {
39 actor: ObjectId<ApubPerson>,
41 pub(in crate::activities) object: ObjectId<ApubPerson>,
42 cc: [ObjectId<ApubCommunity>; 1],
43 #[serde(rename = "type")]
45 // if target is set, this is means remove mod from community
46 pub(in crate::activities) target: Url,
48 #[serde(rename = "@context")]
49 context: OneOrMany<AnyBase>,
56 community: &ApubCommunity,
57 removed_mod: &ApubPerson,
59 context: &LemmyContext,
60 ) -> Result<(), LemmyError> {
61 let id = generate_activity_id(
63 &context.settings().get_protocol_and_hostname(),
65 let remove = RemoveMod {
66 actor: ObjectId::new(actor.actor_id()),
67 to: [PublicUrl::Public],
68 object: ObjectId::new(removed_mod.actor_id()),
69 target: generate_moderators_url(&community.actor_id)?.into(),
71 context: lemmy_context(),
72 cc: [ObjectId::new(community.actor_id())],
73 kind: RemoveType::Remove,
74 unparsed: Default::default(),
77 let activity = AnnouncableActivities::RemoveMod(remove);
78 let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()];
79 send_to_community(activity, &id, actor, community, inboxes, context).await
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for RemoveMod {
85 type DataType = LemmyContext;
88 context: &Data<LemmyContext>,
89 request_counter: &mut i32,
90 ) -> Result<(), LemmyError> {
91 verify_activity(self, &context.settings())?;
92 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
93 verify_mod_action(&self.actor, &self.cc[0], context, request_counter).await?;
94 verify_add_remove_moderator_target(&self.target, &self.cc[0])?;
100 context: &Data<LemmyContext>,
101 request_counter: &mut i32,
102 ) -> Result<(), LemmyError> {
103 let community = self.cc[0].dereference(context, request_counter).await?;
104 let remove_mod = self.object.dereference(context, request_counter).await?;
106 let form = CommunityModeratorForm {
107 community_id: community.id,
108 person_id: remove_mod.id,
110 blocking(context.pool(), move |conn| {
111 CommunityModerator::leave(conn, &form)
114 // TODO: send websocket notification about removed mod