2 objects::{community::ApubCommunity, person::ApubPerson},
3 protocol::objects::{group::Group, person::Person},
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;
12 #[derive(Clone, Debug)]
13 pub enum UserOrCommunity {
15 Community(ApubCommunity),
18 #[derive(Deserialize)]
20 pub enum PersonOrGroup {
25 #[async_trait::async_trait(?Send)]
26 impl ApubObject for UserOrCommunity {
27 type DataType = LemmyContext;
28 type ApubType = PersonOrGroup;
30 type TombstoneType = ();
32 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
34 UserOrCommunity::User(p) => p.last_refreshed_at,
35 UserOrCommunity::Community(p) => p.last_refreshed_at,
39 #[tracing::instrument(skip_all)]
40 async fn read_from_apub_id(
42 data: &Self::DataType,
43 ) -> Result<Option<Self>, LemmyError> {
44 let person = ApubPerson::read_from_apub_id(object_id.clone(), data).await?;
46 Some(o) => Some(UserOrCommunity::User(o)),
47 None => ApubCommunity::read_from_apub_id(object_id, data)
49 .map(UserOrCommunity::Community),
53 #[tracing::instrument(skip_all)]
54 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
56 UserOrCommunity::User(p) => p.delete(data).await,
57 UserOrCommunity::Community(p) => p.delete(data).await,
61 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
65 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
69 #[tracing::instrument(skip_all)]
71 apub: &Self::ApubType,
72 expected_domain: &Url,
73 data: &Self::DataType,
74 request_counter: &mut i32,
75 ) -> Result<(), LemmyError> {
77 PersonOrGroup::Person(a) => {
78 ApubPerson::verify(a, expected_domain, data, request_counter).await
80 PersonOrGroup::Group(a) => {
81 ApubCommunity::verify(a, expected_domain, data, request_counter).await
86 #[tracing::instrument(skip_all)]
89 data: &Self::DataType,
90 request_counter: &mut i32,
91 ) -> Result<Self, LemmyError> {
93 PersonOrGroup::Person(p) => {
94 UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
96 PersonOrGroup::Group(p) => {
97 UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
103 impl ActorType for UserOrCommunity {
104 fn actor_id(&self) -> Url {
106 UserOrCommunity::User(p) => p.actor_id(),
107 UserOrCommunity::Community(p) => p.actor_id(),
111 fn public_key(&self) -> String {
113 UserOrCommunity::User(p) => p.public_key(),
114 UserOrCommunity::Community(p) => p.public_key(),
118 fn private_key(&self) -> Option<String> {
122 fn inbox_url(&self) -> Url {
126 fn shared_inbox_url(&self) -> Option<Url> {