2 objects::{community::ApubCommunity, person::ApubPerson},
3 protocol::objects::{group::Group, person::Person},
5 use activitypub_federation::{core::inbox::ActorPublicKey, traits::ApubObject};
6 use chrono::NaiveDateTime;
7 use lemmy_utils::error::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 Error = LemmyError;
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 #[tracing::instrument(skip_all)]
73 apub: &Self::ApubType,
74 expected_domain: &Url,
75 data: &Self::DataType,
76 request_counter: &mut i32,
77 ) -> Result<(), LemmyError> {
79 PersonOrGroup::Person(a) => {
80 ApubPerson::verify(a, expected_domain, data, request_counter).await
82 PersonOrGroup::Group(a) => {
83 ApubCommunity::verify(a, expected_domain, data, request_counter).await
88 #[tracing::instrument(skip_all)]
91 data: &Self::DataType,
92 request_counter: &mut i32,
93 ) -> Result<Self, LemmyError> {
95 PersonOrGroup::Person(p) => {
96 UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
98 PersonOrGroup::Group(p) => {
99 UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
105 impl ActorPublicKey for UserOrCommunity {
106 fn public_key(&self) -> &str {
108 UserOrCommunity::User(p) => p.public_key(),
109 UserOrCommunity::Community(p) => p.public_key(),