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