Add allowed and blocked instances to the federated_instances response. (#1398)

- Fixes #1315
This commit is contained in:
Dessalines 2021-02-01 13:11:37 -05:00 committed by GitHub
parent 6bb4f0b41f
commit 0be9b5bddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 18 deletions

View 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(instances)
Ok(Some(FederatedInstances {
linked,
allowed,
blocked,
}))
} else {
Ok(None)
}
}
pub async fn match_websocket_operation(

View 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,
})
}
}

View 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>,
}