]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/block/mod.rs
Implement instance actor (#1798)
[lemmy.git] / crates / apub / src / activities / block / mod.rs
1 use crate::{
2   objects::{community::ApubCommunity, instance::ApubSite, person::ApubPerson},
3   protocol::objects::{group::Group, instance::Instance},
4 };
5 use chrono::NaiveDateTime;
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{
8   object_id::ObjectId,
9   traits::{ActorType, ApubObject},
10 };
11 use lemmy_db_schema::{source::site::Site, DbPool};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
14 use serde::Deserialize;
15 use url::Url;
16
17 pub mod block_user;
18 pub mod undo_block_user;
19
20 #[derive(Clone, Debug)]
21 pub enum SiteOrCommunity {
22   Site(ApubSite),
23   Community(ApubCommunity),
24 }
25
26 #[derive(Deserialize)]
27 #[serde(untagged)]
28 pub enum InstanceOrGroup {
29   Instance(Instance),
30   Group(Group),
31 }
32
33 #[async_trait::async_trait(?Send)]
34 impl ApubObject for SiteOrCommunity {
35   type DataType = LemmyContext;
36   type ApubType = InstanceOrGroup;
37   type TombstoneType = ();
38
39   #[tracing::instrument(skip_all)]
40   fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
41     Some(match self {
42       SiteOrCommunity::Site(i) => i.last_refreshed_at,
43       SiteOrCommunity::Community(c) => c.last_refreshed_at,
44     })
45   }
46
47   #[tracing::instrument(skip_all)]
48   async fn read_from_apub_id(
49     object_id: Url,
50     data: &Self::DataType,
51   ) -> Result<Option<Self>, LemmyError>
52   where
53     Self: Sized,
54   {
55     let site = ApubSite::read_from_apub_id(object_id.clone(), data).await?;
56     Ok(match site {
57       Some(o) => Some(SiteOrCommunity::Site(o)),
58       None => ApubCommunity::read_from_apub_id(object_id, data)
59         .await?
60         .map(SiteOrCommunity::Community),
61     })
62   }
63
64   async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
65     unimplemented!()
66   }
67
68   async fn into_apub(self, _data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
69     unimplemented!()
70   }
71
72   fn to_tombstone(&self) -> Result<Self::TombstoneType, LemmyError> {
73     unimplemented!()
74   }
75
76   #[tracing::instrument(skip_all)]
77   async fn verify(
78     apub: &Self::ApubType,
79     expected_domain: &Url,
80     data: &Self::DataType,
81     request_counter: &mut i32,
82   ) -> Result<(), LemmyError> {
83     match apub {
84       InstanceOrGroup::Instance(i) => {
85         ApubSite::verify(i, expected_domain, data, request_counter).await
86       }
87       InstanceOrGroup::Group(g) => {
88         ApubCommunity::verify(g, expected_domain, data, request_counter).await
89       }
90     }
91   }
92
93   #[tracing::instrument(skip_all)]
94   async fn from_apub(
95     apub: Self::ApubType,
96     data: &Self::DataType,
97     request_counter: &mut i32,
98   ) -> Result<Self, LemmyError>
99   where
100     Self: Sized,
101   {
102     Ok(match apub {
103       InstanceOrGroup::Instance(p) => {
104         SiteOrCommunity::Site(ApubSite::from_apub(p, data, request_counter).await?)
105       }
106       InstanceOrGroup::Group(n) => {
107         SiteOrCommunity::Community(ApubCommunity::from_apub(n, data, request_counter).await?)
108       }
109     })
110   }
111 }
112
113 impl SiteOrCommunity {
114   fn id(&self) -> ObjectId<SiteOrCommunity> {
115     match self {
116       SiteOrCommunity::Site(s) => ObjectId::new(s.actor_id.clone()),
117       SiteOrCommunity::Community(c) => ObjectId::new(c.actor_id.clone()),
118     }
119   }
120 }
121
122 async fn generate_cc(target: &SiteOrCommunity, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
123   Ok(match target {
124     SiteOrCommunity::Site(_) => blocking(pool, Site::read_remote_sites)
125       .await??
126       .into_iter()
127       .map(|s| s.actor_id.into())
128       .collect(),
129     SiteOrCommunity::Community(c) => vec![c.actor_id()],
130   })
131 }
132
133 async fn generate_instance_inboxes(
134   blocked_user: &ApubPerson,
135   pool: &DbPool,
136 ) -> Result<Vec<Url>, LemmyError> {
137   let mut inboxes: Vec<Url> = blocking(pool, Site::read_remote_sites)
138     .await??
139     .into_iter()
140     .map(|s| s.inbox_url.into())
141     .collect();
142   inboxes.push(blocked_user.shared_inbox_or_inbox_url());
143   Ok(inboxes)
144 }