Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(admin): hide Explore review when discovery is off (#3704)

Hampusc891c3f7b73c
diff --git a/fluxer_admin/src/api/types/instance_billing.rs b/fluxer_admin/src/api/types/instance_billing.rs
index b0245cc11..768686a78 100644
--- a/fluxer_admin/src/api/types/instance_billing.rs
+++ b/fluxer_admin/src/api/types/instance_billing.rs
@@ -133,10 +133,25 @@ pub struct InstancePremiumDiscoveryBranding {
pub premium_product_name: Option<String>,
}
-#[derive(Clone, Debug, Default, Deserialize)]
+#[derive(Clone, Debug, Deserialize)]
pub struct InstancePremiumDiscoveryFeatures {
#[serde(default)]
pub premium_enabled: bool,
+ #[serde(default = "default_true")]
+ pub discovery_enabled: bool,
+}
+
+impl Default for InstancePremiumDiscoveryFeatures {
+ fn default() -> Self {
+ Self {
+ premium_enabled: false,
+ discovery_enabled: true,
+ }
+ }
+}
+
+fn default_true() -> bool {
+ true
}
impl InstancePremiumDiscovery {
@@ -149,6 +164,7 @@ impl InstancePremiumDiscovery {
pub struct PremiumBranding {
pub name: Option<String>,
pub premium_enabled: bool,
+ pub discovery_enabled: bool,
pub product_name: Option<String>,
pub favicon_url: Option<String>,
pub favicon_dark_url: Option<String>,
@@ -161,6 +177,7 @@ impl PremiumBranding {
Self {
name: discovery.premium_product_name().map(str::to_owned),
premium_enabled: discovery.features.premium_enabled,
+ discovery_enabled: discovery.features.discovery_enabled,
product_name: non_blank(branding.product_name.as_deref()).map(str::to_owned),
favicon_url: favicon_url(
branding.favicon_url.as_deref(),
@@ -199,6 +216,7 @@ impl PremiumBranding {
Self {
name: non_blank(Some(name)).map(str::to_owned),
premium_enabled: !self_hosted || matches!(premium_mode, PremiumMode::Mirror),
+ discovery_enabled: true,
..Self::default()
}
}
@@ -217,3 +235,20 @@ fn favicon_url(favicon_url: Option<&str>, icon_url: Option<&str>) -> Option<Stri
.or_else(|| non_blank(icon_url))
.map(str::to_owned)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn branding(well_known: &str) -> PremiumBranding {
+ PremiumBranding::from_discovery(&serde_json::from_str(well_known).unwrap())
+ }
+
+ #[test]
+ fn discovery_enabled_follows_the_well_known_feature_flag() {
+ assert!(!branding(r#"{"features":{"discovery_enabled":false}}"#).discovery_enabled);
+ assert!(branding(r#"{"features":{"discovery_enabled":true}}"#).discovery_enabled);
+ assert!(branding(r#"{"features":{"premium_enabled":true}}"#).discovery_enabled);
+ assert!(branding("{}").discovery_enabled);
+ }
+}
diff --git a/fluxer_admin/src/middleware/auth.rs b/fluxer_admin/src/middleware/auth.rs
index 5116f5157..87ad5ca24 100644
--- a/fluxer_admin/src/middleware/auth.rs
+++ b/fluxer_admin/src/middleware/auth.rs
@@ -26,6 +26,12 @@ impl AuthContext {
.as_ref()
.map_or(&[], |user| user.acls.as_slice())
}
+
+ pub fn discovery_enabled(&self) -> bool {
+ self.branding
+ .as_ref()
+ .is_none_or(|branding| branding.discovery_enabled)
+ }
}
pub async fn require_auth(
diff --git a/fluxer_admin/src/routes/discovery.rs b/fluxer_admin/src/routes/discovery.rs
index 5d79885a0..258547cb4 100644
--- a/fluxer_admin/src/routes/discovery.rs
+++ b/fluxer_admin/src/routes/discovery.rs
@@ -55,7 +55,9 @@ async fn discovery_page(
_ => "pending",
};
let client = AdminApiClient::new(state.http_client(), config, &auth.0.session);
- let (pending, listed, load_error) = if tab == "pending" {
+ let (pending, listed, load_error) = if !auth.0.discovery_enabled() {
+ (None, None, None)
+ } else if tab == "pending" {
match client.list_pending_discovery_applications().await {
Ok(pending) => (Some(pending), None, None),
Err(error) => {
diff --git a/fluxer_admin/src/routes/system.rs b/fluxer_admin/src/routes/system.rs
index 1d302bb1d..44ea0b555 100644
--- a/fluxer_admin/src/routes/system.rs
+++ b/fluxer_admin/src/routes/system.rs
@@ -228,9 +228,7 @@ async fn instance_config_page(
.await
.log_error("load instance config");
if let Some(instance_config) = &instance_config {
- state.remember_premium_branding(crate::api::types::PremiumBranding::from_instance_config(
- instance_config,
- ));
+ state.remember_instance_config_branding(instance_config);
}
let limit_config = client
.get_limit_config()
diff --git a/fluxer_admin/src/routes/system_actions.rs b/fluxer_admin/src/routes/system_actions.rs
index 6aa9fc8cd..e356c7a7f 100644
--- a/fluxer_admin/src/routes/system_actions.rs
+++ b/fluxer_admin/src/routes/system_actions.rs
@@ -314,9 +314,7 @@ fn remember_premium_branding(
result: &Result<crate::api::types::InstanceConfigResponse, crate::api::client::ApiError>,
) {
if let Ok(instance_config) = result {
- state.remember_premium_branding(crate::api::types::PremiumBranding::from_instance_config(
- instance_config,
- ));
+ state.remember_instance_config_branding(instance_config);
}
}
diff --git a/fluxer_admin/src/state.rs b/fluxer_admin/src/state.rs
index a4fd4b181..3934e06df 100644
--- a/fluxer_admin/src/state.rs
+++ b/fluxer_admin/src/state.rs
@@ -3,7 +3,10 @@
use crate::{
api::{
client::{AdminApiClient, ApiResultExt},
- types::{AccountIdentityMode, AccountIdentitySettings, PremiumBranding, ReportReasonEntry},
+ types::{
+ AccountIdentityMode, AccountIdentitySettings, InstanceConfigResponse, PremiumBranding,
+ ReportReasonEntry,
+ },
},
config::AdminConfig,
};
@@ -74,6 +77,25 @@ impl AppState {
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some((Instant::now(), branding));
}
+ pub fn remember_instance_config_branding(&self, instance_config: &InstanceConfigResponse) {
+ self.merge_instance_config_branding(PremiumBranding::from_instance_config(instance_config));
+ }
+
+ fn merge_instance_config_branding(&self, branding: PremiumBranding) {
+ let mut cache = self
+ .inner
+ .premium_branding
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ if let Some((fetched_at, previous)) = cache.as_ref() {
+ let branding = PremiumBranding {
+ discovery_enabled: previous.discovery_enabled,
+ ..branding
+ };
+ *cache = Some((*fetched_at, branding));
+ }
+ }
+
pub async fn premium_branding(&self, client: &AdminApiClient) -> Option<PremiumBranding> {
if let Some(branding) = self.cached_premium_branding() {
return Some(branding);
@@ -188,3 +210,73 @@ impl axum::extract::FromRef<AppState> for AdminConfig {
state.inner.config.clone()
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::config::ProxyConfig;
+
+ fn self_hosted_state() -> AppState {
+ AppState::new(AdminConfig {
+ host: String::new(),
+ port: 0,
+ secret_key_base: String::new(),
+ base_path: String::new(),
+ api_endpoint: String::new(),
+ api_public_endpoint: String::new(),
+ media_endpoint: String::new(),
+ static_cdn_endpoint: String::new(),
+ reports_bucket_origin: String::new(),
+ admin_endpoint: String::new(),
+ web_app_endpoint: String::new(),
+ oauth_client_id: String::new(),
+ oauth_client_secret: String::new(),
+ oauth_redirect_uri: String::new(),
+ build_version: String::new(),
+ self_hosted: true,
+ proxy: ProxyConfig {
+ trust_client_ip_header: false,
+ client_ip_header_name: String::new(),
+ },
+ })
+ }
+
+ fn seed(state: &AppState, fetched_at: Instant, discovery_enabled: bool) {
+ *state.inner.premium_branding.lock().unwrap() = Some((
+ fetched_at,
+ PremiumBranding {
+ discovery_enabled,
+ ..PremiumBranding::default()
+ },
+ ));
+ }
+
+ #[test]
+ fn instance_config_branding_keeps_the_discovery_fetch_time() {
+ let state = self_hosted_state();
+ let expired = Instant::now()
+ .checked_sub(PREMIUM_BRANDING_TTL + Duration::from_secs(1))
+ .unwrap();
+ seed(&state, expired, false);
+ state.merge_instance_config_branding(PremiumBranding {
+ discovery_enabled: true,
+ product_name: Some("Example".to_owned()),
+ ..PremiumBranding::default()
+ });
+ assert_eq!(None, state.cached_premium_branding());
+ }
+
+ #[test]
+ fn instance_config_branding_updates_a_fresh_entry_and_keeps_discovery() {
+ let state = self_hosted_state();
+ seed(&state, Instant::now(), false);
+ state.merge_instance_config_branding(PremiumBranding {
+ discovery_enabled: true,
+ product_name: Some("Example".to_owned()),
+ ..PremiumBranding::default()
+ });
+ let cached = state.cached_premium_branding().unwrap();
+ assert_eq!(Some("Example"), cached.product_name.as_deref());
+ assert!(!cached.discovery_enabled);
+ }
+}
diff --git a/fluxer_admin/src/templates/layout.rs b/fluxer_admin/src/templates/layout.rs
index d8768b914..54d0c028c 100644
--- a/fluxer_admin/src/templates/layout.rs
+++ b/fluxer_admin/src/templates/layout.rs
@@ -122,7 +122,7 @@ pub fn admin_layout_ext(
body class="min-h-screen overflow-hidden bg-neutral-50" hx-boost="true" {
a href="#main-content" class="skip-link" { "Skip to main content" }
div class="flex h-[100dvh]" {
- (render_sidebar(config, &admin_title(auth.branding.as_ref()), admin_acls, active_page, options.inspected_voice_region_id, auth.pending_registration_count))
+ (render_sidebar(config, &admin_title(auth.branding.as_ref()), admin_acls, active_page, options.inspected_voice_region_id, auth.pending_registration_count, auth.discovery_enabled()))
div data-sidebar-overlay="" class="pointer-events-none fixed inset-0 z-30 bg-black/50 opacity-0 transition-opacity duration-200 ease-in-out lg:hidden" aria-hidden="true" {}
div class="flex h-[100dvh] w-full flex-1 flex-col overflow-y-auto" {
(render_header(config, auth, csrf_token))
diff --git a/fluxer_admin/src/templates/layout_sidebar.rs b/fluxer_admin/src/templates/layout_sidebar.rs
index 8e101a598..f5aefaf18 100644
--- a/fluxer_admin/src/templates/layout_sidebar.rs
+++ b/fluxer_admin/src/templates/layout_sidebar.rs
@@ -13,6 +13,7 @@ pub fn render_sidebar(
active_page: &str,
inspected_voice_region_id: Option<&str>,
pending_registration_count: Option<u32>,
+ discovery_enabled: bool,
) -> Markup {
let base = &config.base_path;
html! {
@@ -36,6 +37,7 @@ pub fn render_sidebar(
.filter(|item| {
item.active_key != "voice-servers" || inspected_voice_region_id.is_some()
})
+ .filter(|item| item.active_key != "discovery" || discovery_enabled)
.collect();
@if !visible_items.is_empty() {
div {
diff --git a/fluxer_admin/src/templates/pages/discovery.rs b/fluxer_admin/src/templates/pages/discovery.rs
index 6af3a6c97..7fa27c1c4 100644
--- a/fluxer_admin/src/templates/pages/discovery.rs
+++ b/fluxer_admin/src/templates/pages/discovery.rs
@@ -271,6 +271,17 @@ fn listed_card(
)
}
+fn discovery_disabled_page(config: &AdminConfig, auth: &AuthContext) -> Markup {
+ let content = html! {
+ (page_header("Explore", None))
+ div class="rounded-lg border border-neutral-200 bg-white p-6 text-sm text-neutral-500" {
+ "Explore is turned off on this instance. Set FLUXER_DISCOVERY_ENABLED to true to \
+ review applications."
+ }
+ };
+ admin_layout(config, auth, "Explore", "discovery", None, content)
+}
+
pub fn discovery_page(
config: &AdminConfig,
auth: &AuthContext,
@@ -280,6 +291,9 @@ pub fn discovery_page(
listed: Option<&[DiscoveryGuild]>,
load_error: Option<&str>,
) -> Markup {
+ if !auth.discovery_enabled() {
+ return discovery_disabled_page(config, auth);
+ }
let base = &config.base_path;
let admin_acls = auth.acls();
let can_review = acl::has_permission(admin_acls, acl::DISCOVERY_REVIEW);
@@ -339,3 +353,93 @@ pub fn discovery_page(
};
admin_layout(config, auth, "Explore", "discovery", None, content)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{
+ api::types::{InstancePremiumDiscovery, PremiumBranding},
+ config::ProxyConfig,
+ session::Session,
+ };
+
+ fn self_hosted_config() -> AdminConfig {
+ AdminConfig {
+ host: String::new(),
+ port: 0,
+ secret_key_base: String::new(),
+ base_path: String::new(),
+ api_endpoint: String::new(),
+ api_public_endpoint: String::new(),
+ media_endpoint: String::new(),
+ static_cdn_endpoint: String::new(),
+ reports_bucket_origin: String::new(),
+ admin_endpoint: String::new(),
+ web_app_endpoint: String::new(),
+ oauth_client_id: String::new(),
+ oauth_client_secret: String::new(),
+ oauth_redirect_uri: String::new(),
+ build_version: String::new(),
+ self_hosted: true,
+ proxy: ProxyConfig {
+ trust_client_ip_header: false,
+ client_ip_header_name: String::new(),
+ },
+ }
+ }
+
+ fn reviewer(well_known: &str) -> AuthContext {
+ let discovery: InstancePremiumDiscovery = serde_json::from_str(well_known).unwrap();
+ AuthContext {
+ session: Session {
+ user_id: "1".to_owned(),
+ access_token: String::new(),
+ created_at: 0,
+ },
+ admin_user: Some(
+ serde_json::from_value(serde_json::json!({
+ "id": "1",
+ "username": "admin",
+ "discriminator": "0001",
+ "acls": [acl::DISCOVERY_REVIEW, acl::DISCOVERY_REMOVE],
+ }))
+ .unwrap(),
+ ),
+ pending_registration_count: None,
+ branding: Some(PremiumBranding::from_discovery(&discovery)),
+ }
+ }
+
+ fn render(well_known: &str) -> String {
+ discovery_page(
+ &self_hosted_config(),
+ &reviewer(well_known),
+ "pending",
+ "csrf",
+ Some(&[]),
+ None,
+ None,
+ )
+ .into_string()
+ }
+
+ #[test]
+ fn explore_review_is_hidden_when_discovery_is_turned_off() {
+ let html = render(r#"{"features":{"discovery_enabled":false}}"#);
+ assert!(!html.contains("Explore review"));
+ assert!(!html.contains(r#"href="/discovery?tab=pending""#));
+ assert!(!html.contains("No pending applications"));
+ assert!(html.contains("Explore is turned off on this instance."));
+ }
+
+ #[test]
+ fn explore_review_shows_when_discovery_is_on() {
+ for well_known in [r#"{"features":{"discovery_enabled":true}}"#, "{}"] {
+ let html = render(well_known);
+ assert!(html.contains("Explore review"));
+ assert!(html.contains(r#"href="/discovery?tab=pending""#));
+ assert!(html.contains("No pending applications"));
+ assert!(!html.contains("Explore is turned off on this instance."));
+ }
+ }
+}