From: Dessalines Date: Mon, 1 Feb 2021 18:11:37 +0000 (-0500) Subject: Add allowed and blocked instances to the federated_instances response. (#1398) X-Git-Url: http://these/git/%7B%60/static/assets/css/themes/%7Bthis.captchaPngSrc%28%29%7D?a=commitdiff_plain;h=0be9b5bddb83b0367363464347ee6175565d2436;p=lemmy.git Add allowed and blocked instances to the federated_instances response. (#1398) - Fixes #1315 --- diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 2ed3862a..161540d5 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -183,33 +183,38 @@ pub(crate) fn check_optional_url(item: &Option>) -> Result<(), Le Ok(()) } -pub(crate) async fn linked_instances(pool: &DbPool) -> Result, LemmyError> { - let mut instances: Vec = Vec::new(); - +pub(crate) async fn build_federated_instances( + pool: &DbPool, +) -> Result, LemmyError> { if Settings::get().federation.enabled { let distinct_communities = blocking(pool, move |conn| { Community::distinct_federated_communities(conn) }) .await??; - instances = distinct_communities + let allowed = Settings::get().get_allowed_instances(); + let blocked = Settings::get().get_blocked_instances(); + + let mut linked = distinct_communities .iter() .map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string())) .collect::, LemmyError>>()?; - instances.append(&mut Settings::get().get_allowed_instances()); - instances.retain(|a| { - !Settings::get().get_blocked_instances().contains(a) - && !a.eq("") - && !a.eq(&Settings::get().hostname) - }); + linked.extend_from_slice(&allowed); + linked.retain(|a| !blocked.contains(a) && !a.eq("") && !a.eq(&Settings::get().hostname)); // Sort and remove dupes - instances.sort_unstable(); - instances.dedup(); + linked.sort_unstable(); + linked.dedup(); + + Ok(Some(FederatedInstances { + linked, + allowed, + blocked, + })) + } else { + Ok(None) } - - Ok(instances) } pub async fn match_websocket_operation( diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 0475954c..8fbb8f1c 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -1,10 +1,10 @@ use crate::{ + build_federated_instances, get_user_from_jwt, get_user_from_jwt_opt, get_user_safe_settings_from_jwt, get_user_safe_settings_from_jwt_opt, is_admin, - linked_instances, version, Perform, }; @@ -324,6 +324,7 @@ impl Perform for GetSite { .unwrap_or(1); let my_user = get_user_safe_settings_from_jwt_opt(&data.auth, context.pool()).await?; + let federated_instances = build_federated_instances(context.pool()).await?; Ok(GetSiteResponse { site_view, @@ -332,7 +333,7 @@ impl Perform for GetSite { online, version: version::VERSION.to_string(), my_user, - federated_instances: linked_instances(context.pool()).await?, + federated_instances, }) } } @@ -550,6 +551,7 @@ impl Perform for TransferSite { admins.insert(0, creator_user); let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??; + let federated_instances = build_federated_instances(context.pool()).await?; Ok(GetSiteResponse { site_view: Some(site_view), @@ -558,7 +560,7 @@ impl Perform for TransferSite { online: 0, version: version::VERSION.to_string(), my_user: Some(user), - federated_instances: linked_instances(context.pool()).await?, + federated_instances, }) } } diff --git a/crates/structs/src/site.rs b/crates/structs/src/site.rs index ffe311b0..22b44d73 100644 --- a/crates/structs/src/site.rs +++ b/crates/structs/src/site.rs @@ -106,7 +106,7 @@ pub struct GetSiteResponse { pub online: usize, pub version: String, pub my_user: Option, - pub federated_instances: Vec, + pub federated_instances: Option, // Federation may be disabled } #[derive(Deserialize)] @@ -130,3 +130,10 @@ pub struct SaveSiteConfig { pub config_hjson: String, pub auth: String, } + +#[derive(Serialize)] +pub struct FederatedInstances { + pub linked: Vec, + pub allowed: Vec, + pub blocked: Vec, +}