CI / go (Server) (push) Failing after 4m38s
CI / go (Web-App) (push) Successful in 12m18s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / frontend (push) Failing after 7m49s
CI / desktop (ubuntu-latest) (push) Successful in 1h25m30s
CI / mobile (push) Failing after 5m14s
CI / docker (push) Successful in 39m26s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Databases (ADR-13 follow-ups):
- Relation properties link records to another database, on the same page
or on any page the reader can open (read live and read-only, like synced
pages). Cells list the linked titles; clicking one opens the record.
- Rollup properties work out counts, sums, averages, medians, min/max,
ranges, earliest/latest dates and checked counts over related records.
Neither is stored: views compute them before drawing (linked.ts), so they
follow both databases live, and rollups sort and filter as numbers.
- Exports (Markdown, HTML, PDF, Word) resolve them for same-page databases.
- A record opens as a full page at /p/<page>/r/<database>/<record>, with
"Open as full page" on the side panel and "Back to the page".
Edgeless mode (ADR-14 follow-ups):
- History previews show a version's canvas and count canvas changes.
- Comments can be anchored to canvas elements (anchor {element}); elements
with open threads show a badge that opens the thread.
- Workspaces choose how new pages open (workspaces.default_page_mode); the
per-device choice now defaults to following the workspace.
- Phones edit the canvas once a page is opened for editing; the page note
shows the page title.
Languages:
- The admin panel speaks English, Polish, German, French and Spanish, with
a picker sharing the web app's setting and a key parity test.
- The phone app speaks the same five, follows the phone's language (and
Android's per-app language setting), and has its own parity test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
5.4 KiB
Dart
167 lines
5.4 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;
|
|
import 'l10n.dart';
|
|
|
|
/// 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 = tr('pushNoDistributor');
|
|
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 = tr('pushRefused', {'error': 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: NotificationDetails(
|
|
android: AndroidNotificationDetails(
|
|
'activity',
|
|
tr('pushChannel'),
|
|
channelDescription: tr('pushChannelHint'),
|
|
importance: Importance.defaultImportance,
|
|
),
|
|
),
|
|
payload: msg['route'] as String?,
|
|
);
|
|
}
|
|
}
|