2 objects::{comment::ApubComment, community::ApubCommunity, post::ApubPost},
4 objects::{note::Note, page::Page},
8 use activitypub_federation::{config::Data, traits::Object};
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]
33 impl Object for PostOrComment {
34 type DataType = LemmyContext;
35 type Kind = PageOrNote;
36 type Error = LemmyError;
38 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
42 #[tracing::instrument(skip_all)]
43 async fn read_from_id(
45 data: &Data<Self::DataType>,
46 ) -> Result<Option<Self>, LemmyError> {
47 let post = ApubPost::read_from_id(object_id.clone(), data).await?;
49 Some(o) => Some(PostOrComment::Post(o)),
50 None => ApubComment::read_from_id(object_id, data)
52 .map(PostOrComment::Comment),
56 #[tracing::instrument(skip_all)]
57 async fn delete(self, data: &Data<Self::DataType>) -> Result<(), LemmyError> {
59 PostOrComment::Post(p) => p.delete(data).await,
60 PostOrComment::Comment(c) => c.delete(data).await,
64 async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, LemmyError> {
68 #[tracing::instrument(skip_all)]
71 expected_domain: &Url,
72 data: &Data<Self::DataType>,
73 ) -> Result<(), LemmyError> {
75 PageOrNote::Page(a) => ApubPost::verify(a, expected_domain, data).await,
76 PageOrNote::Note(a) => ApubComment::verify(a, expected_domain, data).await,
80 #[tracing::instrument(skip_all)]
81 async fn from_json(apub: PageOrNote, context: &Data<LemmyContext>) -> Result<Self, LemmyError> {
83 PageOrNote::Page(p) => PostOrComment::Post(ApubPost::from_json(*p, context).await?),
84 PageOrNote::Note(n) => PostOrComment::Comment(ApubComment::from_json(n, context).await?),
89 #[async_trait::async_trait]
90 impl InCommunity for PostOrComment {
91 async fn community(&self, context: &Data<LemmyContext>) -> Result<ApubCommunity, LemmyError> {
92 let cid = match self {
93 PostOrComment::Post(p) => p.community_id,
94 PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
96 Ok(Community::read(context.pool(), cid).await?.into())