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::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
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 {
40 pub(in crate::activities) object: Url,
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(RemoveType::Remove)?;
61 let remove = RemoveMod {
62 actor: actor.actor_id(),
63 to: PublicUrl::Public,
64 object: removed_mod.actor_id(),
65 target: Some(generate_moderators_url(&community.actor_id)?.into()),
67 context: lemmy_context(),
68 cc: [community.actor_id()],
69 kind: RemoveType::Remove,
70 unparsed: Default::default(),
73 let activity = AnnouncableActivities::RemoveMod(remove);
74 let inboxes = vec![removed_mod.get_shared_inbox_or_inbox_url()];
75 send_to_community_new(activity, &id, actor, community, inboxes, context).await
79 #[async_trait::async_trait(?Send)]
80 impl ActivityHandler for RemoveMod {
83 context: &LemmyContext,
84 request_counter: &mut i32,
85 ) -> Result<(), LemmyError> {
86 verify_activity(self)?;
87 if let Some(target) = &self.target {
88 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
89 verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
90 verify_add_remove_moderator_target(target, self.cc[0].clone())?;
92 verify_delete_activity(
107 context: &LemmyContext,
108 request_counter: &mut i32,
109 ) -> Result<(), LemmyError> {
110 if self.target.is_some() {
112 get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter).await?;
114 get_or_fetch_and_upsert_person(&self.object, context, request_counter).await?;
116 let form = CommunityModeratorForm {
117 community_id: community.id,
118 person_id: remove_mod.id,
120 blocking(context.pool(), move |conn| {
121 CommunityModerator::leave(conn, &form)
124 // TODO: send websocket notification about removed mod
127 receive_remove_action(&self.actor, &self.object, None, context, request_counter).await