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