4 announce::{AnnouncableActivities, GetCommunity},
5 get_community_from_moderators_url,
10 verify_add_remove_moderator_target,
13 verify_person_in_community,
15 fetcher::object_id::ObjectId,
16 generate_moderators_url,
17 objects::{community::ApubCommunity, person::ApubPerson},
19 use activitystreams::{activity::kind::RemoveType, public, unparsed::Unparsed};
20 use lemmy_api_common::blocking;
23 traits::{ActivityFields, ActivityHandler, ActorType},
25 use lemmy_db_schema::{
26 source::community::{CommunityModerator, CommunityModeratorForm},
29 use lemmy_utils::LemmyError;
30 use lemmy_websocket::LemmyContext;
31 use serde::{Deserialize, Serialize};
34 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
35 #[serde(rename_all = "camelCase")]
36 pub struct RemoveMod {
37 actor: ObjectId<ApubPerson>,
39 pub(in crate::activities) object: ObjectId<ApubPerson>,
41 #[serde(rename = "type")]
43 pub(in crate::activities) target: Url,
51 community: &ApubCommunity,
52 removed_mod: &ApubPerson,
54 context: &LemmyContext,
55 ) -> Result<(), LemmyError> {
56 let id = generate_activity_id(
58 &context.settings().get_protocol_and_hostname(),
60 let remove = RemoveMod {
61 actor: ObjectId::new(actor.actor_id()),
63 object: ObjectId::new(removed_mod.actor_id()),
64 target: generate_moderators_url(&community.actor_id)?.into(),
66 cc: vec![community.actor_id()],
67 kind: RemoveType::Remove,
68 unparsed: Default::default(),
71 let activity = AnnouncableActivities::RemoveMod(remove);
72 let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()];
73 send_to_community(activity, &id, actor, community, inboxes, context).await
77 #[async_trait::async_trait(?Send)]
78 impl ActivityHandler for RemoveMod {
79 type DataType = LemmyContext;
82 context: &Data<LemmyContext>,
83 request_counter: &mut i32,
84 ) -> Result<(), LemmyError> {
85 verify_is_public(&self.to)?;
86 verify_activity(self, &context.settings())?;
87 let community = self.get_community(context, request_counter).await?;
88 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
89 verify_mod_action(&self.actor, &community, context, request_counter).await?;
90 verify_add_remove_moderator_target(&self.target, &community)?;
96 context: &Data<LemmyContext>,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
99 let community = self.get_community(context, request_counter).await?;
100 let remove_mod = self.object.dereference(context, request_counter).await?;
102 let form = CommunityModeratorForm {
103 community_id: community.id,
104 person_id: remove_mod.id,
106 blocking(context.pool(), move |conn| {
107 CommunityModerator::leave(conn, &form)
110 // TODO: send websocket notification about removed mod
115 #[async_trait::async_trait(?Send)]
116 impl GetCommunity for RemoveMod {
117 async fn get_community(
119 context: &LemmyContext,
120 request_counter: &mut i32,
121 ) -> Result<ApubCommunity, LemmyError> {
122 get_community_from_moderators_url(&self.target, context, request_counter).await