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;
37 type TombstoneType = ();
39 #[tracing::instrument(skip_all)]
40 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
42 SiteOrCommunity::Site(i) => i.last_refreshed_at,
43 SiteOrCommunity::Community(c) => c.last_refreshed_at,
47 #[tracing::instrument(skip_all)]
48 async fn read_from_apub_id(
50 data: &Self::DataType,
51 ) -> Result<Option<Self>, LemmyError>
55 let site = ApubSite::read_from_apub_id(object_id.clone(), data).await?;
57 Some(o) => Some(SiteOrCommunity::Site(o)),
58 None => ApubCommunity::read_from_apub_id(object_id, data)
60 .map(SiteOrCommunity::Community),
64 async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
68 async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
72 fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
76 #[tracing::instrument(skip_all)]
78 apub: &Self::ApubType,
79 expected_domain: &Url,
80 data: &Self::DataType,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
84 InstanceOrGroup::Instance(i) => {
85 ApubSite::verify(i, expected_domain, data, request_counter).await
87 InstanceOrGroup::Group(g) => {
88 ApubCommunity::verify(g, expected_domain, data, request_counter).await
93 #[tracing::instrument(skip_all)]
96 data: &Self::DataType,
97 request_counter: &mut i32,
98 ) -> Result<Self, LemmyError>
103 InstanceOrGroup::Instance(p) => {
104 SiteOrCommunity::Site(ApubSite::from_apub(p, data, request_counter).await?)
106 InstanceOrGroup::Group(n) => {
107 SiteOrCommunity::Community(ApubCommunity::from_apub(n, data, request_counter).await?)
113 impl SiteOrCommunity {
114 fn id(&self) -> ObjectId<SiteOrCommunity> {
116 SiteOrCommunity::Site(s) => ObjectId::new(s.actor_id.clone()),
117 SiteOrCommunity::Community(c) => ObjectId::new(c.actor_id.clone()),
122 async fn generate_cc(target: &SiteOrCommunity, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
124 SiteOrCommunity::Site(_) => blocking(pool, Site::read_remote_sites)
127 .map(|s| s.actor_id.into())
129 SiteOrCommunity::Community(c) => vec![c.actor_id()],
133 async fn generate_instance_inboxes(
134 blocked_user: &ApubPerson,
136 ) -> Result<Vec<Url>, LemmyError> {
137 let mut inboxes: Vec<Url> = blocking(pool, Site::read_remote_sites)
140 .map(|s| s.inbox_url.into())
142 inboxes.push(blocked_user.shared_inbox_or_inbox_url());