]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/user_or_community.rs
Replace activitystreams crate with activitystreams-kinds
[lemmy.git] / crates / apub / src / fetcher / user_or_community.rs
1 use crate::{
2   objects::{community::ApubCommunity, person::ApubPerson},
3   protocol::objects::{group::Group, person::Person},
4 };
5 use chrono::NaiveDateTime;
6 use lemmy_apub_lib::traits::{ActorType, 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 UserOrCommunity {
14   User(ApubPerson),
15   Community(ApubCommunity),
16 }
17
18 #[derive(Deserialize)]
19 #[serde(untagged)]
20 pub enum PersonOrGroup {
21   Person(Person),
22   Group(Group),
23 }
24
25 #[async_trait::async_trait(?Send)]
26 impl ApubObject for UserOrCommunity {
27   type DataType = LemmyContext;
28   type ApubType = PersonOrGroup;
29   type TombstoneType = ();
30
31   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
32     Some(match self {
33       UserOrCommunity::User(p) => p.last_refreshed_at,
34       UserOrCommunity::Community(p) => p.last_refreshed_at,
35     })
36   }
37
38   async fn read_from_apub_id(
39     object_id: Url,
40     data: &Self::DataType,
41   ) -> Result<Option<Self>, LemmyError> {
42     let person = ApubPerson::read_from_apub_id(object_id.clone(), data).await?;
43     Ok(match person {
44       Some(o) => Some(UserOrCommunity::User(o)),
45       None => ApubCommunity::read_from_apub_id(object_id, data)
46         .await?
47         .map(UserOrCommunity::Community),
48     })
49   }
50
51   async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
52     match self {
53       UserOrCommunity::User(p) => p.delete(data).await,
54       UserOrCommunity::Community(p) => p.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   async fn verify(
67     apub: &Self::ApubType,
68     expected_domain: &Url,
69     data: &Self::DataType,
70     request_counter: &mut i32,
71   ) -> Result<(), LemmyError> {
72     match apub {
73       PersonOrGroup::Person(a) => {
74         ApubPerson::verify(a, expected_domain, data, request_counter).await
75       }
76       PersonOrGroup::Group(a) => {
77         ApubCommunity::verify(a, expected_domain, data, request_counter).await
78       }
79     }
80   }
81
82   async fn from_apub(
83     apub: Self::ApubType,
84     data: &Self::DataType,
85     request_counter: &mut i32,
86   ) -> Result<Self, LemmyError> {
87     Ok(match apub {
88       PersonOrGroup::Person(p) => {
89         UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
90       }
91       PersonOrGroup::Group(p) => {
92         UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
93       }
94     })
95   }
96 }
97
98 impl ActorType for UserOrCommunity {
99   fn actor_id(&self) -> Url {
100     match self {
101       UserOrCommunity::User(p) => p.actor_id(),
102       UserOrCommunity::Community(p) => p.actor_id(),
103     }
104   }
105
106   fn public_key(&self) -> Option<String> {
107     match self {
108       UserOrCommunity::User(p) => p.public_key(),
109       UserOrCommunity::Community(p) => p.public_key(),
110     }
111   }
112
113   fn private_key(&self) -> Option<String> {
114     todo!()
115   }
116
117   fn inbox_url(&self) -> Url {
118     todo!()
119   }
120
121   fn shared_inbox_url(&self) -> Option<Url> {
122     todo!()
123   }
124 }