2 fetcher::post_or_comment::PostOrComment,
3 objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
6 use activitystreams::{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) 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, 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)))
91 // For Pleroma/Mastodon compat. Unimplemented because its only used for sending.
92 #[async_trait::async_trait(?Send)]
93 impl ActivityHandler for Note {
94 type DataType = LemmyContext;
95 async fn verify(&self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
96 Err(anyhow!("Announce/Page can only be sent, not received").into())
98 async fn receive(self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {