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, Serialize};
12 #[derive(Clone, Debug)]
13 pub enum UserOrCommunity {
15 Community(ApubCommunity),
18 #[derive(Serialize, Deserialize, Clone, Debug)]
20 pub enum PersonOrGroup {
25 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
26 pub enum PersonOrGroupType {
31 #[async_trait::async_trait(?Send)]
32 impl ApubObject for UserOrCommunity {
33 type DataType = LemmyContext;
34 type ApubType = PersonOrGroup;
36 type TombstoneType = ();
38 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
40 UserOrCommunity::User(p) => p.last_refreshed_at,
41 UserOrCommunity::Community(p) => p.last_refreshed_at,
45 #[tracing::instrument(skip_all)]
46 async fn read_from_apub_id(
48 data: &Self::DataType,
49 ) -> Result<Option<Self>, LemmyError> {
50 let person = ApubPerson::read_from_apub_id(object_id.clone(), data).await?;
52 Some(o) => Some(UserOrCommunity::User(o)),
53 None => ApubCommunity::read_from_apub_id(object_id, data)
55 .map(UserOrCommunity::Community),
59 #[tracing::instrument(skip_all)]
60 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
62 UserOrCommunity::User(p) => p.delete(data).await,
63 UserOrCommunity::Community(p) => p.delete(data).await,
67 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
71 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
75 #[tracing::instrument(skip_all)]
77 apub: &Self::ApubType,
78 expected_domain: &Url,
79 data: &Self::DataType,
80 request_counter: &mut i32,
81 ) -> Result<(), LemmyError> {
83 PersonOrGroup::Person(a) => {
84 ApubPerson::verify(a, expected_domain, data, request_counter).await
86 PersonOrGroup::Group(a) => {
87 ApubCommunity::verify(a, expected_domain, data, request_counter).await
92 #[tracing::instrument(skip_all)]
95 data: &Self::DataType,
96 request_counter: &mut i32,
97 ) -> Result<Self, LemmyError> {
99 PersonOrGroup::Person(p) => {
100 UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
102 PersonOrGroup::Group(p) => {
103 UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
109 impl ActorType for UserOrCommunity {
110 fn actor_id(&self) -> Url {
112 UserOrCommunity::User(p) => p.actor_id(),
113 UserOrCommunity::Community(p) => p.actor_id(),
117 fn public_key(&self) -> String {
119 UserOrCommunity::User(p) => p.public_key(),
120 UserOrCommunity::Community(p) => p.public_key(),
124 fn private_key(&self) -> Option<String> {
128 fn inbox_url(&self) -> Url {
132 fn shared_inbox_url(&self) -> Option<Url> {