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_json::Value;
16 use serde_with::skip_serializing_none;
20 #[skip_serializing_none]
21 #[derive(Clone, Debug, Deserialize, Serialize)]
22 #[serde(rename_all = "camelCase")]
24 pub(crate) r#type: NoteType,
25 pub(crate) id: ObjectId<ApubComment>,
26 pub(crate) attributed_to: ObjectId<ApubPerson>,
27 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
28 pub(crate) to: Vec<Url>,
30 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
31 pub(crate) cc: Vec<Url>,
32 pub(crate) content: String,
33 pub(crate) media_type: Option<MediaTypeHtml>,
35 pub(crate) source: SourceCompat,
36 pub(crate) in_reply_to: ObjectId<PostOrComment>,
37 pub(crate) published: Option<DateTime<FixedOffset>>,
38 pub(crate) updated: Option<DateTime<FixedOffset>>,
40 pub(crate) tag: Vec<Mention>,
42 pub(crate) unparsed: Unparsed,
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")]
49 pub(crate) enum SourceCompat {
55 impl Default for SourceCompat {
56 fn default() -> Self {
62 pub(crate) async fn get_parents(
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(
71 .dereference(context, context.client(), request_counter)
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).
79 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
80 Ok((post.into(), None))
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)))