2 activities::{verify_is_public, verify_person_in_community},
3 fetcher::{object_id::ObjectId, post_or_comment::PostOrComment},
4 objects::{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,
30 pub(crate) attributed_to: ObjectId<ApubPerson>,
31 /// Indicates that the object is publicly readable. Unlike [`Post.to`], this one doesn't contain
32 /// the community ID, as it would be incompatible with Pleroma (and we can get the community from
33 /// the post in [`in_reply_to`]).
34 pub(crate) to: Vec<Url>,
35 pub(crate) content: String,
36 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 pub(crate) fn id_unchecked(&self) -> &Url {
58 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
59 verify_domains_match(&self.id, expected_domain)?;
63 pub(crate) async fn get_parents(
65 context: &LemmyContext,
66 request_counter: &mut i32,
67 ) -> Result<(ApubPost, Option<CommentId>), LemmyError> {
68 // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
69 let parent = Box::pin(
72 .dereference(context, request_counter)
75 match parent.deref() {
76 PostOrComment::Post(p) => {
77 // Workaround because I cant figure out how to get the post out of the box (and we dont
78 // want to stackoverflow in a deep comment hierarchy).
80 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
81 Ok((post.into(), None))
83 PostOrComment::Comment(c) => {
84 let post_id = c.post_id;
85 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
86 Ok((post.into(), Some(c.id)))
91 pub(crate) async fn verify(
93 context: &LemmyContext,
94 request_counter: &mut i32,
95 ) -> Result<(), LemmyError> {
96 let (post, _parent_comment_id) = self.get_parents(context, request_counter).await?;
97 let community_id = post.community_id;
98 let community: ApubCommunity = blocking(context.pool(), move |conn| {
99 Community::read(conn, community_id)
105 return Err(anyhow!("Post is locked").into());
107 verify_domains_match(self.attributed_to.inner(), &self.id)?;
108 verify_person_in_community(&self.attributed_to, &community, context, request_counter).await?;
109 verify_is_public(&self.to)?;