2 fetcher::post_or_comment::PostOrComment,
3 objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
6 use activitystreams::{link::Mention, object::kind::NoteType, unparsed::Unparsed};
8 use chrono::{DateTime, FixedOffset};
9 use lemmy_api_common::blocking;
13 traits::ActivityHandler,
14 values::MediaTypeHtml,
16 use lemmy_db_schema::{newtypes::CommentId, source::post::Post, traits::Crud};
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::LemmyContext;
19 use serde::{Deserialize, Serialize};
20 use serde_with::skip_serializing_none;
24 #[skip_serializing_none]
25 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[serde(rename_all = "camelCase")]
28 pub(crate) r#type: NoteType,
29 pub(crate) id: ObjectId<ApubComment>,
30 pub(crate) attributed_to: ObjectId<ApubPerson>,
31 pub(crate) to: Vec<Url>,
33 pub(crate) cc: Vec<Url>,
34 pub(crate) content: String,
35 pub(crate) media_type: Option<MediaTypeHtml>,
37 pub(crate) source: SourceCompat,
38 pub(crate) in_reply_to: ObjectId<PostOrComment>,
39 pub(crate) published: Option<DateTime<FixedOffset>>,
40 pub(crate) updated: Option<DateTime<FixedOffset>>,
42 pub(crate) tag: Vec<Mention>,
44 pub(crate) unparsed: Unparsed,
47 /// Pleroma puts a raw string in the source, so we have to handle it here for deserialization to work
48 #[derive(Clone, Debug, Deserialize, Serialize)]
49 #[serde(rename_all = "camelCase")]
51 pub(crate) enum SourceCompat {
57 impl Default for SourceCompat {
58 fn default() -> Self {
64 pub(crate) async fn get_parents(
66 context: &LemmyContext,
67 request_counter: &mut i32,
68 ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
69 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
70 let parent = Box::pin(
73 .dereference(context, request_counter)
76 match parent.deref() {
77 PostOrComment::Post(p) => {
78 // Workaround because I cant figure out how to get the post out of the box (and we dont
79 // want to stackoverflow in a deep comment hierarchy).
81 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
82 Ok((post.into(), None))
84 PostOrComment::Comment(c) => {
85 let post_id = c.post_id;
86 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
87 Ok((post.into(), Some(c.id)))
93 // For Pleroma/Mastodon compat. Unimplemented because its only used for sending.
94 #[async_trait::async_trait(?Send)]
95 impl ActivityHandler for Note {
96 type DataType = LemmyContext;
97 async fn verify(&self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
98 Err(anyhow!("Announce/Page can only be sent, not received").into())
100 async fn receive(self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {