Mobile app (Android):
- Sign-in to any server, with authenticator codes. The server address and
token are kept in secure storage; a dropped connection never signs out.
- Workspaces, recent pages, spaces and a foldable page tree; search; the
inbox with mark-read.
- Pages open read-only in the shared editor from their saved content and
join the live document to edit (with the native toolbar). Page links in
the text open in the app (new "navigate" bridge message).
- Comments: threads, replies, resolve.
- "Save to Docwell" in the Android share sheet makes a page from shared
text, with web addresses as links.
- Offline reading: every answer the app reads is kept on the phone, and
used when the server can't be reached.
- Push through UnifiedPush (ntfy, NextPush, ...). The app uses the Android
connector directly; the umbrella package pulls in a native crypto build
for Linux that breaks `dart run` and tests.
Server:
- /me/devices registers a phone's UnifiedPush endpoint and Web Push keys
(new push_devices collection). Each notification is also pushed,
encrypted with RFC 8291 aes128gcm so the distributor can't read it; the
app drops messages that don't decrypt. Endpoints answering 404/410 are
forgotten. Endpoints follow the webhook address rules.
- /collab accepts the apps' own origins ("null" for the mobile editor page,
wails:// for the desktop app). Sign-in is a token in the first message,
not a cookie, so this opens no cross-site hijacking.
Tried on an Android 15 emulator: sign-in, browsing, reading, live editing,
sharing and offline reading. Not covered: push end to end (no distributor
on the emulator), iOS, and YouTube embeds, which refuse to play from the
app's local editor page (error 153).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
5.5 KiB
Dart
167 lines
5.5 KiB
Dart
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:unifiedpush_platform_interface/data/push_endpoint.dart';
|
|
import 'package:unifiedpush_platform_interface/data/push_message.dart';
|
|
import 'package:unifiedpush_platform_interface/unifiedpush_platform_interface.dart';
|
|
|
|
import 'api/docwell_api.dart' hide PushMessage;
|
|
|
|
/// Push notifications through UnifiedPush (Android). The phone's distributor
|
|
/// app (ntfy, NextPush, …) gives this app an endpoint; docwell-server sends
|
|
/// notifications there, encrypted to this app's Web Push keys. Messages that
|
|
/// don't decrypt are dropped, so knowing the endpoint is not enough to send
|
|
/// a notification.
|
|
class PushService extends ChangeNotifier {
|
|
PushService({FlutterSecureStorage? storage})
|
|
: _storage = storage ?? const FlutterSecureStorage();
|
|
|
|
static const _instance = 'docwell';
|
|
final FlutterSecureStorage _storage;
|
|
final _local = FlutterLocalNotificationsPlugin();
|
|
DocwellApi? _api;
|
|
void Function(String route)? _open;
|
|
|
|
/// Whether this phone gets push notifications (it registered an endpoint).
|
|
bool get enabled => _deviceId != null;
|
|
String? _deviceId;
|
|
|
|
/// Why push can't be turned on, e.g. no distributor installed.
|
|
String? problem;
|
|
|
|
bool get supported => !kIsWeb && Platform.isAndroid;
|
|
|
|
/// Sets up the handlers. [open] receives the app route of a tapped
|
|
/// notification.
|
|
Future<void> init(void Function(String route) open) async {
|
|
_open = open;
|
|
if (!supported) return;
|
|
_deviceId = await _storage.read(key: 'push_device');
|
|
await _local.initialize(
|
|
settings: const InitializationSettings(
|
|
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
|
),
|
|
onDidReceiveNotificationResponse: (r) {
|
|
final route = r.payload;
|
|
if (route != null && route.startsWith('/')) _open?.call(route);
|
|
},
|
|
);
|
|
// A tap that started the app.
|
|
final launch = await _local.getNotificationAppLaunchDetails();
|
|
final route = launch?.notificationResponse?.payload;
|
|
if (launch?.didNotificationLaunchApp == true && route != null) {
|
|
Future.microtask(() => _open?.call(route));
|
|
}
|
|
await UnifiedPushPlatform.instance.initializeCallback(
|
|
onNewEndpoint: (e, _) => _registerEndpoint(e),
|
|
onUnregistered: (_) => _forget(),
|
|
onMessage: (m, _) => _show(m),
|
|
);
|
|
}
|
|
|
|
/// The signed-in session: endpoints get registered with this server.
|
|
set api(DocwellApi? api) {
|
|
_api = api;
|
|
// Endpoints can change; the connector reports them again on register.
|
|
if (api != null && enabled) _register();
|
|
}
|
|
|
|
/// Turns push on. Returns false (and sets [problem]) when the phone has
|
|
/// no UnifiedPush distributor.
|
|
Future<bool> enable() async {
|
|
if (!supported) return false;
|
|
problem = null;
|
|
final ok = await UnifiedPushPlatform.instance
|
|
.tryUseCurrentOrDefaultDistributor();
|
|
if (!ok) {
|
|
final all = await UnifiedPushPlatform.instance.getDistributors(const []);
|
|
if (all.isEmpty) {
|
|
problem =
|
|
'Install a UnifiedPush distributor such as ntfy to get notifications.';
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
await UnifiedPushPlatform.instance.saveDistributor(all.first);
|
|
}
|
|
await _local
|
|
.resolvePlatformSpecificImplementation<
|
|
AndroidFlutterLocalNotificationsPlugin
|
|
>()
|
|
?.requestNotificationsPermission();
|
|
await _register();
|
|
return true;
|
|
}
|
|
|
|
Future<void> _register() =>
|
|
UnifiedPushPlatform.instance.register(_instance, const [], null, null);
|
|
|
|
/// Turns push off on this phone and tells the server to stop.
|
|
Future<void> disable() async {
|
|
final id = _deviceId;
|
|
if (id != null) {
|
|
try {
|
|
await _api?.removeDevice(id);
|
|
} catch (_) {
|
|
// The server forgets endpoints that stop answering anyway.
|
|
}
|
|
}
|
|
if (supported) await UnifiedPushPlatform.instance.unregister(_instance);
|
|
await _forget();
|
|
}
|
|
|
|
Future<void> _registerEndpoint(PushEndpoint e) async {
|
|
final keys = e.pubKeySet;
|
|
final api = _api;
|
|
if (keys == null || api == null) return;
|
|
try {
|
|
final device = await api.registerDevice(
|
|
endpoint: e.url,
|
|
p256dh: keys.pubKey,
|
|
auth: keys.auth,
|
|
name: 'Android',
|
|
);
|
|
_deviceId = device.id;
|
|
await _storage.write(key: 'push_device', value: device.id);
|
|
notifyListeners();
|
|
} catch (err) {
|
|
problem = 'The server did not accept this phone: $err';
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> _forget() async {
|
|
_deviceId = null;
|
|
await _storage.delete(key: 'push_device');
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> _show(PushMessage m) async {
|
|
if (!m.decrypted) return;
|
|
final Map<String, dynamic> msg;
|
|
try {
|
|
msg = jsonDecode(utf8.decode(m.content)) as Map<String, dynamic>;
|
|
} catch (_) {
|
|
return;
|
|
}
|
|
await _local.show(
|
|
id: DateTime.now().millisecondsSinceEpoch & 0x7fffffff,
|
|
title: msg['title'] as String? ?? 'Docwell',
|
|
body: msg['body'] as String? ?? '',
|
|
notificationDetails: const NotificationDetails(
|
|
android: AndroidNotificationDetails(
|
|
'activity',
|
|
'Comments and mentions',
|
|
channelDescription: 'Replies, mentions and pages shared with you',
|
|
importance: Importance.defaultImportance,
|
|
),
|
|
),
|
|
payload: msg['route'] as String?,
|
|
);
|
|
}
|
|
}
|