4 announce::GetCommunity,
5 get_community_from_moderators_url,
6 send_activity_in_community,
10 verify_add_remove_moderator_target,
13 verify_person_in_community,
15 activity_lists::AnnouncableActivities,
16 generate_moderators_url,
17 objects::{community::ApubCommunity, person::ApubPerson},
18 protocol::activities::community::remove_mod::RemoveMod,
20 use activitystreams_kinds::{activity::RemoveType, public};
21 use lemmy_api_common::blocking;
25 traits::{ActivityHandler, ActorType},
27 use lemmy_db_schema::{
28 source::community::{CommunityModerator, CommunityModeratorForm},
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
35 #[tracing::instrument(skip_all)]
37 community: &ApubCommunity,
38 removed_mod: &ApubPerson,
40 context: &LemmyContext,
41 ) -> Result<(), LemmyError> {
42 let id = generate_activity_id(
44 &context.settings().get_protocol_and_hostname(),
46 let remove = RemoveMod {
47 actor: ObjectId::new(actor.actor_id()),
49 object: ObjectId::new(removed_mod.actor_id()),
50 target: generate_moderators_url(&community.actor_id)?.into(),
52 cc: vec![community.actor_id()],
53 kind: RemoveType::Remove,
54 unparsed: Default::default(),
57 let activity = AnnouncableActivities::RemoveMod(remove);
58 let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()];
59 send_activity_in_community(activity, &id, actor, community, inboxes, context).await
63 #[async_trait::async_trait(?Send)]
64 impl ActivityHandler for RemoveMod {
65 type DataType = LemmyContext;
67 #[tracing::instrument(skip_all)]
70 context: &Data<LemmyContext>,
71 request_counter: &mut i32,
72 ) -> Result<(), LemmyError> {
73 verify_is_public(&self.to, &self.cc)?;
74 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
75 let community = self.get_community(context, request_counter).await?;
76 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
77 verify_mod_action(&self.actor, &community, context, request_counter).await?;
78 verify_add_remove_moderator_target(&self.target, &community)?;
82 #[tracing::instrument(skip_all)]
85 context: &Data<LemmyContext>,
86 request_counter: &mut i32,
87 ) -> Result<(), LemmyError> {
88 let community = self.get_community(context, request_counter).await?;
89 let remove_mod = self.object.dereference(context, request_counter).await?;
91 let form = CommunityModeratorForm {
92 community_id: community.id,
93 person_id: remove_mod.id,
95 blocking(context.pool(), move |conn| {
96 CommunityModerator::leave(conn, &form)
99 // TODO: send websocket notification about removed mod
104 #[async_trait::async_trait(?Send)]
105 impl GetCommunity for RemoveMod {
106 #[tracing::instrument(skip_all)]
107 async fn get_community(
109 context: &LemmyContext,
110 request_counter: &mut i32,
111 ) -> Result<ApubCommunity, LemmyError> {
112 get_community_from_moderators_url(&self.target, context, request_counter).await