]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Accept comments with hashtags from Friendica (#2236)
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
2   fetcher::post_or_comment::PostOrComment,
3   mentions::MentionOrValue,
4   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
5   protocol::Source,
6 };
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;
16 use std::ops::Deref;
17 use url::Url;
18
19 #[skip_serializing_none]
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
22 pub struct Note {
23   pub(crate) r#type: NoteType,
24   pub(crate) id: ObjectId<ApubComment>,
25   pub(crate) attributed_to: ObjectId<ApubPerson>,
26   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
27   pub(crate) to: Vec<Url>,
28   #[serde(default)]
29   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
30   pub(crate) cc: Vec<Url>,
31   pub(crate) content: String,
32   pub(crate) in_reply_to: ObjectId<PostOrComment>,
33
34   pub(crate) media_type: Option<MediaTypeHtml>,
35   #[serde(default)]
36   #[serde(deserialize_with = "crate::deserialize_skip_error")]
37   pub(crate) source: Option<Source>,
38   pub(crate) published: Option<DateTime<FixedOffset>>,
39   pub(crate) updated: Option<DateTime<FixedOffset>>,
40   #[serde(default)]
41   pub(crate) tag: Vec<MentionOrValue>,
42 }
43
44 impl Note {
45   pub(crate) async fn get_parents(
46     &self,
47     context: &LemmyContext,
48     request_counter: &mut i32,
49   ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
50     // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
51     let parent = Box::pin(
52       self
53         .in_reply_to
54         .dereference(context, context.client(), request_counter)
55         .await?,
56     );
57     match parent.deref() {
58       PostOrComment::Post(p) => {
59         // Workaround because I cant figure out how to get the post out of the box (and we dont
60         // want to stackoverflow in a deep comment hierarchy).
61         let post_id = p.id;
62         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
63         Ok((post.into(), None))
64       }
65       PostOrComment::Comment(c) => {
66         let post_id = c.post_id;
67         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
68         Ok((post.into(), Some(c.id)))
69       }
70     }
71   }
72 }