2 objects::{comment::ApubComment, community::ApubCommunity, post::ApubPost},
4 objects::{note::Note, page::Page},
8 use activitypub_federation::traits::ApubObject;
9 use chrono::NaiveDateTime;
10 use lemmy_db_schema::{
11 source::{community::Community, post::Post},
14 use lemmy_utils::error::LemmyError;
15 use lemmy_websocket::LemmyContext;
16 use serde::Deserialize;
19 #[derive(Clone, Debug)]
20 pub enum PostOrComment {
22 Comment(Box<ApubComment>),
25 #[derive(Deserialize)]
32 #[async_trait::async_trait(?Send)]
33 impl ApubObject for PostOrComment {
34 type DataType = LemmyContext;
35 type ApubType = PageOrNote;
37 type Error = LemmyError;
39 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
43 // TODO: this can probably be implemented using a single sql query
44 #[tracing::instrument(skip_all)]
45 async fn read_from_apub_id(
47 data: &Self::DataType,
48 ) -> Result<Option<Self>, LemmyError> {
49 let post = ApubPost::read_from_apub_id(object_id.clone(), data).await?;
51 Some(o) => Some(PostOrComment::Post(Box::new(o))),
52 None => ApubComment::read_from_apub_id(object_id, data)
54 .map(|c| PostOrComment::Comment(Box::new(c))),
58 #[tracing::instrument(skip_all)]
59 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
61 PostOrComment::Post(p) => p.delete(data).await,
62 PostOrComment::Comment(c) => c.delete(data).await,
66 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
70 #[tracing::instrument(skip_all)]
72 apub: &Self::ApubType,
73 expected_domain: &Url,
74 data: &Self::DataType,
75 request_counter: &mut i32,
76 ) -> Result<(), LemmyError> {
78 PageOrNote::Page(a) => ApubPost::verify(a, expected_domain, data, request_counter).await,
79 PageOrNote::Note(a) => ApubComment::verify(a, expected_domain, data, request_counter).await,
83 #[tracing::instrument(skip_all)]
86 context: &LemmyContext,
87 request_counter: &mut i32,
88 ) -> Result<Self, LemmyError> {
90 PageOrNote::Page(p) => PostOrComment::Post(Box::new(
91 ApubPost::from_apub(*p, context, request_counter).await?,
93 PageOrNote::Note(n) => PostOrComment::Comment(Box::new(
94 ApubComment::from_apub(*n, context, request_counter).await?,
101 pub(crate) fn ap_id(&self) -> Url {
103 PostOrComment::Post(p) => p.ap_id.clone(),
104 PostOrComment::Comment(c) => c.ap_id.clone(),
110 #[async_trait::async_trait(?Send)]
111 impl InCommunity for PostOrComment {
114 context: &LemmyContext,
116 ) -> Result<ApubCommunity, LemmyError> {
117 let cid = match self {
118 PostOrComment::Post(p) => p.community_id,
119 PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
121 Ok(Community::read(context.pool(), cid).await?.into())