add UnifiedPushAllowedController

This commit is contained in:
Henry Hiles 2026-08-23 14:26:51 -04:00
commit 7218eb5195
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
3 changed files with 67 additions and 16 deletions

View file

@ -11,6 +11,7 @@ import "package:nexus/controllers/client_state.dart";
import "package:nexus/controllers/settings.dart"; import "package:nexus/controllers/settings.dart";
import "package:nexus/controllers/unified_push.dart"; import "package:nexus/controllers/unified_push.dart";
import "package:nexus/controllers/spec_versions.dart"; import "package:nexus/controllers/spec_versions.dart";
import "package:nexus/controllers/unified_push_allowed.dart";
import "package:nexus/models/account_data.dart"; import "package:nexus/models/account_data.dart";
import "package:nexus/models/settings_category.dart"; import "package:nexus/models/settings_category.dart";
import "package:nexus/main.dart"; import "package:nexus/main.dart";
@ -100,7 +101,6 @@ class SettingsSectionsController
title: "Notifications", title: "Notifications",
icon: Icons.notifications, icon: Icons.notifications,
settings: .new([ settings: .new([
// TODO: Disable if not supported
.new( .new(
title: "Push notifications", title: "Push notifications",
description: "Enable push notifications using Web Push", description: "Enable push notifications using Web Push",
@ -109,26 +109,43 @@ class SettingsSectionsController
final pusherRegistered = ref.watch( final pusherRegistered = ref.watch(
UnifiedPushController.provider, UnifiedPushController.provider,
); );
final unifiedPushAllowed = ref.watch(
UnifiedPushAllowedController.provider,
);
return SwitchListTile( return SwitchListTile(
title: Text(title), title: Text(title),
subtitle: Text(description), subtitle: Text(
unifiedPushAllowed.maybeWhen(
data: (data) => data,
orElse: () => null,
) ??
description,
),
secondary: Icon(icon), secondary: Icon(icon),
value: pusherRegistered.maybeWhen( value: pusherRegistered.maybeWhen(
data: (value) => value, data: (value) => value,
orElse: () => false, orElse: () => false,
), ),
onChanged: pusherRegistered.maybeWhen( onChanged:
data: (_) => unifiedPushAllowed.maybeWhen(
(value) => value data: (data) => data == null,
? ref orElse: () => false,
.watch( )
UnifiedPushController.provider.notifier, ? pusherRegistered.maybeWhen(
) data: (_) =>
.register() (value) => value
.onError(showError) ? ref
: /*deregeister*/ null, .watch(
orElse: () => null, UnifiedPushController
), .provider
.notifier,
)
.register()
.onError(showError)
: /*deregeister*/ null,
orElse: () => null,
)
: null,
); );
}, },
), ),

View file

@ -78,8 +78,9 @@ class UnifiedPushController extends AsyncNotifier<bool> {
if (clientState?.deviceId == null) ref.invalidateSelf(); if (clientState?.deviceId == null) ref.invalidateSelf();
final alreadyRegistered = early ? true : await future; final alreadyRegistered = early ? true : await future;
final client = ref.watch(ClientController.provider.notifier); final capabilities = await ref
final capabilities = await client.getCapabilities(); .watch(ClientController.provider.notifier)
.getCapabilities();
if (capabilities.webpush?.vapid == null) { if (capabilities.webpush?.vapid == null) {
throw UnsupportedError( throw UnsupportedError(

View file

@ -0,0 +1,33 @@
import "dart:io";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:nexus/controllers/client.dart";
import "package:unifiedpush/unifiedpush.dart";
class UnifiedPushAllowedController extends AsyncNotifier<String?> {
@override
Future<String?> build() async {
if (!await UnifiedPush.tryUseCurrentOrDefaultDistributor()) {
return "No valid distributors found. Try installing ${Platform.isLinux
? "KUnifiedPush"
: Platform.isAndroid
? "FCM or NTFY"
: "NTFY"}.";
}
final capabilities = await ref
.watch(ClientController.provider.notifier)
.getCapabilities();
if (capabilities.webpush?.vapid == null) {
return "Your homeserver does not support MSC4174 (Web Push), and therefore cannot send notifications to Nexus.";
}
return null;
}
static final provider =
AsyncNotifierProvider.autoDispose<UnifiedPushAllowedController, String?>(
UnifiedPushAllowedController.new,
);
}