]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
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},
7 };
8 use activitypub_federation::{
9   config::Data,
10   fetch::object_id::ObjectId,
11   kinds::object::NoteType,
12   protocol::{
13     helpers::{deserialize_one_or_many, deserialize_skip_error},
14     values::MediaTypeMarkdownOrHtml,
15   },
16 };
17 use chrono::{DateTime, FixedOffset};
18 use lemmy_api_common::context::LemmyContext;
19 use lemmy_db_schema::{
20   source::{community::Community, post::Post},
21   traits::Crud,
22 };
23 use lemmy_utils::error::LemmyError;
24 use serde::{Deserialize, Serialize};
25 use serde_with::skip_serializing_none;
26 use std::ops::Deref;
27 use url::Url;
28
29 #[skip_serializing_none]
30 #[derive(Clone, Debug, Deserialize, Serialize)]
31 #[serde(rename_all = "camelCase")]
32 pub struct Note {
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>,
42
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>>,
48   #[serde(default)]
49   pub(crate) tag: Vec<MentionOrValue>,
50   // lemmy extension
51   pub(crate) distinguished: Option<bool>,
52   pub(crate) language: Option<LanguageTag>,
53   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
54 }
55
56 impl Note {
57   pub(crate) async fn get_parents(
58     &self,
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(&mut context.pool(), post_id).await?;
68         Ok((post.into(), Some(c.clone())))
69       }
70     }
71   }
72 }
73
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(&mut context.pool(), post.community_id).await?;
79     if let Some(audience) = &self.audience {
80       verify_community_matches(audience, community.actor_id.clone())?;
81     }
82     Ok(community.into())
83   }
84 }