2 activities::verify_community_matches,
3 fetcher::post_or_comment::PostOrComment,
5 mentions::MentionOrValue,
6 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
7 protocol::{objects::LanguageTag, InCommunity, Source},
9 use activitypub_federation::{
10 core::object_id::ObjectId,
12 helpers::{deserialize_one_or_many, deserialize_skip_error},
13 values::MediaTypeMarkdownOrHtml,
16 use activitystreams_kinds::object::NoteType;
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: &LemmyContext,
60 request_counter: &mut i32,
61 ) -> Result<(ApubPost, Option<ApubComment>), LemmyError> {
62 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
63 let parent = Box::pin(
66 .dereference(context, local_instance(context).await, request_counter)
69 match parent.deref() {
70 PostOrComment::Post(p) => {
71 let post = p.deref().clone();
74 PostOrComment::Comment(c) => {
75 let post_id = c.post_id;
76 let post = Post::read(context.pool(), post_id).await?;
77 let comment = c.deref().clone();
78 Ok((post.into(), Some(comment)))
84 #[async_trait::async_trait(?Send)]
85 impl InCommunity for Note {
88 context: &LemmyContext,
89 request_counter: &mut i32,
90 ) -> Result<ApubCommunity, LemmyError> {
91 let (post, _) = self.get_parents(context, request_counter).await?;
92 let community_id = post.community_id;
93 if let Some(audience) = &self.audience {
94 let audience = audience
95 .dereference(context, local_instance(context).await, request_counter)
97 verify_community_matches(&audience, community_id)?;
100 Ok(Community::read(context.pool(), community_id).await?.into())