]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/create_or_update/note.rs
Implement separate mod activities for feature, lock post (#2716)
[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::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
8 use lemmy_api_common::context::LemmyContext;
9 use lemmy_db_schema::{source::community::Community, traits::Crud};
10 use lemmy_utils::error::LemmyError;
11 use serde::{Deserialize, Serialize};
12 use url::Url;
13
14 #[derive(Clone, Debug, Deserialize, Serialize)]
15 #[serde(rename_all = "camelCase")]
16 pub struct CreateOrUpdateNote {
17   pub(crate) actor: ObjectId<ApubPerson>,
18   #[serde(deserialize_with = "deserialize_one_or_many")]
19   pub(crate) to: Vec<Url>,
20   pub(crate) object: Note,
21   #[serde(deserialize_with = "deserialize_one_or_many")]
22   pub(crate) cc: Vec<Url>,
23   #[serde(default)]
24   pub(crate) tag: Vec<MentionOrValue>,
25   #[serde(rename = "type")]
26   pub(crate) kind: CreateOrUpdateType,
27   pub(crate) id: Url,
28   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
29 }
30
31 #[async_trait::async_trait(?Send)]
32 impl InCommunity for CreateOrUpdateNote {
33   async fn community(
34     &self,
35     context: &LemmyContext,
36     request_counter: &mut i32,
37   ) -> Result<ApubCommunity, LemmyError> {
38     let post = self.object.get_parents(context, request_counter).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 }