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