7718604268
An abstract NotificationBridge with a dart:js_interop web implementation and an honest unsupported stub; a NotificationController that reports only the platform's permission word, asks only from an explicit gesture, never re-prompts a denied answer, and notifies once per run+kind+finished marker under its own persisted ledger key. One reusable native NotificationPermissionTile for Settings, an isolated browser probe, and focused tests. No shell/settings wiring.
92 lines
3.6 KiB
Dart
92 lines
3.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../domain/attention_transitions.dart';
|
|
|
|
/// This client's own storage key for the notification ledger, namespaced like
|
|
/// the rest of the app. It is deliberately not the legacy web key, so neither
|
|
/// client rewrites the other's local state on a shared origin.
|
|
const notificationStorageKey = 'pipelines.next.notify.v1';
|
|
|
|
/// The marker that separates two transitions of one run and kind: the server's
|
|
/// own `finished_at` when it recorded one, else the run's creation time — the
|
|
/// same recency the attention surface orders by.
|
|
///
|
|
/// A run that is retried and fails again carries a new marker, so its new
|
|
/// failure is a new identity and can notify once more; the same failure can
|
|
/// never notify twice.
|
|
String notificationMarker(JsonMap run) {
|
|
final finished = '${run['finished_at'] ?? ''}'.trim();
|
|
return finished.isNotEmpty ? finished : '${run['created_at'] ?? ''}'.trim();
|
|
}
|
|
|
|
/// The identity of one transition: run, kind and marker. The unit of dedupe.
|
|
String notificationIdentity(AttentionTransition transition) =>
|
|
'${transition.key}@${notificationMarker(transition.run)}';
|
|
|
|
/// What this client has already announced, plus whether the operator has ever
|
|
/// been asked.
|
|
///
|
|
/// The ledger is pure: it records identities and decides whether one is new.
|
|
/// Persistence is the store's job.
|
|
///
|
|
/// An identity is never forgotten while the ledger lives, so publishing the same
|
|
/// transition list again is always idempotent. A genuinely new transition of the
|
|
/// same run and kind is a new identity because its marker changed (see
|
|
/// [notificationMarker]); the browser's in-session prune is deliberately not
|
|
/// reproduced, because a prune would let an already-announced transition that is
|
|
/// still in the caller's list be posted a second time.
|
|
class NotificationLedger {
|
|
const NotificationLedger({
|
|
this.notified = const <String>{},
|
|
this.asked = false,
|
|
});
|
|
|
|
/// The transition identities already recorded, in no particular order.
|
|
final Set<String> notified;
|
|
|
|
/// Whether the operator has ever been asked for permission.
|
|
final bool asked;
|
|
|
|
bool hasNotified(String identity) => notified.contains(identity);
|
|
|
|
/// The ledger with [identity] recorded.
|
|
NotificationLedger recording(String identity) =>
|
|
NotificationLedger(notified: {...notified, identity}, asked: asked);
|
|
|
|
NotificationLedger withAsked(bool value) =>
|
|
NotificationLedger(notified: notified, asked: value);
|
|
}
|
|
|
|
/// The one-line JSON the ledger is stored as. The identities are sorted so an
|
|
/// unchanged ledger always encodes to the same bytes.
|
|
String encodeNotificationLedger(NotificationLedger ledger) {
|
|
final identities = ledger.notified.where((id) => id.isNotEmpty).toList()
|
|
..sort();
|
|
return jsonEncode({'asked': ledger.asked, 'notified': identities});
|
|
}
|
|
|
|
/// Decodes a stored value into a ledger. Total: blank text, unreadable JSON, a
|
|
/// non-map, a non-boolean `asked` and non-string members all decode to the
|
|
/// members that can be read, and a malformed value never throws.
|
|
NotificationLedger decodeNotificationLedger(String? stored) {
|
|
final text = (stored ?? '').trim();
|
|
if (text.isEmpty) return const NotificationLedger();
|
|
dynamic parsed;
|
|
try {
|
|
parsed = jsonDecode(text);
|
|
} catch (_) {
|
|
return const NotificationLedger();
|
|
}
|
|
if (parsed is! Map) return const NotificationLedger();
|
|
final raw = parsed['notified'];
|
|
return NotificationLedger(
|
|
notified: {
|
|
if (raw is List)
|
|
for (final value in raw)
|
|
if (value is String && value.isNotEmpty) value,
|
|
},
|
|
asked: parsed['asked'] == true,
|
|
);
|
|
}
|