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_api_common::utils::blocking;
9 use lemmy_db_schema::{source::site::Site, utils::DbPool};
10 use lemmy_utils::error::LemmyError;
11 use lemmy_websocket::LemmyContext;
12 use serde::Deserialize;
16 pub mod undo_block_user;
18 #[derive(Clone, Debug)]
19 pub enum SiteOrCommunity {
21 Community(ApubCommunity),
24 #[derive(Deserialize)]
26 pub enum InstanceOrGroup {
31 #[async_trait::async_trait(?Send)]
32 impl ApubObject for SiteOrCommunity {
33 type DataType = LemmyContext;
34 type ApubType = InstanceOrGroup;
36 type Error = LemmyError;
38 #[tracing::instrument(skip_all)]
39 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
41 SiteOrCommunity::Site(i) => i.last_refreshed_at,
42 SiteOrCommunity::Community(c) => c.last_refreshed_at,
46 #[tracing::instrument(skip_all)]
47 async fn read_from_apub_id(
49 data: &Self::DataType,
50 ) -> Result<Option<Self>, LemmyError>
54 let site = ApubSite::read_from_apub_id(object_id.clone(), data).await?;
56 Some(o) => Some(SiteOrCommunity::Site(o)),
57 None => ApubCommunity::read_from_apub_id(object_id, data)
59 .map(SiteOrCommunity::Community),
63 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
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 InstanceOrGroup::Instance(i) => {
80 ApubSite::verify(i, expected_domain, data, request_counter).await
82 InstanceOrGroup::Group(g) => {
83 ApubCommunity::verify(g, expected_domain, data, request_counter).await
88 #[tracing::instrument(skip_all)]
91 data: &Self::DataType,
92 request_counter: &mut i32,
93 ) -> Result<Self, LemmyError>
98 InstanceOrGroup::Instance(p) => {
99 SiteOrCommunity::Site(ApubSite::from_apub(p, data, request_counter).await?)
101 InstanceOrGroup::Group(n) => {
102 SiteOrCommunity::Community(ApubCommunity::from_apub(n, data, request_counter).await?)
108 impl SiteOrCommunity {
109 fn id(&self) -> ObjectId<SiteOrCommunity> {
111 SiteOrCommunity::Site(s) => ObjectId::new(s.actor_id.clone()),
112 SiteOrCommunity::Community(c) => ObjectId::new(c.actor_id.clone()),
117 async fn generate_cc(target: &SiteOrCommunity, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
119 SiteOrCommunity::Site(_) => blocking(pool, Site::read_remote_sites)
122 .map(|s| s.actor_id.into())
124 SiteOrCommunity::Community(c) => vec![c.actor_id()],