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::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
22 use activitystreams_kinds::{activity::RemoveType, public};
23 use lemmy_api_common::utils::blocking;
24 use lemmy_db_schema::{
26 community::{CommunityModerator, CommunityModeratorForm},
27 moderator::{ModAddCommunity, ModAddCommunityForm},
29 traits::{Crud, Joinable},
31 use lemmy_utils::error::LemmyError;
32 use lemmy_websocket::LemmyContext;
36 #[tracing::instrument(skip_all)]
38 community: &ApubCommunity,
39 removed_mod: &ApubPerson,
41 context: &LemmyContext,
42 ) -> Result<(), LemmyError> {
43 let id = generate_activity_id(
45 &context.settings().get_protocol_and_hostname(),
47 let remove = RemoveMod {
48 actor: ObjectId::new(actor.actor_id()),
50 object: ObjectId::new(removed_mod.actor_id()),
51 target: generate_moderators_url(&community.actor_id)?.into(),
53 cc: vec![community.actor_id()],
54 kind: RemoveType::Remove,
55 unparsed: Default::default(),
58 let activity = AnnouncableActivities::RemoveMod(remove);
59 let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()];
60 send_activity_in_community(activity, &id, actor, community, inboxes, context).await
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for RemoveMod {
66 type DataType = LemmyContext;
67 type Error = LemmyError;
69 fn id(&self) -> &Url {
73 fn actor(&self) -> &Url {
77 #[tracing::instrument(skip_all)]
80 context: &Data<LemmyContext>,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
83 verify_is_public(&self.to, &self.cc)?;
84 let community = self.get_community(context, request_counter).await?;
85 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
94 verify_add_remove_moderator_target(&self.target, &community)?;
98 #[tracing::instrument(skip_all)]
101 context: &Data<LemmyContext>,
102 request_counter: &mut i32,
103 ) -> Result<(), LemmyError> {
104 let community = self.get_community(context, request_counter).await?;
105 let remove_mod = self
107 .dereference::<LemmyError>(context, local_instance(context), request_counter)
110 let form = CommunityModeratorForm {
111 community_id: community.id,
112 person_id: remove_mod.id,
114 blocking(context.pool(), move |conn| {
115 CommunityModerator::leave(conn, &form)
122 .dereference::<LemmyError>(context, local_instance(context), 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 blocking(context.pool(), move |conn| {
131 ModAddCommunity::create(conn, &form)
135 // TODO: send websocket notification about removed mod
140 #[async_trait::async_trait(?Send)]
141 impl GetCommunity for RemoveMod {
142 #[tracing::instrument(skip_all)]
143 async fn get_community(
145 context: &LemmyContext,
146 request_counter: &mut i32,
147 ) -> Result<ApubCommunity, LemmyError> {
148 get_community_from_moderators_url(&self.target, context, request_counter).await