3 community::announce::AnnouncableActivities,
4 deletion::{delete::receive_remove_action, verify_delete_activity},
7 verify_add_remove_moderator_target,
9 verify_person_in_community,
11 activity_queue::send_to_community_new,
12 extensions::context::lemmy_context,
13 fetcher::object_id::ObjectId,
14 generate_moderators_url,
17 use activitystreams::{
18 activity::kind::RemoveType,
20 primitives::OneOrMany,
23 use lemmy_api_common::blocking;
24 use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler};
25 use lemmy_db_queries::Joinable;
26 use lemmy_db_schema::source::{
27 community::{Community, CommunityModerator, CommunityModeratorForm},
30 use lemmy_utils::LemmyError;
31 use lemmy_websocket::LemmyContext;
32 use serde::{Deserialize, Serialize};
35 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
36 #[serde(rename_all = "camelCase")]
37 pub struct RemoveMod {
38 actor: ObjectId<Person>,
40 pub(in crate::activities) object: ObjectId<Person>,
41 cc: [ObjectId<Community>; 1],
42 #[serde(rename = "type")]
44 // if target is set, this is means remove mod from community
45 pub(in crate::activities) target: Option<Url>,
47 #[serde(rename = "@context")]
48 context: OneOrMany<AnyBase>,
55 community: &Community,
58 context: &LemmyContext,
59 ) -> Result<(), LemmyError> {
60 let id = generate_activity_id(
62 &context.settings().get_protocol_and_hostname(),
64 let remove = RemoveMod {
65 actor: ObjectId::new(actor.actor_id()),
66 to: [PublicUrl::Public],
67 object: ObjectId::new(removed_mod.actor_id()),
68 target: Some(generate_moderators_url(&community.actor_id)?.into()),
70 context: lemmy_context(),
71 cc: [ObjectId::new(community.actor_id())],
72 kind: RemoveType::Remove,
73 unparsed: Default::default(),
76 let activity = AnnouncableActivities::RemoveMod(remove);
77 let inboxes = vec![removed_mod.get_shared_inbox_or_inbox_url()];
78 send_to_community_new(activity, &id, actor, community, inboxes, context).await
82 #[async_trait::async_trait(?Send)]
83 impl ActivityHandler for RemoveMod {
86 context: &LemmyContext,
87 request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 verify_activity(self, &context.settings())?;
90 if let Some(target) = &self.target {
91 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
92 verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
93 verify_add_remove_moderator_target(target, &self.cc[0])?;
95 verify_delete_activity(
110 context: &LemmyContext,
111 request_counter: &mut i32,
112 ) -> Result<(), LemmyError> {
113 if self.target.is_some() {
114 let community = self.cc[0].dereference(context, request_counter).await?;
115 let remove_mod = self.object.dereference(context, request_counter).await?;
117 let form = CommunityModeratorForm {
118 community_id: community.id,
119 person_id: remove_mod.id,
121 blocking(context.pool(), move |conn| {
122 CommunityModerator::leave(conn, &form)
125 // TODO: send websocket notification about removed mod
128 receive_remove_action(