]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Create and Note always need to tag parent creator, for mastodon notifications
[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 anyhow::anyhow;
8 use chrono::{DateTime, FixedOffset};
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{
11   data::Data,
12   object_id::ObjectId,
13   traits::ActivityHandler,
14   values::MediaTypeHtml,
15 };
16 use lemmy_db_schema::{newtypes::CommentId, source::post::Post, traits::Crud};
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::LemmyContext;
19 use serde::{Deserialize, Serialize};
20 use serde_with::skip_serializing_none;
21 use std::ops::Deref;
22 use url::Url;
23
24 #[skip_serializing_none]
25 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[serde(rename_all = "camelCase")]
27 pub struct Note {
28   pub(crate) r#type: NoteType,
29   pub(crate) id: ObjectId<ApubComment>,
30   pub(crate) attributed_to: ObjectId<ApubPerson>,
31   pub(crate) to: Vec<Url>,
32   #[serde(default)]
33   pub(crate) cc: Vec<Url>,
34   pub(crate) content: String,
35   pub(crate) media_type: Option<MediaTypeHtml>,
36   #[serde(default)]
37   pub(crate) source: SourceCompat,
38   pub(crate) in_reply_to: ObjectId<PostOrComment>,
39   pub(crate) published: Option<DateTime<FixedOffset>>,
40   pub(crate) updated: Option<DateTime<FixedOffset>>,
41   #[serde(default)]
42   pub(crate) tag: Vec<Mention>,
43   #[serde(flatten)]
44   pub(crate) unparsed: Unparsed,
45 }
46
47 /// Pleroma puts a raw string in the source, so we have to handle it here for deserialization to work
48 #[derive(Clone, Debug, Deserialize, Serialize)]
49 #[serde(rename_all = "camelCase")]
50 #[serde(untagged)]
51 pub(crate) enum SourceCompat {
52   None,
53   Lemmy(Source),
54   Pleroma(String),
55 }
56
57 impl Default for SourceCompat {
58   fn default() -> Self {
59     SourceCompat::None
60   }
61 }
62
63 impl Note {
64   pub(crate) async fn get_parents(
65     &self,
66     context: &LemmyContext,
67     request_counter: &mut i32,
68   ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
69     // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
70     let parent = Box::pin(
71       self
72         .in_reply_to
73         .dereference(context, request_counter)
74         .await?,
75     );
76     match parent.deref() {
77       PostOrComment::Post(p) => {
78         // Workaround because I cant figure out how to get the post out of the box (and we dont
79         // want to stackoverflow in a deep comment hierarchy).
80         let post_id = p.id;
81         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
82         Ok((post.into(), None))
83       }
84       PostOrComment::Comment(c) => {
85         let post_id = c.post_id;
86         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
87         Ok((post.into(), Some(c.id)))
88       }
89     }
90   }
91 }
92
93 // For Pleroma/Mastodon compat. Unimplemented because its only used for sending.
94 #[async_trait::async_trait(?Send)]
95 impl ActivityHandler for Note {
96   type DataType = LemmyContext;
97   async fn verify(&self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
98     Err(anyhow!("Announce/Page can only be sent, not received").into())
99   }
100   async fn receive(self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
101     unimplemented!()
102   }
103 }