2 fetcher::post_or_comment::PostOrComment,
4 mentions::MentionOrValue,
5 objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
8 use activitypub_federation::{
9 core::object_id::ObjectId,
11 helpers::{deserialize_one_or_many, deserialize_skip_error},
12 values::MediaTypeMarkdownOrHtml,
15 use activitystreams_kinds::object::NoteType;
16 use chrono::{DateTime, FixedOffset};
17 use lemmy_api_common::utils::blocking;
18 use lemmy_db_schema::{newtypes::CommentId, source::post::Post, traits::Crud};
19 use lemmy_utils::error::LemmyError;
20 use lemmy_websocket::LemmyContext;
21 use serde::{Deserialize, Serialize};
22 use serde_with::skip_serializing_none;
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
30 pub(crate) r#type: NoteType,
31 pub(crate) id: ObjectId<ApubComment>,
32 pub(crate) attributed_to: ObjectId<ApubPerson>,
33 #[serde(deserialize_with = "deserialize_one_or_many")]
34 pub(crate) to: Vec<Url>,
35 #[serde(deserialize_with = "deserialize_one_or_many", default)]
36 pub(crate) cc: Vec<Url>,
37 pub(crate) content: String,
38 pub(crate) in_reply_to: ObjectId<PostOrComment>,
40 pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
41 #[serde(deserialize_with = "deserialize_skip_error", default)]
42 pub(crate) source: Option<Source>,
43 pub(crate) published: Option<DateTime<FixedOffset>>,
44 pub(crate) updated: Option<DateTime<FixedOffset>>,
46 pub(crate) tag: Vec<MentionOrValue>,
50 pub(crate) async fn get_parents(
52 context: &LemmyContext,
53 request_counter: &mut i32,
54 ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
55 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
56 let parent = Box::pin(
59 .dereference::<LemmyError>(context, local_instance(context), request_counter)
62 match parent.deref() {
63 PostOrComment::Post(p) => {
64 // Workaround because I cant figure out how to get the post out of the box (and we dont
65 // want to stackoverflow in a deep comment hierarchy).
67 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
68 Ok((post.into(), None))
70 PostOrComment::Comment(c) => {
71 let post_id = c.post_id;
72 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
73 Ok((post.into(), Some(c.id)))