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