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;
29 type TombstoneType = ();
31 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
33 UserOrCommunity::User(p) => p.last_refreshed_at,
34 UserOrCommunity::Community(p) => p.last_refreshed_at,
38 #[tracing::instrument(skip_all)]
39 async fn read_from_apub_id(
41 data: &Self::DataType,
42 ) -> Result<Option<Self>, LemmyError> {
43 let person = ApubPerson::read_from_apub_id(object_id.clone(), data).await?;
45 Some(o) => Some(UserOrCommunity::User(o)),
46 None => ApubCommunity::read_from_apub_id(object_id, data)
48 .map(UserOrCommunity::Community),
52 #[tracing::instrument(skip_all)]
53 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
55 UserOrCommunity::User(p) => p.delete(data).await,
56 UserOrCommunity::Community(p) => p.delete(data).await,
60 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
64 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
68 #[tracing::instrument(skip_all)]
70 apub: &Self::ApubType,
71 expected_domain: &Url,
72 data: &Self::DataType,
73 request_counter: &mut i32,
74 ) -> Result<(), LemmyError> {
76 PersonOrGroup::Person(a) => {
77 ApubPerson::verify(a, expected_domain, data, request_counter).await
79 PersonOrGroup::Group(a) => {
80 ApubCommunity::verify(a, expected_domain, data, request_counter).await
85 #[tracing::instrument(skip_all)]
88 data: &Self::DataType,
89 request_counter: &mut i32,
90 ) -> Result<Self, LemmyError> {
92 PersonOrGroup::Person(p) => {
93 UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
95 PersonOrGroup::Group(p) => {
96 UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
102 impl ActorType for UserOrCommunity {
103 fn actor_id(&self) -> Url {
105 UserOrCommunity::User(p) => p.actor_id(),
106 UserOrCommunity::Community(p) => p.actor_id(),
110 fn public_key(&self) -> String {
112 UserOrCommunity::User(p) => p.public_key(),
113 UserOrCommunity::Community(p) => p.public_key(),
117 fn private_key(&self) -> Option<String> {
121 fn inbox_url(&self) -> Url {
125 fn shared_inbox_url(&self) -> Option<Url> {