4 announce::GetCommunity,
5 get_community_from_moderators_url,
6 send_activity_in_community,
9 verify_add_remove_moderator_target,
12 verify_person_in_community,
14 activity_lists::AnnouncableActivities,
15 generate_moderators_url,
17 objects::{community::ApubCommunity, person::ApubPerson},
18 protocol::activities::community::remove_mod::RemoveMod,
21 use activitypub_federation::{
22 core::object_id::ObjectId,
24 traits::{ActivityHandler, Actor},
26 use activitystreams_kinds::{activity::RemoveType, public};
27 use lemmy_db_schema::{
29 community::{CommunityModerator, CommunityModeratorForm},
30 moderator::{ModAddCommunity, ModAddCommunityForm},
32 traits::{Crud, Joinable},
34 use lemmy_utils::error::LemmyError;
35 use lemmy_websocket::LemmyContext;
39 #[tracing::instrument(skip_all)]
41 community: &ApubCommunity,
42 removed_mod: &ApubPerson,
44 context: &LemmyContext,
45 ) -> Result<(), LemmyError> {
46 let id = generate_activity_id(
48 &context.settings().get_protocol_and_hostname(),
50 let remove = RemoveMod {
51 actor: ObjectId::new(actor.actor_id()),
53 object: ObjectId::new(removed_mod.actor_id()),
54 target: generate_moderators_url(&community.actor_id)?.into(),
56 cc: vec![community.actor_id()],
57 kind: RemoveType::Remove,
58 unparsed: Default::default(),
61 let activity = AnnouncableActivities::RemoveMod(remove);
62 let inboxes = vec![removed_mod.shared_inbox_or_inbox()];
63 send_activity_in_community(activity, actor, community, inboxes, context).await
67 #[async_trait::async_trait(?Send)]
68 impl ActivityHandler for RemoveMod {
69 type DataType = LemmyContext;
70 type Error = LemmyError;
72 fn id(&self) -> &Url {
76 fn actor(&self) -> &Url {
80 #[tracing::instrument(skip_all)]
83 context: &Data<LemmyContext>,
84 request_counter: &mut i32,
85 ) -> Result<(), LemmyError> {
86 verify_is_public(&self.to, &self.cc)?;
87 let community = self.get_community(context, request_counter).await?;
88 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
97 verify_add_remove_moderator_target(&self.target, &community)?;
101 #[tracing::instrument(skip_all)]
104 context: &Data<LemmyContext>,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
107 let community = self.get_community(context, request_counter).await?;
108 let remove_mod = self
110 .dereference(context, local_instance(context).await, request_counter)
113 let form = CommunityModeratorForm {
114 community_id: community.id,
115 person_id: remove_mod.id,
117 CommunityModerator::leave(context.pool(), &form).await?;
122 .dereference(context, local_instance(context).await, request_counter)
124 let form = ModAddCommunityForm {
125 mod_person_id: actor.id,
126 other_person_id: remove_mod.id,
127 community_id: community.id,
130 ModAddCommunity::create(context.pool(), &form).await?;
132 // TODO: send websocket notification about removed mod
137 #[async_trait::async_trait(?Send)]
138 impl GetCommunity for RemoveMod {
139 #[tracing::instrument(skip_all)]
140 async fn get_community(
142 context: &LemmyContext,
143 request_counter: &mut i32,
144 ) -> Result<ApubCommunity, LemmyError> {
145 get_community_from_moderators_url(&self.target, context, request_counter).await