]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/block/block_user.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[lemmy.git] / crates / apub / src / protocol / activities / block / block_user.rs
1 use crate::{
2   activities::{block::SiteOrCommunity, 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::BlockType;
9 use anyhow::anyhow;
10 use chrono::{DateTime, FixedOffset};
11 use lemmy_utils::error::LemmyError;
12 use lemmy_websocket::LemmyContext;
13 use serde::{Deserialize, Serialize};
14 use serde_with::skip_serializing_none;
15 use url::Url;
16
17 #[skip_serializing_none]
18 #[derive(Clone, Debug, Deserialize, Serialize)]
19 #[serde(rename_all = "camelCase")]
20 pub struct BlockUser {
21   pub(crate) actor: ObjectId<ApubPerson>,
22   #[serde(deserialize_with = "deserialize_one_or_many")]
23   pub(crate) to: Vec<Url>,
24   pub(crate) object: ObjectId<ApubPerson>,
25   #[serde(deserialize_with = "deserialize_one_or_many")]
26   pub(crate) cc: Vec<Url>,
27   pub(crate) target: ObjectId<SiteOrCommunity>,
28   #[serde(rename = "type")]
29   pub(crate) kind: BlockType,
30   pub(crate) id: Url,
31   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
32
33   /// Quick and dirty solution.
34   /// TODO: send a separate Delete activity instead
35   pub(crate) remove_data: Option<bool>,
36   /// block reason, written to mod log
37   pub(crate) summary: Option<String>,
38   pub(crate) expires: Option<DateTime<FixedOffset>>,
39 }
40
41 #[async_trait::async_trait(?Send)]
42 impl InCommunity for BlockUser {
43   async fn community(
44     &self,
45     context: &LemmyContext,
46     request_counter: &mut i32,
47   ) -> Result<ApubCommunity, LemmyError> {
48     let target = self
49       .target
50       .dereference(context, local_instance(context).await, request_counter)
51       .await?;
52     let target_community = match target {
53       SiteOrCommunity::Community(c) => c,
54       SiteOrCommunity::Site(_) => return Err(anyhow!("activity is not in community").into()),
55     };
56     if let Some(audience) = &self.audience {
57       let audience = audience
58         .dereference(context, local_instance(context).await, request_counter)
59         .await?;
60       verify_community_matches(&audience, target_community.id)?;
61       Ok(audience)
62     } else {
63       Ok(target_community)
64     }
65   }
66 }