]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Move ObjectId to library
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
2   activities::{verify_is_public, verify_person_in_community},
3   fetcher::post_or_comment::PostOrComment,
4   objects::{comment::ApubComment, 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::{object_id::ObjectId, 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: 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>>,
38   #[serde(flatten)]
39   pub(crate) unparsed: Unparsed,
40 }
41
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")]
45 #[serde(untagged)]
46 pub(crate) enum SourceCompat {
47   Lemmy(Source),
48   Pleroma(String),
49 }
50
51 impl Note {
52   pub(crate) async fn get_parents(
53     &self,
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(
59       self
60         .in_reply_to
61         .dereference(context, request_counter)
62         .await?,
63     );
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).
68         let post_id = p.id;
69         let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
70         Ok((post.into(), None))
71       }
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)))
76       }
77     }
78   }
79
80   pub(crate) async fn verify(
81     &self,
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)
89     })
90     .await??
91     .into();
92
93     if post.locked {
94       return Err(anyhow!("Post is locked").into());
95     }
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)?;
99     Ok(())
100   }
101 }