3 community::{announce::AnnouncableActivities, send_to_community},
4 deletion::{delete::receive_remove_action, verify_delete_activity},
7 verify_add_remove_moderator_target,
9 verify_person_in_community,
11 context::lemmy_context,
12 fetcher::object_id::ObjectId,
13 generate_moderators_url,
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_queries::Joinable;
28 use lemmy_db_schema::source::{
29 community::{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<Person>,
42 pub(in crate::activities) object: ObjectId<Person>,
43 cc: [ObjectId<Community>; 1],
44 #[serde(rename = "type")]
46 // if target is set, this is means remove mod from community
47 pub(in crate::activities) target: Option<Url>,
49 #[serde(rename = "@context")]
50 context: OneOrMany<AnyBase>,
57 community: &Community,
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()),
68 to: [PublicUrl::Public],
69 object: ObjectId::new(removed_mod.actor_id()),
70 target: Some(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_activity(self, &context.settings())?;
93 if let Some(target) = &self.target {
94 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
95 verify_mod_action(&self.actor, self.cc[0].clone(), context, request_counter).await?;
96 verify_add_remove_moderator_target(target, &self.cc[0])?;
98 verify_delete_activity(
113 context: &Data<LemmyContext>,
114 request_counter: &mut i32,
115 ) -> Result<(), LemmyError> {
116 if self.target.is_some() {
117 let community = self.cc[0].dereference(context, request_counter).await?;
118 let remove_mod = self.object.dereference(context, request_counter).await?;
120 let form = CommunityModeratorForm {
121 community_id: community.id,
122 person_id: remove_mod.id,
124 blocking(context.pool(), move |conn| {
125 CommunityModerator::leave(conn, &form)
128 // TODO: send websocket notification about removed mod
131 receive_remove_action(