2 activities::{verify_is_public, verify_person_in_community},
3 fetcher::{object_id::ObjectId, post_or_comment::PostOrComment},
4 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
7 use activitystreams::{object::kind::NoteType, unparsed::Unparsed};
9 use chrono::{DateTime, FixedOffset};
10 use lemmy_api_common::blocking;
11 use lemmy_apub_lib::{values::MediaTypeHtml, verify::verify_domains_match};
12 use lemmy_db_schema::{
14 source::{community::Community, post::Post},
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>,
32 pub(crate) content: String,
33 pub(crate) media_type: Option<MediaTypeHtml>,
34 pub(crate) source: SourceCompat,
35 pub(crate) in_reply_to: ObjectId<PostOrComment>,
36 pub(crate) published: Option<DateTime<FixedOffset>>,
37 pub(crate) updated: Option<DateTime<FixedOffset>>,
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 pub(crate) async fn get_parents(
54 context: &LemmyContext,
55 request_counter: &mut i32,
56 ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
57 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
58 let parent = Box::pin(
61 .dereference(context, request_counter)
64 match parent.deref() {
65 PostOrComment::Post(p) => {
66 // Workaround because I cant figure out how to get the post out of the box (and we dont
67 // want to stackoverflow in a deep comment hierarchy).
69 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
70 Ok((post.into(), None))
72 PostOrComment::Comment(c) => {
73 let post_id = c.post_id;
74 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
75 Ok((post.into(), Some(c.id)))
80 pub(crate) async fn verify(
82 context: &LemmyContext,
83 request_counter: &mut i32,
84 ) -> Result<(), LemmyError> {
85 let (post, _parent_comment_id) = self.get_parents(context, request_counter).await?;
86 let community_id = post.community_id;
87 let community: ApubCommunity = blocking(context.pool(), move |conn| {
88 Community::read(conn, community_id)
94 return Err(anyhow!("Post is locked").into());
96 verify_domains_match(self.attributed_to.inner(), self.id.inner())?;
97 verify_person_in_community(&self.attributed_to, &community, context, request_counter).await?;
98 verify_is_public(&self.to)?;