]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/post_or_comment.rs
Fix clippy warnings added in nightly (#1833)
[lemmy.git] / crates / apub / src / fetcher / post_or_comment.rs
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},
8 };
9 use lemmy_utils::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
12 use url::Url;
13
14 #[derive(Clone, Debug)]
15 pub enum PostOrComment {
16   Post(Box<Post>),
17   Comment(Comment),
18 }
19
20 pub enum PostOrCommentForm {
21   PostForm(Box<PostForm>),
22   CommentForm(CommentForm),
23 }
24
25 #[derive(Deserialize)]
26 pub enum PageOrNote {
27   Page(Box<Page>),
28   Note(Box<Note>),
29 }
30
31 #[async_trait::async_trait(?Send)]
32 impl ApubObject for PostOrComment {
33   type DataType = PgConnection;
34
35   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
36     None
37   }
38
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>
41   where
42     Self: Sized,
43   {
44     let post = Post::read_from_apub_id(conn, object_id.clone())?;
45     Ok(match post {
46       Some(o) => Some(PostOrComment::Post(Box::new(o))),
47       None => Comment::read_from_apub_id(conn, object_id)?.map(PostOrComment::Comment),
48     })
49   }
50 }
51
52 #[async_trait::async_trait(?Send)]
53 impl FromApub for PostOrComment {
54   type ApubType = PageOrNote;
55
56   async fn from_apub(
57     apub: &PageOrNote,
58     context: &LemmyContext,
59     expected_domain: &Url,
60     request_counter: &mut i32,
61   ) -> Result<Self, LemmyError>
62   where
63     Self: Sized,
64   {
65     Ok(match apub {
66       PageOrNote::Page(p) => PostOrComment::Post(Box::new(
67         Post::from_apub(p, context, expected_domain, request_counter).await?,
68       )),
69       PageOrNote::Note(n) => PostOrComment::Comment(
70         Comment::from_apub(n, context, expected_domain, request_counter).await?,
71       ),
72     })
73   }
74 }
75
76 impl PostOrComment {
77   pub(crate) fn ap_id(&self) -> Url {
78     match self {
79       PostOrComment::Post(p) => p.ap_id.clone(),
80       PostOrComment::Comment(c) => c.ap_id.clone(),
81     }
82     .into()
83   }
84 }