1 use crate::objects::{comment::Note, post::Page, FromApub};
2 use activitystreams::chrono::NaiveDateTime;
3 use diesel::{result::Error, PgConnection};
4 use lemmy_db_queries::ApubObject;
7 comment::{Comment, CommentForm},
8 post::{Post, PostForm},
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
14 use serde::Deserialize;
17 #[derive(Clone, Debug)]
18 pub enum PostOrComment {
19 Comment(Box<Comment>),
23 pub enum PostOrCommentForm {
25 CommentForm(CommentForm),
28 #[derive(Deserialize)]
34 #[async_trait::async_trait(?Send)]
35 impl ApubObject for PostOrComment {
36 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
40 // TODO: this can probably be implemented using a single sql query
41 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
45 let post = Post::read_from_apub_id(conn, object_id);
47 Ok(o) => PostOrComment::Post(Box::new(o)),
48 Err(_) => PostOrComment::Comment(Box::new(Comment::read_from_apub_id(conn, object_id)?)),
53 #[async_trait::async_trait(?Send)]
54 impl FromApub for PostOrComment {
55 type ApubType = PageOrNote;
59 context: &LemmyContext,
60 expected_domain: &Url,
61 request_counter: &mut i32,
62 ) -> Result<Self, LemmyError>
67 PageOrNote::Page(p) => PostOrComment::Post(Box::new(
68 Post::from_apub(p, context, expected_domain, request_counter).await?,
70 PageOrNote::Note(n) => PostOrComment::Comment(Box::new(
71 Comment::from_apub(n, context, expected_domain, request_counter).await?,
78 pub(crate) fn ap_id(&self) -> Url {
80 PostOrComment::Post(p) => p.ap_id.clone(),
81 PostOrComment::Comment(c) => c.ap_id.clone(),