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