2 fetcher::post_or_comment::PostOrComment,
4 objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
5 protocol::{Source, Unparsed},
7 use activitystreams_kinds::object::NoteType;
8 use chrono::{DateTime, FixedOffset};
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{object_id::ObjectId, values::MediaTypeHtml};
11 use lemmy_db_schema::{newtypes::CommentId, source::post::Post, traits::Crud};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
15 use serde_with::skip_serializing_none;
19 #[skip_serializing_none]
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
23 pub(crate) r#type: NoteType,
24 pub(crate) id: ObjectId<ApubComment>,
25 pub(crate) attributed_to: ObjectId<ApubPerson>,
26 pub(crate) to: Vec<Url>,
28 pub(crate) cc: Vec<Url>,
29 pub(crate) content: String,
30 pub(crate) media_type: Option<MediaTypeHtml>,
32 pub(crate) source: SourceCompat,
33 pub(crate) in_reply_to: ObjectId<PostOrComment>,
34 pub(crate) published: Option<DateTime<FixedOffset>>,
35 pub(crate) updated: Option<DateTime<FixedOffset>>,
37 pub(crate) tag: Vec<Mention>,
39 pub(crate) unparsed: Unparsed,
42 /// Pleroma puts a raw string in the source, so we have to handle it here for deserialization to work
43 #[derive(Clone, Debug, Deserialize, Serialize)]
44 #[serde(rename_all = "camelCase")]
46 pub(crate) enum SourceCompat {
52 impl Default for SourceCompat {
53 fn default() -> Self {
59 pub(crate) async fn get_parents(
61 context: &LemmyContext,
62 request_counter: &mut i32,
63 ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
64 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
65 let parent = Box::pin(
68 .dereference(context, request_counter)
71 match parent.deref() {
72 PostOrComment::Post(p) => {
73 // Workaround because I cant figure out how to get the post out of the box (and we dont
74 // want to stackoverflow in a deep comment hierarchy).
76 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
77 Ok((post.into(), None))
79 PostOrComment::Comment(c) => {
80 let post_id = c.post_id;
81 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
82 Ok((post.into(), Some(c.id)))