]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Merge pull request #1874 from LemmyNet/protocol-testing
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
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},
5   protocol::Source,
6 };
7 use activitystreams::{object::kind::NoteType, unparsed::Unparsed};
8 use anyhow::anyhow;
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::{
13   newtypes::CommentId,
14   source::{community::Community, post::Post},
15   traits::Crud,
16 };
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::LemmyContext;
19 use serde::{Deserialize, Serialize};
20 use serde_with::skip_serializing_none;
21 use std::ops::Deref;
22 use url::Url;
23
24 #[skip_serializing_none]
25 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[serde(rename_all = "camelCase")]
27 pub struct Note {
28   pub(crate) r#type: NoteType,
29   pub(crate) id: Url,
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>>,
41   #[serde(flatten)]
42   pub(crate) unparsed: Unparsed,
43 }
44
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")]
48 #[serde(untagged)]
49 pub(crate) enum SourceCompat {
50   Lemmy(Source),
51   Pleroma(String),
52 }
53
54 impl Note {
55   pub(crate) fn id_unchecked(&self) -> &Url {
56     &self.id
57   }
58   pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
59     verify_domains_match(&self.id, expected_domain)?;
60     Ok(&self.id)
61   }
62
63   pub(crate) async fn get_parents(
64     &self,
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(
70       self
71         .in_reply_to
72         .dereference(context, request_counter)
73         .await?,
74     );
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).
79         let post_id = p.id;
80         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
81         Ok((post.into(), None))
82       }
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)))
87       }
88     }
89   }
90
91   pub(crate) async fn verify(
92     &self,
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)
100     })
101     .await??
102     .into();
103
104     if post.locked {
105       return Err(anyhow!("Post is locked").into());
106     }
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)?;
110     Ok(())
111   }
112 }