3 deletion::{delete::receive_remove_action, verify_delete_activity},
5 verify_add_remove_moderator_target,
7 verify_person_in_community,
9 fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
12 use activitystreams::{activity::kind::RemoveType, base::AnyBase};
13 use lemmy_api_common::blocking;
14 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
15 use lemmy_db_queries::Joinable;
16 use lemmy_db_schema::source::community::{CommunityModerator, CommunityModeratorForm};
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::LemmyContext;
21 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
22 #[serde(rename_all = "camelCase")]
23 pub struct RemoveMod {
25 pub(in crate::activities::removal) object: Url,
27 #[serde(rename = "type")]
29 // if target is set, this is means remove mod from community
30 pub(in crate::activities::removal) target: Option<Url>,
32 common: ActivityCommonFields,
35 #[async_trait::async_trait(?Send)]
36 impl ActivityHandler for RemoveMod {
39 context: &LemmyContext,
40 request_counter: &mut i32,
41 ) -> Result<(), LemmyError> {
42 verify_activity(self.common())?;
43 if let Some(target) = &self.target {
44 verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?;
45 verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
46 verify_add_remove_moderator_target(target, self.cc[0].clone())?;
48 verify_delete_activity(
63 context: &LemmyContext,
64 request_counter: &mut i32,
65 ) -> Result<(), LemmyError> {
66 if self.target.is_some() {
68 get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter).await?;
70 get_or_fetch_and_upsert_person(&self.object, context, request_counter).await?;
72 let form = CommunityModeratorForm {
73 community_id: community.id,
74 person_id: remove_mod.id,
76 blocking(context.pool(), move |conn| {
77 CommunityModerator::leave(conn, &form)
80 let anybase = AnyBase::from_arbitrary_json(serde_json::to_string(&self)?)?;
82 .send_announce(anybase, Some(self.object.clone()), context)
84 // TODO: send websocket notification about removed mod
87 receive_remove_action(
98 fn common(&self) -> &ActivityCommonFields {