2 objects::{community::ApubCommunity, instance::ApubSite, person::ApubPerson},
3 protocol::objects::{group::Group, instance::Instance},
5 use chrono::NaiveDateTime;
6 use lemmy_api_common::blocking;
9 traits::{ActorType, ApubObject},
11 use lemmy_db_schema::{source::site::Site, DbPool};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
14 use serde::Deserialize;
18 pub mod undo_block_user;
20 #[derive(Clone, Debug)]
21 pub enum SiteOrCommunity {
23 Community(ApubCommunity),
26 #[derive(Deserialize)]
28 pub enum InstanceOrGroup {
33 #[async_trait::async_trait(?Send)]
34 impl ApubObject for SiteOrCommunity {
35 type DataType = LemmyContext;
36 type ApubType = InstanceOrGroup;
38 type TombstoneType = ();
40 #[tracing::instrument(skip_all)]
41 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
43 SiteOrCommunity::Site(i) => i.last_refreshed_at,
44 SiteOrCommunity::Community(c) => c.last_refreshed_at,
48 #[tracing::instrument(skip_all)]
49 async fn read_from_apub_id(
51 data: &Self::DataType,
52 ) -> Result<Option<Self>, LemmyError>
56 let site = ApubSite::read_from_apub_id(object_id.clone(), data).await?;
58 Some(o) => Some(SiteOrCommunity::Site(o)),
59 None => ApubCommunity::read_from_apub_id(object_id, data)
61 .map(SiteOrCommunity::Community),
65 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
69 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
73 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
77 #[tracing::instrument(skip_all)]
79 apub: &Self::ApubType,
80 expected_domain: &Url,
81 data: &Self::DataType,
82 request_counter: &mut i32,
83 ) -> Result<(), LemmyError> {
85 InstanceOrGroup::Instance(i) => {
86 ApubSite::verify(i, expected_domain, data, request_counter).await
88 InstanceOrGroup::Group(g) => {
89 ApubCommunity::verify(g, expected_domain, data, request_counter).await
94 #[tracing::instrument(skip_all)]
97 data: &Self::DataType,
98 request_counter: &mut i32,
99 ) -> Result<Self, LemmyError>
104 InstanceOrGroup::Instance(p) => {
105 SiteOrCommunity::Site(ApubSite::from_apub(p, data, request_counter).await?)
107 InstanceOrGroup::Group(n) => {
108 SiteOrCommunity::Community(ApubCommunity::from_apub(n, data, request_counter).await?)
114 impl SiteOrCommunity {
115 fn id(&self) -> ObjectId<SiteOrCommunity> {
117 SiteOrCommunity::Site(s) => ObjectId::new(s.actor_id.clone()),
118 SiteOrCommunity::Community(c) => ObjectId::new(c.actor_id.clone()),
123 async fn generate_cc(target: &SiteOrCommunity, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
125 SiteOrCommunity::Site(_) => blocking(pool, Site::read_remote_sites)
128 .map(|s| s.actor_id.into())
130 SiteOrCommunity::Community(c) => vec![c.actor_id()],
134 async fn generate_instance_inboxes(
135 blocked_user: &ApubPerson,
137 ) -> Result<Vec<Url>, LemmyError> {
138 let mut inboxes: Vec<Url> = blocking(pool, Site::read_remote_sites)
141 .map(|s| s.inbox_url.into())
143 inboxes.push(blocked_user.shared_inbox_or_inbox_url());