support for registering pushers
This commit is contained in:
parent
88cdcf9863
commit
fe871217b2
21 changed files with 350 additions and 22 deletions
106
lib/controllers/unified_push.dart
Normal file
106
lib/controllers/unified_push.dart
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
import "dart:io";
|
||||
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:intl/intl.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/models/requests/register_pusher.dart";
|
||||
import "package:unifiedpush/unifiedpush.dart";
|
||||
import "package:unifiedpush_storage_shared_preferences/storage.dart";
|
||||
|
||||
class UnifiedPushController extends AsyncNotifier<bool> {
|
||||
@override
|
||||
Future<bool> build() async {
|
||||
final registered = await UnifiedPush.initialize(
|
||||
linuxOptions: .new(
|
||||
dbusName: "nexus.federated.nexus",
|
||||
storage: UnifiedPushStorageSharedPreferences(),
|
||||
background: isInBackground,
|
||||
),
|
||||
onNewEndpoint: (endpoint, instance) async {
|
||||
final client = ref.watch(ClientController.provider.notifier);
|
||||
await client.registerPusher(
|
||||
.new(
|
||||
appDisplayName: "Nexus",
|
||||
appId: "nexus.federated.nexus",
|
||||
data: PusherData.webPush(
|
||||
url: Uri.parse(endpoint.url),
|
||||
auth: endpoint.pubKeySet!.auth,
|
||||
),
|
||||
deviceDisplayName:
|
||||
"Nexus on ${toBeginningOfSentenceCase(Platform.operatingSystem)}",
|
||||
kind: .webPush,
|
||||
lang: "en",
|
||||
pushKey: endpoint.pubKeySet!.pubKey,
|
||||
),
|
||||
|
||||
// Pusher(
|
||||
// appDisplayName: ,
|
||||
// data: {
|
||||
// "url": endpoint.url,
|
||||
// "auth": endpoint.pubKeySet.auth,
|
||||
// "format": "event_id_only",
|
||||
// },
|
||||
// deviceDisplayName: "Nexus",
|
||||
// kind: "org.matrix.msc4174.webpush",
|
||||
// lang: WidgetsBinding.instance.platformDispatcher.locale
|
||||
// .toLanguageTag(),
|
||||
// pushkey: endpoint.pubKeySet.publicKey,
|
||||
// ),
|
||||
);
|
||||
},
|
||||
onMessage: (message, instance) async {
|
||||
print(message);
|
||||
// TODO: Handle incoming message
|
||||
},
|
||||
onRegistrationFailed: (e, r) {
|
||||
throw "e";
|
||||
},
|
||||
onUnregistered: (_) {
|
||||
throw "r";
|
||||
},
|
||||
onTempUnavailable: (_) {
|
||||
throw "t";
|
||||
},
|
||||
);
|
||||
|
||||
if (registered) {
|
||||
// Needs to be registered every startup
|
||||
await register(true);
|
||||
}
|
||||
|
||||
return registered;
|
||||
}
|
||||
|
||||
Future<void> register([bool early = false]) async {
|
||||
final clientState = ref.watch(ClientStateController.provider);
|
||||
if (clientState?.deviceId == null) ref.invalidateSelf();
|
||||
|
||||
final alreadyRegistered = early ? true : await future;
|
||||
final client = ref.watch(ClientController.provider.notifier);
|
||||
final capabilities = await client.getCapabilities();
|
||||
|
||||
if (capabilities.webpush?.vapid == null) {
|
||||
throw UnsupportedError(
|
||||
"Your homeserver does not support MSC4174 (Web Push), and therefore cannot send notifications to Nexus.",
|
||||
);
|
||||
}
|
||||
|
||||
if (!alreadyRegistered &&
|
||||
!await UnifiedPush.tryUseCurrentOrDefaultDistributor()) {
|
||||
throw Exception("No UnifiedPush distributors found");
|
||||
}
|
||||
|
||||
await UnifiedPush.register(
|
||||
instance: clientState!.deviceId!,
|
||||
vapid: capabilities.webpush?.vapid,
|
||||
);
|
||||
|
||||
if (!alreadyRegistered) ref.invalidateSelf();
|
||||
}
|
||||
|
||||
static final provider = AsyncNotifierProvider<UnifiedPushController, bool>(
|
||||
UnifiedPushController.new,
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue