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