]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/post_or_comment.rs
55d4948117c6fd7dabb45fd8c01b3c19212950ae
[lemmy.git] / crates / apub / src / fetcher / post_or_comment.rs
1 use crate::{
2   objects::{comment::ApubComment, post::ApubPost},
3   protocol::objects::{note::Note, page::Page},
4 };
5 use chrono::NaiveDateTime;
6 use lemmy_apub_lib::traits::ApubObject;
7 use lemmy_utils::LemmyError;
8 use lemmy_websocket::LemmyContext;
9 use serde::Deserialize;
10 use url::Url;
11
12 #[derive(Clone, Debug)]
13 pub enum PostOrComment {
14   Post(Box<ApubPost>),
15   Comment(Box<ApubComment>),
16 }
17
18 #[derive(Deserialize)]
19 #[serde(untagged)]
20 pub enum PageOrNote {
21   Page(Page),
22   Note(Note),
23 }
24
25 #[async_trait::async_trait(?Send)]
26 impl ApubObject for PostOrComment {
27   type DataType = LemmyContext;
28   type ApubType = PageOrNote;
29   type TombstoneType = ();
30
31   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
32     None
33   }
34
35   // TODO: this can probably be implemented using a single sql query
36   async fn read_from_apub_id(
37     object_id: Url,
38     data: &Self::DataType,
39   ) -> Result<Option<Self>, LemmyError> {
40     let post = ApubPost::read_from_apub_id(object_id.clone(), data).await?;
41     Ok(match post {
42       Some(o) => Some(PostOrComment::Post(Box::new(o))),
43       None => ApubComment::read_from_apub_id(object_id, data)
44         .await?
45         .map(|c| PostOrComment::Comment(Box::new(c))),
46     })
47   }
48
49   async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
50     match self {
51       PostOrComment::Post(p) => p.delete(data).await,
52       PostOrComment::Comment(c) => c.delete(data).await,
53     }
54   }
55
56   async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
57     unimplemented!()
58   }
59
60   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
61     unimplemented!()
62   }
63
64   async fn verify(
65     apub: &Self::ApubType,
66     expected_domain: &Url,
67     data: &Self::DataType,
68     request_counter: &mut i32,
69   ) -> Result<(), LemmyError> {
70     match apub {
71       PageOrNote::Page(a) => ApubPost::verify(a, expected_domain, data, request_counter).await,
72       PageOrNote::Note(a) => ApubComment::verify(a, expected_domain, data, request_counter).await,
73     }
74   }
75
76   async fn from_apub(
77     apub: PageOrNote,
78     context: &LemmyContext,
79     request_counter: &mut i32,
80   ) -> Result<Self, LemmyError> {
81     Ok(match apub {
82       PageOrNote::Page(p) => PostOrComment::Post(Box::new(
83         ApubPost::from_apub(p, context, request_counter).await?,
84       )),
85       PageOrNote::Note(n) => PostOrComment::Comment(Box::new(
86         ApubComment::from_apub(n, context, request_counter).await?,
87       )),
88     })
89   }
90 }
91
92 impl PostOrComment {
93   pub(crate) fn ap_id(&self) -> Url {
94     match self {
95       PostOrComment::Post(p) => p.ap_id.clone(),
96       PostOrComment::Comment(c) => c.ap_id.clone(),
97     }
98     .into()
99   }
100 }