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