]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
75fecac799bb371adb0c3abcdb9fe6401e5c44f9
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
2   fetcher::post_or_comment::PostOrComment,
3   local_instance,
4   mentions::MentionOrValue,
5   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
6   protocol::Source,
7 };
8 use activitypub_federation::{
9   core::object_id::ObjectId,
10   deser::{
11     helpers::{deserialize_one_or_many, deserialize_skip_error},
12     values::MediaTypeMarkdownOrHtml,
13   },
14 };
15 use activitystreams_kinds::object::NoteType;
16 use chrono::{DateTime, FixedOffset};
17 use lemmy_api_common::utils::blocking;
18 use lemmy_db_schema::{source::post::Post, traits::Crud};
19 use lemmy_utils::error::LemmyError;
20 use lemmy_websocket::LemmyContext;
21 use serde::{Deserialize, Serialize};
22 use serde_with::skip_serializing_none;
23 use std::ops::Deref;
24 use url::Url;
25
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
29 pub struct Note {
30   pub(crate) r#type: NoteType,
31   pub(crate) id: ObjectId<ApubComment>,
32   pub(crate) attributed_to: ObjectId<ApubPerson>,
33   #[serde(deserialize_with = "deserialize_one_or_many")]
34   pub(crate) to: Vec<Url>,
35   #[serde(deserialize_with = "deserialize_one_or_many", default)]
36   pub(crate) cc: Vec<Url>,
37   pub(crate) content: String,
38   pub(crate) in_reply_to: ObjectId<PostOrComment>,
39
40   pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
41   #[serde(deserialize_with = "deserialize_skip_error", default)]
42   pub(crate) source: Option<Source>,
43   pub(crate) published: Option<DateTime<FixedOffset>>,
44   pub(crate) updated: Option<DateTime<FixedOffset>>,
45   #[serde(default)]
46   pub(crate) tag: Vec<MentionOrValue>,
47   // lemmy extension
48   pub(crate) distinguished: Option<bool>,
49 }
50
51 impl Note {
52   pub(crate) async fn get_parents(
53     &self,
54     context: &LemmyContext,
55     request_counter: &mut i32,
56   ) -> Result<(ApubPost, Option<ApubComment>), LemmyError> {
57     // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
58     let parent = Box::pin(
59       self
60         .in_reply_to
61         .dereference(context, local_instance(context), request_counter)
62         .await?,
63     );
64     match parent.deref() {
65       PostOrComment::Post(p) => {
66         let post = p.deref().to_owned();
67         Ok((post, None))
68       }
69       PostOrComment::Comment(c) => {
70         let post_id = c.post_id;
71         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
72         let comment = c.deref().to_owned();
73         Ok((post.into(), Some(comment)))
74       }
75     }
76   }
77 }