]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/community/update.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[lemmy.git] / crates / apub / src / protocol / activities / community / update.rs
1 use crate::{
2   activities::verify_community_matches,
3   local_instance,
4   objects::{community::ApubCommunity, person::ApubPerson},
5   protocol::{objects::group::Group, InCommunity},
6 };
7 use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
8 use activitystreams_kinds::activity::UpdateType;
9 use lemmy_utils::error::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::{Deserialize, Serialize};
12 use url::Url;
13
14 /// This activity is received from a remote community mod, and updates the description or other
15 /// fields of a local community.
16 #[derive(Clone, Debug, Deserialize, Serialize)]
17 #[serde(rename_all = "camelCase")]
18 pub struct UpdateCommunity {
19   pub(crate) actor: ObjectId<ApubPerson>,
20   #[serde(deserialize_with = "deserialize_one_or_many")]
21   pub(crate) to: Vec<Url>,
22   // TODO: would be nice to use a separate struct here, which only contains the fields updated here
23   pub(crate) object: Box<Group>,
24   #[serde(deserialize_with = "deserialize_one_or_many")]
25   pub(crate) cc: Vec<Url>,
26   #[serde(rename = "type")]
27   pub(crate) kind: UpdateType,
28   pub(crate) id: Url,
29   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
30 }
31
32 #[async_trait::async_trait(?Send)]
33 impl InCommunity for UpdateCommunity {
34   async fn community(
35     &self,
36     context: &LemmyContext,
37     request_counter: &mut i32,
38   ) -> Result<ApubCommunity, LemmyError> {
39     let object_community: ApubCommunity = ObjectId::new(self.object.id.clone())
40       .dereference(context, local_instance(context).await, request_counter)
41       .await?;
42     if let Some(audience) = &self.audience {
43       let audience = audience
44         .dereference(context, local_instance(context).await, request_counter)
45         .await?;
46       verify_community_matches(&audience, object_community.id)?;
47       Ok(audience)
48     } else {
49       Ok(object_community)
50     }
51   }
52 }