2 fetcher::post_or_comment::PostOrComment,
3 objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
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;
18 #[skip_serializing_none]
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 #[serde(rename_all = "camelCase")]
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>,
27 pub(crate) cc: Vec<Url>,
28 pub(crate) content: String,
29 pub(crate) media_type: Option<MediaTypeHtml>,
30 pub(crate) source: SourceCompat,
31 pub(crate) in_reply_to: ObjectId<PostOrComment>,
32 pub(crate) published: Option<DateTime<FixedOffset>>,
33 pub(crate) updated: Option<DateTime<FixedOffset>>,
35 pub(crate) unparsed: Unparsed,
38 /// Pleroma puts a raw string in the source, so we have to handle it here for deserialization to work
39 #[derive(Clone, Debug, Deserialize, Serialize)]
40 #[serde(rename_all = "camelCase")]
42 pub(crate) enum SourceCompat {
48 pub(crate) async fn get_parents(
50 context: &LemmyContext,
51 request_counter: &mut i32,
52 ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
53 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
54 let parent = Box::pin(
57 .dereference(context, request_counter)
60 match parent.deref() {
61 PostOrComment::Post(p) => {
62 // Workaround because I cant figure out how to get the post out of the box (and we dont
63 // want to stackoverflow in a deep comment hierarchy).
65 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
66 Ok((post.into(), None))
68 PostOrComment::Comment(c) => {
69 let post_id = c.post_id;
70 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
71 Ok((post.into(), Some(c.id)))