]> Untitled Git - lemmy.git/commitdiff
Add allowed and blocked instances to the federated_instances response. (#1398)
authorDessalines <dessalines@users.noreply.github.com>
Mon, 1 Feb 2021 18:11:37 +0000 (13:11 -0500)
committerGitHub <noreply@github.com>
Mon, 1 Feb 2021 18:11:37 +0000 (13:11 -0500)
- Fixes #1315

crates/api/src/lib.rs
crates/api/src/site.rs
crates/structs/src/site.rs

index 2ed3862aed48ebfd5f15dc0948b5c2eabe105789..161540d53aac423be7ff2dcf46255b13c25f8f9e 100644 (file)
@@ -183,33 +183,38 @@ pub(crate) fn check_optional_url(item: &Option<Option<String>>) -> Result<(), Le
   Ok(())
 }
 
-pub(crate) async fn linked_instances(pool: &DbPool) -> Result<Vec<String>, LemmyError> {
-  let mut instances: Vec<String> = Vec::new();
-
+pub(crate) async fn build_federated_instances(
+  pool: &DbPool,
+) -> Result<Option<FederatedInstances>, 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::<Result<Vec<String>, 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(
index 0475954cd6d21256891e917e021cdf6c0b85bc6d..8fbb8f1c5ecd1ef4346932329b7dfc0ca2b3392f 100644 (file)
@@ -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,
     })
   }
 }
index ffe311b0aaaf53d7b30e2236e27df8b6b31532e6..22b44d73ce0efb98341e7b02b8880769a192c6b3 100644 (file)
@@ -106,7 +106,7 @@ pub struct GetSiteResponse {
   pub online: usize,
   pub version: String,
   pub my_user: Option<UserSafeSettings>,
-  pub federated_instances: Vec<String>,
+  pub federated_instances: Option<FederatedInstances>, // 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<String>,
+  pub allowed: Vec<String>,
+  pub blocked: Vec<String>,
+}