2 objects::{community::ApubCommunity, person::ApubPerson},
3 protocol::objects::{group::Group, person::Person},
6 use activitypub_federation::traits::{Actor, ApubObject};
7 use chrono::NaiveDateTime;
8 use lemmy_api_common::context::LemmyContext;
9 use lemmy_utils::error::LemmyError;
10 use serde::{Deserialize, Serialize};
13 #[derive(Clone, Debug)]
14 pub enum UserOrCommunity {
16 Community(ApubCommunity),
19 #[derive(Serialize, Deserialize, Clone, Debug)]
21 pub enum PersonOrGroup {
26 #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
27 pub enum PersonOrGroupType {
32 #[async_trait::async_trait(?Send)]
33 impl ApubObject for UserOrCommunity {
34 type DataType = LemmyContext;
35 type ApubType = PersonOrGroup;
37 type Error = LemmyError;
39 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
41 UserOrCommunity::User(p) => p.last_refreshed_at,
42 UserOrCommunity::Community(p) => p.last_refreshed_at,
46 #[tracing::instrument(skip_all)]
47 async fn read_from_apub_id(
49 data: &Self::DataType,
50 ) -> Result<Option<Self>, LemmyError> {
51 let person = ApubPerson::read_from_apub_id(object_id.clone(), data).await?;
53 Some(o) => Some(UserOrCommunity::User(o)),
54 None => ApubCommunity::read_from_apub_id(object_id, data)
56 .map(UserOrCommunity::Community),
60 #[tracing::instrument(skip_all)]
61 async fn delete(self, data: &Self::DataType) -> Result<(), LemmyError> {
63 UserOrCommunity::User(p) => p.delete(data).await,
64 UserOrCommunity::Community(p) => p.delete(data).await,
68 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
72 #[tracing::instrument(skip_all)]
74 apub: &Self::ApubType,
75 expected_domain: &Url,
76 data: &Self::DataType,
77 request_counter: &mut i32,
78 ) -> Result<(), LemmyError> {
80 PersonOrGroup::Person(a) => {
81 ApubPerson::verify(a, expected_domain, data, request_counter).await
83 PersonOrGroup::Group(a) => {
84 ApubCommunity::verify(a, expected_domain, data, request_counter).await
89 #[tracing::instrument(skip_all)]
92 data: &Self::DataType,
93 request_counter: &mut i32,
94 ) -> Result<Self, LemmyError> {
96 PersonOrGroup::Person(p) => {
97 UserOrCommunity::User(ApubPerson::from_apub(p, data, request_counter).await?)
99 PersonOrGroup::Group(p) => {
100 UserOrCommunity::Community(ApubCommunity::from_apub(p, data, request_counter).await?)
106 impl Actor for UserOrCommunity {
107 fn public_key(&self) -> &str {
109 UserOrCommunity::User(p) => p.public_key(),
110 UserOrCommunity::Community(p) => p.public_key(),
114 fn inbox(&self) -> Url {
119 impl ActorType for UserOrCommunity {
120 fn actor_id(&self) -> Url {
122 UserOrCommunity::User(u) => u.actor_id(),
123 UserOrCommunity::Community(c) => c.actor_id(),
127 fn private_key(&self) -> Option<String> {
129 UserOrCommunity::User(u) => u.private_key(),
130 UserOrCommunity::Community(c) => c.private_key(),