]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Replace activitystreams crate with activitystreams-kinds
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
2   fetcher::post_or_comment::PostOrComment,
3   mentions::Mention,
4   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
5   protocol::{Source, Unparsed},
6 };
7 use activitystreams_kinds::object::NoteType;
8 use chrono::{DateTime, FixedOffset};
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{object_id::ObjectId, values::MediaTypeHtml};
11 use lemmy_db_schema::{newtypes::CommentId, source::post::Post, traits::Crud};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
15 use serde_with::skip_serializing_none;
16 use std::ops::Deref;
17 use url::Url;
18
19 #[skip_serializing_none]
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
22 pub struct Note {
23   pub(crate) r#type: NoteType,
24   pub(crate) id: ObjectId<ApubComment>,
25   pub(crate) attributed_to: ObjectId<ApubPerson>,
26   pub(crate) to: Vec<Url>,
27   #[serde(default)]
28   pub(crate) cc: Vec<Url>,
29   pub(crate) content: String,
30   pub(crate) media_type: Option<MediaTypeHtml>,
31   #[serde(default)]
32   pub(crate) source: SourceCompat,
33   pub(crate) in_reply_to: ObjectId<PostOrComment>,
34   pub(crate) published: Option<DateTime<FixedOffset>>,
35   pub(crate) updated: Option<DateTime<FixedOffset>>,
36   #[serde(default)]
37   pub(crate) tag: Vec<Mention>,
38   #[serde(flatten)]
39   pub(crate) unparsed: Unparsed,
40 }
41
42 /// Pleroma puts a raw string in the source, so we have to handle it here for deserialization to work
43 #[derive(Clone, Debug, Deserialize, Serialize)]
44 #[serde(rename_all = "camelCase")]
45 #[serde(untagged)]
46 pub(crate) enum SourceCompat {
47   None,
48   Lemmy(Source),
49   Pleroma(String),
50 }
51
52 impl Default for SourceCompat {
53   fn default() -> Self {
54     SourceCompat::None
55   }
56 }
57
58 impl Note {
59   pub(crate) async fn get_parents(
60     &self,
61     context: &LemmyContext,
62     request_counter: &mut i32,
63   ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
64     // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
65     let parent = Box::pin(
66       self
67         .in_reply_to
68         .dereference(context, request_counter)
69         .await?,
70     );
71     match parent.deref() {
72       PostOrComment::Post(p) => {
73         // Workaround because I cant figure out how to get the post out of the box (and we dont
74         // want to stackoverflow in a deep comment hierarchy).
75         let post_id = p.id;
76         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
77         Ok((post.into(), None))
78       }
79       PostOrComment::Comment(c) => {
80         let post_id = c.post_id;
81         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
82         Ok((post.into(), Some(c.id)))
83       }
84     }
85   }
86 }