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 {
20 pub enum PostOrCommentForm {
21 PostForm(Box<PostForm>),
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))),
47 None => Comment::read_from_apub_id(conn, object_id)?.map(PostOrComment::Comment),
52 #[async_trait::async_trait(?Send)]
53 impl FromApub for PostOrComment {
54 type ApubType = PageOrNote;
58 context: &LemmyContext,
59 expected_domain: &Url,
60 request_counter: &mut i32,
61 ) -> Result<Self, LemmyError>
66 PageOrNote::Page(p) => PostOrComment::Post(Box::new(
67 Post::from_apub(p, context, expected_domain, request_counter).await?,
69 PageOrNote::Note(n) => PostOrComment::Comment(
70 Comment::from_apub(n, context, expected_domain, request_counter).await?,
77 pub(crate) fn ap_id(&self) -> Url {
79 PostOrComment::Post(p) => p.ap_id.clone(),
80 PostOrComment::Comment(c) => c.ap_id.clone(),