]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/post_or_comment.rs
Don't drop error context when adding a message to errors (#1958)
[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   #[tracing::instrument(skip_all)]
37   async fn read_from_apub_id(
38     object_id: Url,
39     data: &Self::DataType,
40   ) -> Result<Option<Self>, LemmyError> {
41     let post = ApubPost::read_from_apub_id(object_id.clone(), data).await?;
42     Ok(match post {
43       Some(o) => Some(PostOrComment::Post(Box::new(o))),
44       None => ApubComment::read_from_apub_id(object_id, data)
45         .await?
46         .map(|c| PostOrComment::Comment(Box::new(c))),
47     })
48   }
49
50   #[tracing::instrument(skip_all)]
51   async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
52     match self {
53       PostOrComment::Post(p) => p.delete(data).await,
54       PostOrComment::Comment(c) => c.delete(data).await,
55     }
56   }
57
58   async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
59     unimplemented!()
60   }
61
62   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
63     unimplemented!()
64   }
65
66   #[tracing::instrument(skip_all)]
67   async fn verify(
68     apub: &Self::ApubType,
69     expected_domain: &Url,
70     data: &Self::DataType,
71     request_counter: &mut i32,
72   ) -> Result<(), LemmyError> {
73     match apub {
74       PageOrNote::Page(a) => ApubPost::verify(a, expected_domain, data, request_counter).await,
75       PageOrNote::Note(a) => ApubComment::verify(a, expected_domain, data, request_counter).await,
76     }
77   }
78
79   #[tracing::instrument(skip_all)]
80   async fn from_apub(
81     apub: PageOrNote,
82     context: &LemmyContext,
83     request_counter: &mut i32,
84   ) -> Result<Self, LemmyError> {
85     Ok(match apub {
86       PageOrNote::Page(p) => PostOrComment::Post(Box::new(
87         ApubPost::from_apub(p, context, request_counter).await?,
88       )),
89       PageOrNote::Note(n) => PostOrComment::Comment(Box::new(
90         ApubComment::from_apub(n, context, request_counter).await?,
91       )),
92     })
93   }
94 }
95
96 impl PostOrComment {
97   pub(crate) fn ap_id(&self) -> Url {
98     match self {
99       PostOrComment::Post(p) => p.ap_id.clone(),
100       PostOrComment::Comment(c) => c.ap_id.clone(),
101     }
102     .into()
103   }
104 }