1 use crate::objects::{comment::Note, post::Page, FromApub};
2 use activitystreams::chrono::NaiveDateTime;
3 use diesel::PgConnection;
4 use lemmy_apub_lib::traits::ApubObject;
5 use lemmy_db_schema::source::{
6 comment::{Comment, CommentForm},
7 post::{Post, PostForm},
9 use lemmy_utils::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
14 #[derive(Clone, Debug)]
15 pub enum PostOrComment {
16 Comment(Box<Comment>),
20 pub enum PostOrCommentForm {
22 CommentForm(CommentForm),
25 #[derive(Deserialize)]
31 #[async_trait::async_trait(?Send)]
32 impl ApubObject for PostOrComment {
33 type DataType = PgConnection;
35 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
39 // TODO: this can probably be implemented using a single sql query
40 fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError>
44 let post = Post::read_from_apub_id(conn, object_id.clone())?;
46 Some(o) => Some(PostOrComment::Post(Box::new(o))),
48 Comment::read_from_apub_id(conn, object_id)?.map(|c| PostOrComment::Comment(Box::new(c)))
54 #[async_trait::async_trait(?Send)]
55 impl FromApub for PostOrComment {
56 type ApubType = PageOrNote;
60 context: &LemmyContext,
61 expected_domain: &Url,
62 request_counter: &mut i32,
63 ) -> Result<Self, LemmyError>
68 PageOrNote::Page(p) => PostOrComment::Post(Box::new(
69 Post::from_apub(p, context, expected_domain, request_counter).await?,
71 PageOrNote::Note(n) => PostOrComment::Comment(Box::new(
72 Comment::from_apub(n, context, expected_domain, request_counter).await?,
79 pub(crate) fn ap_id(&self) -> Url {
81 PostOrComment::Post(p) => p.ap_id.clone(),
82 PostOrComment::Comment(c) => c.ap_id.clone(),