]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/send/community.rs
29aa9dba8c62c43ae2c15115d20dfae85ddc4206
[lemmy.git] / crates / apub / src / activities / send / community.rs
1 use crate::{check_is_apub_id_valid, ActorType, CommunityType};
2 use itertools::Itertools;
3 use lemmy_api_common::blocking;
4 use lemmy_db_queries::DbPool;
5 use lemmy_db_schema::source::community::Community;
6 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
7 use lemmy_utils::LemmyError;
8 use url::Url;
9
10 impl ActorType for Community {
11   fn is_local(&self) -> bool {
12     self.local
13   }
14   fn actor_id(&self) -> Url {
15     self.actor_id.to_owned().into()
16   }
17   fn name(&self) -> String {
18     self.name.clone()
19   }
20   fn public_key(&self) -> Option<String> {
21     self.public_key.to_owned()
22   }
23   fn private_key(&self) -> Option<String> {
24     self.private_key.to_owned()
25   }
26
27   fn get_shared_inbox_or_inbox_url(&self) -> Url {
28     self
29       .shared_inbox_url
30       .clone()
31       .unwrap_or_else(|| self.inbox_url.to_owned())
32       .into()
33   }
34 }
35
36 #[async_trait::async_trait(?Send)]
37 impl CommunityType for Community {
38   fn followers_url(&self) -> Url {
39     self.followers_url.clone().into()
40   }
41
42   /// For a given community, returns the inboxes of all followers.
43   async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
44     let id = self.id;
45
46     let follows = blocking(pool, move |conn| {
47       CommunityFollowerView::for_community(conn, id)
48     })
49     .await??;
50     let inboxes = follows
51       .into_iter()
52       .filter(|f| !f.follower.local)
53       .map(|f| f.follower.shared_inbox_url.unwrap_or(f.follower.inbox_url))
54       .map(|i| i.into_inner())
55       .unique()
56       // Don't send to blocked instances
57       .filter(|inbox| check_is_apub_id_valid(inbox, false).is_ok())
58       .collect();
59
60     Ok(inboxes)
61   }
62 }