]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/user_or_community.rs
da23ad59a180a85395d27aa9f325e12fa8f4b014
[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 activitypub_federation::traits::{Actor, ApubObject};
6 use chrono::NaiveDateTime;
7 use lemmy_utils::error::LemmyError;
8 use lemmy_websocket::LemmyContext;
9 use serde::{Deserialize, Serialize};
10 use url::Url;
11
12 #[derive(Clone, Debug)]
13 pub enum UserOrCommunity {
14   User(ApubPerson),
15   Community(ApubCommunity),
16 }
17
18 #[derive(Serialize, Deserialize, Clone, Debug)]
19 #[serde(untagged)]
20 pub enum PersonOrGroup {
21   Person(Person),
22   Group(Group),
23 }
24
25 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
26 pub enum PersonOrGroupType {
27   Person,
28   Group,
29 }
30
31 #[async_trait::async_trait(?Send)]
32 impl ApubObject for UserOrCommunity {
33   type DataType = LemmyContext;
34   type ApubType = PersonOrGroup;
35   type DbType = ();
36   type Error = LemmyError;
37
38   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
39     Some(match self {
40       UserOrCommunity::User(p) => p.last_refreshed_at,
41       UserOrCommunity::Community(p) => p.last_refreshed_at,
42     })
43   }
44
45   #[tracing::instrument(skip_all)]
46   async fn read_from_apub_id(
47     object_id: Url,
48     data: &Self::DataType,
49   ) -> Result<Option<Self>, LemmyError> {
50     let person = ApubPerson::read_from_apub_id(object_id.clone(), data).await?;
51     Ok(match person {
52       Some(o) => Some(UserOrCommunity::User(o)),
53       None => ApubCommunity::read_from_apub_id(object_id, data)
54         .await?
55         .map(UserOrCommunity::Community),
56     })
57   }
58
59   #[tracing::instrument(skip_all)]
60   async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
61     match self {
62       UserOrCommunity::User(p) => p.delete(data).await,
63       UserOrCommunity::Community(p) => p.delete(data).await,
64     }
65   }
66
67   async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
68     unimplemented!()
69   }
70
71   #[tracing::instrument(skip_all)]
72   async fn verify(
73     apub: &Self::ApubType,
74     expected_domain: &Url,
75     data: &Self::DataType,
76     request_counter: &mut i32,
77   ) -> Result<(), LemmyError> {
78     match apub {
79       PersonOrGroup::Person(a) => {
80         ApubPerson::verify(a, expected_domain, data, request_counter).await
81       }
82       PersonOrGroup::Group(a) => {
83         ApubCommunity::verify(a, expected_domain, data, request_counter).await
84       }
85     }
86   }
87
88   #[tracing::instrument(skip_all)]
89   async fn from_apub(
90     apub: Self::ApubType,
91     data: &Self::DataType,
92     request_counter: &mut i32,
93   ) -> Result<Self, LemmyError> {
94     Ok(match apub {
95       PersonOrGroup::Person(p) => {
96         UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
97       }
98       PersonOrGroup::Group(p) => {
99         UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
100       }
101     })
102   }
103 }
104
105 impl Actor for UserOrCommunity {
106   fn public_key(&self) -> &str {
107     match self {
108       UserOrCommunity::User(p) => p.public_key(),
109       UserOrCommunity::Community(p) => p.public_key(),
110     }
111   }
112
113   fn inbox(&self) -> Url {
114     unimplemented!()
115   }
116 }