2 activities::verify_community_matches,
3 fetcher::post_or_comment::PostOrComment,
4 mentions::MentionOrValue,
5 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
6 protocol::{objects::LanguageTag, InCommunity, Source},
8 use activitypub_federation::{
10 fetch::object_id::ObjectId,
11 kinds::object::NoteType,
13 helpers::{deserialize_one_or_many, deserialize_skip_error},
14 values::MediaTypeMarkdownOrHtml,
17 use chrono::{DateTime, FixedOffset};
18 use lemmy_api_common::context::LemmyContext;
19 use lemmy_db_schema::{
20 source::{community::Community, post::Post},
23 use lemmy_utils::error::LemmyError;
24 use serde::{Deserialize, Serialize};
25 use serde_with::skip_serializing_none;
29 #[skip_serializing_none]
30 #[derive(Clone, Debug, Deserialize, Serialize)]
31 #[serde(rename_all = "camelCase")]
33 pub(crate) r#type: NoteType,
34 pub(crate) id: ObjectId<ApubComment>,
35 pub(crate) attributed_to: ObjectId<ApubPerson>,
36 #[serde(deserialize_with = "deserialize_one_or_many")]
37 pub(crate) to: Vec<Url>,
38 #[serde(deserialize_with = "deserialize_one_or_many", default)]
39 pub(crate) cc: Vec<Url>,
40 pub(crate) content: String,
41 pub(crate) in_reply_to: ObjectId<PostOrComment>,
43 pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
44 #[serde(deserialize_with = "deserialize_skip_error", default)]
45 pub(crate) source: Option<Source>,
46 pub(crate) published: Option<DateTime<FixedOffset>>,
47 pub(crate) updated: Option<DateTime<FixedOffset>>,
49 pub(crate) tag: Vec<MentionOrValue>,
51 pub(crate) distinguished: Option<bool>,
52 pub(crate) language: Option<LanguageTag>,
53 pub(crate) audience: Option<ObjectId<ApubCommunity>>,
57 pub(crate) async fn get_parents(
59 context: &Data<LemmyContext>,
60 ) -> Result<(ApubPost, Option<ApubComment>), LemmyError> {
61 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
62 let parent = Box::pin(self.in_reply_to.dereference(context).await?);
63 match parent.deref() {
64 PostOrComment::Post(p) => Ok((p.clone(), None)),
65 PostOrComment::Comment(c) => {
66 let post_id = c.post_id;
67 let post = Post::read(context.pool(), post_id).await?;
68 Ok((post.into(), Some(c.clone())))
74 #[async_trait::async_trait]
75 impl InCommunity for Note {
76 async fn community(&self, context: &Data<LemmyContext>) -> Result<ApubCommunity, LemmyError> {
77 let (post, _) = self.get_parents(context).await?;
78 let community = Community::read(context.pool(), post.community_id).await?;
79 if let Some(audience) = &self.audience {
80 verify_community_matches(audience, community.actor_id.clone())?;