2 objects::{community::ApubCommunity, instance::ApubSite},
3 protocol::objects::{group::Group, instance::Instance},
6 use activitypub_federation::{core::object_id::ObjectId, traits::ApubObject};
7 use chrono::NaiveDateTime;
8 use lemmy_db_schema::{source::site::Site, utils::DbPool};
9 use lemmy_utils::error::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
15 pub mod undo_block_user;
17 #[derive(Clone, Debug)]
18 pub enum SiteOrCommunity {
20 Community(ApubCommunity),
23 #[derive(Deserialize)]
25 pub enum InstanceOrGroup {
30 #[async_trait::async_trait(?Send)]
31 impl ApubObject for SiteOrCommunity {
32 type DataType = LemmyContext;
33 type ApubType = InstanceOrGroup;
35 type Error = LemmyError;
37 #[tracing::instrument(skip_all)]
38 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
40 SiteOrCommunity::Site(i) => i.last_refreshed_at,
41 SiteOrCommunity::Community(c) => c.last_refreshed_at,
45 #[tracing::instrument(skip_all)]
46 async fn read_from_apub_id(
48 data: &Self::DataType,
49 ) -> Result<Option<Self>, LemmyError>
53 let site = ApubSite::read_from_apub_id(object_id.clone(), data).await?;
55 Some(o) => Some(SiteOrCommunity::Site(o)),
56 None => ApubCommunity::read_from_apub_id(object_id, data)
58 .map(SiteOrCommunity::Community),
62 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
66 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
70 #[tracing::instrument(skip_all)]
72 apub: &Self::ApubType,
73 expected_domain: &Url,
74 data: &Self::DataType,
75 request_counter: &mut i32,
76 ) -> Result<(), LemmyError> {
78 InstanceOrGroup::Instance(i) => {
79 ApubSite::verify(i, expected_domain, data, request_counter).await
81 InstanceOrGroup::Group(g) => {
82 ApubCommunity::verify(g, expected_domain, data, request_counter).await
87 #[tracing::instrument(skip_all)]
90 data: &Self::DataType,
91 request_counter: &mut i32,
92 ) -> Result<Self, LemmyError>
97 InstanceOrGroup::Instance(p) => {
98 SiteOrCommunity::Site(ApubSite::from_apub(p, data, request_counter).await?)
100 InstanceOrGroup::Group(n) => {
101 SiteOrCommunity::Community(ApubCommunity::from_apub(n, data, request_counter).await?)
107 impl SiteOrCommunity {
108 fn id(&self) -> ObjectId<SiteOrCommunity> {
110 SiteOrCommunity::Site(s) => ObjectId::new(s.actor_id.clone()),
111 SiteOrCommunity::Community(c) => ObjectId::new(c.actor_id.clone()),
116 async fn generate_cc(target: &SiteOrCommunity, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
118 SiteOrCommunity::Site(_) => Site::read_remote_sites(pool)
121 .map(|s| s.actor_id.into())
123 SiteOrCommunity::Community(c) => vec![c.actor_id()],