]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/community/remove_mod.rs
ce46fb920f6cba3b8aaf22d0992514ee8a3955a7
[lemmy.git] / crates / apub / src / protocol / activities / community / remove_mod.rs
1 use crate::{
2   activities::{community::get_community_from_moderators_url, verify_community_matches},
3   local_instance,
4   objects::{community::ApubCommunity, person::ApubPerson},
5   protocol::InCommunity,
6 };
7 use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
8 use activitystreams_kinds::activity::RemoveType;
9 use lemmy_api_common::context::LemmyContext;
10 use lemmy_utils::error::LemmyError;
11 use serde::{Deserialize, Serialize};
12 use url::Url;
13
14 #[derive(Clone, Debug, Deserialize, Serialize)]
15 #[serde(rename_all = "camelCase")]
16 pub struct RemoveMod {
17   pub(crate) actor: ObjectId<ApubPerson>,
18   #[serde(deserialize_with = "deserialize_one_or_many")]
19   pub(crate) to: Vec<Url>,
20   pub(crate) object: ObjectId<ApubPerson>,
21   #[serde(deserialize_with = "deserialize_one_or_many")]
22   pub(crate) cc: Vec<Url>,
23   #[serde(rename = "type")]
24   pub(crate) kind: RemoveType,
25   pub(crate) target: Url,
26   pub(crate) id: Url,
27   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
28 }
29
30 #[async_trait::async_trait(?Send)]
31 impl InCommunity for RemoveMod {
32   async fn community(
33     &self,
34     context: &LemmyContext,
35     request_counter: &mut i32,
36   ) -> Result<ApubCommunity, LemmyError> {
37     let mod_community =
38       get_community_from_moderators_url(&self.target, context, request_counter).await?;
39     if let Some(audience) = &self.audience {
40       let audience = audience
41         .dereference(context, local_instance(context).await, request_counter)
42         .await?;
43       verify_community_matches(&audience, mod_community.id)?;
44       Ok(audience)
45     } else {
46       Ok(mod_community)
47     }
48   }
49 }