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