]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
More federation compat (#1894)
[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::{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(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   None,
51   Lemmy(Source),
52   Pleroma(String),
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, 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 }
90
91 // For Pleroma/Mastodon compat. Unimplemented because its only used for sending.
92 #[async_trait::async_trait(?Send)]
93 impl ActivityHandler for Note {
94   type DataType = LemmyContext;
95   async fn verify(&self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
96     Err(anyhow!("Announce/Page can only be sent, not received").into())
97   }
98   async fn receive(self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
99     unimplemented!()
100   }
101 }