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_api_common::context::LemmyContext;
11 use lemmy_db_schema::{
12 source::{community::Community, post::Post},
15 use lemmy_utils::error::LemmyError;
16 use serde::Deserialize;
19 #[derive(Clone, Debug)]
20 pub enum PostOrComment {
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 #[tracing::instrument(skip_all)]
44 async fn read_from_apub_id(
46 data: &Self::DataType,
47 ) -> Result<Option<Self>, LemmyError> {
48 let post = ApubPost::read_from_apub_id(object_id.clone(), data).await?;
50 Some(o) => Some(PostOrComment::Post(o)),
51 None => ApubComment::read_from_apub_id(object_id, data)
53 .map(PostOrComment::Comment),
57 #[tracing::instrument(skip_all)]
58 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
60 PostOrComment::Post(p) => p.delete(data).await,
61 PostOrComment::Comment(c) => c.delete(data).await,
65 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
69 #[tracing::instrument(skip_all)]
71 apub: &Self::ApubType,
72 expected_domain: &Url,
73 data: &Self::DataType,
74 request_counter: &mut i32,
75 ) -> Result<(), LemmyError> {
77 PageOrNote::Page(a) => ApubPost::verify(a, expected_domain, data, request_counter).await,
78 PageOrNote::Note(a) => ApubComment::verify(a, expected_domain, data, request_counter).await,
82 #[tracing::instrument(skip_all)]
85 context: &LemmyContext,
86 request_counter: &mut i32,
87 ) -> Result<Self, LemmyError> {
89 PageOrNote::Page(p) => {
90 PostOrComment::Post(ApubPost::from_apub(*p, context, request_counter).await?)
92 PageOrNote::Note(n) => {
93 PostOrComment::Comment(ApubComment::from_apub(n, context, request_counter).await?)
99 #[async_trait::async_trait(?Send)]
100 impl InCommunity for PostOrComment {
103 context: &LemmyContext,
105 ) -> Result<ApubCommunity, LemmyError> {
106 let cid = match self {
107 PostOrComment::Post(p) => p.community_id,
108 PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
110 Ok(Community::read(context.pool(), cid).await?.into())