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