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(
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,
};
.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,
online,
version: version::VERSION.to_string(),
my_user,
- federated_instances: linked_instances(context.pool()).await?,
+ federated_instances,
})
}
}
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),
online: 0,
version: version::VERSION.to_string(),
my_user: Some(user),
- federated_instances: linked_instances(context.pool()).await?,
+ federated_instances,
})
}
}
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)]
pub config_hjson: String,
pub auth: String,
}
+
+#[derive(Serialize)]
+pub struct FederatedInstances {
+ pub linked: Vec<String>,
+ pub allowed: Vec<String>,
+ pub blocked: Vec<String>,
+}