mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
## What An Android push wake that runs out of time can leave a claim still running. If that claim then wins, the message is recorded as shown but was never returned. No later wake finds it, so the user is never told. This makes the wake report that case, and the native handler shows its generic notification for it. - `resolvePushWake` tracks the message each community is claiming. After the claim grace it sets `PushWakeResult.unreported` when a claim is still running for a message with something to show (a message whose body is empty would not be returned anyway). It is read in the same synchronous step as the claimed-message snapshot, so a claim that finishes before the snapshot is returned, and one that finishes after it is counted. - `PushWakeRunner` reports `unreported` when any pass does, because a message lost in one pass stays lost. - The channel payload carries `unreported`. The native handler posts the named notifications, and the generic notification beside them, silently when something already alerted. If the user already cleared the generic card this wake posted at 2 seconds, it stays cleared. The generic is marked as standing in for a lost message, so later wakes, which clear an unmarked generic as before, leave it until the user clears it. A later wake that re-posts the generic keeps the mark. - It stays separate from `incomplete`, which means a later wake retries. Folding the two would show the generic beside named notifications on every retryable partial wake. - `ShownMessageStore.claim` documents that a caller that stops waiting for a claim must report it. A claim that later returns false still costs one unnecessary generic notification, which is accepted. The realistic trigger is storage that stalls after the claim marker is created, during the watermark and prune I/O in `DirectoryShownMessageStore.claim` (a pre-existing finding from the review of the Android wake handler). The store's order is unchanged. The marker has to come first for atomicity, and this signal turns the stall into a generic notification instead of silence. ## Validation - The push-wake journey test's stalled-storage step now asserts `unreported`, and a stalled claim for a message with an empty body asserts it is not set. A wake cut short in the name lookup, outside any claim, asserts it is not set and still returns its messages. The runner step asserts that a message lost in one pass is still reported after a clean rerun. The channel payload test covers both values. - Forcing the signal off fails the stalled-storage step. - `WakeNotifierGenericTest` (instrumented) drives the generic notification against the real notification manager on a Pixel 9a: an unmarked card clears on a clean wake, a kept card survives clean wakes and a later 2-second re-post, and a card the user cleared before the result is not posted again. Each step waits for the state it expects, a re-post check waits for a newer post, and checks that nothing changed run after a barrier notification. Its two updates wait 700 ms first, so Android's update rate limit can't drop them on quick reruns (12 back-to-back runs passed). Removing the cleared-card check, or dropping the mark on re-post, fails it. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
136 lines
5.0 KiB
Dart
136 lines
5.0 KiB
Dart
import 'dart:io';
|
|
import 'dart:ui';
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import '../community/community.dart';
|
|
import '../hosted/builderlab_account.dart';
|
|
import '../http/app_http_client.dart';
|
|
import '../hosted/hosted_relay_auth.dart';
|
|
import '../hosted/organization_environment.dart';
|
|
import '../relay/relay_provider.dart';
|
|
import 'push_wake_resolver.dart';
|
|
import 'push_wake_runner.dart';
|
|
import 'shown_message_store.dart';
|
|
|
|
/// The channel the native wake handler runs a wake over. The handler starts
|
|
/// this engine, waits for [wakeReadyMethod], then asks for each wake with
|
|
/// [wakeMethod] and posts the notifications of the answer.
|
|
const androidPushWakeChannel = MethodChannel('buzz/push_wake');
|
|
const wakeReadyMethod = 'ready';
|
|
const wakeMethod = 'wake';
|
|
|
|
/// The time the native handler has left for a wake's lookup, which it sends
|
|
/// as `remainingMillis`, or null when it sends none.
|
|
Duration? wakeRemaining(Object? arguments) {
|
|
final millis = arguments is Map ? arguments['remainingMillis'] : null;
|
|
return millis is int ? Duration(milliseconds: millis < 0 ? 0 : millis) : null;
|
|
}
|
|
|
|
/// What a wake sends the native handler.
|
|
///
|
|
/// The handler may ask for another wake while one runs. [PushWakeRunner] folds
|
|
/// it into the run in progress, so only the call that started the run gets
|
|
/// the notifications, once. A call that joined it gets [joinedWake], which
|
|
/// tells the handler to post nothing for it.
|
|
const joinedWake = <String, Object?>{'joined': true};
|
|
|
|
Map<String, Object?> pushWakeChannelPayload(PushWakeResult result) => {
|
|
'notifications': [
|
|
for (final notification in result.notifications)
|
|
{
|
|
'eventId': notification.eventId,
|
|
'createdAtMillis': notification.createdAt.millisecondsSinceEpoch,
|
|
'title': notification.title,
|
|
'body': notification.body,
|
|
'conversationId': notification.conversationId,
|
|
'conversationTitle': ?notification.conversationTitle,
|
|
'category': notification.category.name,
|
|
'target': notification.target.toMap(),
|
|
},
|
|
],
|
|
'incomplete': result.incomplete,
|
|
'unreported': result.unreported,
|
|
};
|
|
|
|
/// Answers the native handler's wake requests from a [PushWakeRunner].
|
|
class PushWakeChannelHandler {
|
|
PushWakeChannelHandler(this._runner);
|
|
|
|
final PushWakeRunner _runner;
|
|
Future<PushWakeResult>? _latest;
|
|
|
|
/// Runs a wake, or joins the one in progress, within [remaining] when the
|
|
/// handler sends it.
|
|
Future<Map<String, Object?>> wake({Duration? remaining}) {
|
|
final run = _runner.wake(remaining: remaining);
|
|
// The runner hands every caller of one run the same future.
|
|
if (identical(run, _latest)) return run.then((_) => joinedWake);
|
|
_latest = run;
|
|
return run.then(pushWakeChannelPayload);
|
|
}
|
|
}
|
|
|
|
/// Entry point of the background engine the native wake handler starts.
|
|
///
|
|
/// It runs headless: no UI and no Riverpod state. The foreground app never
|
|
/// runs this, since the handler does nothing while the app is in the
|
|
/// foreground.
|
|
Future<void> runAndroidPushWake() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
DartPluginRegistrant.ensureInitialized();
|
|
// Each wake runs in a new engine, and a Cronet engine opened in it would
|
|
// outlive it.
|
|
useDartIoHttpOnly();
|
|
final store = await _openShownMessageStore();
|
|
final relayHeaders = _headlessRelayHeaders();
|
|
final handler = PushWakeChannelHandler(
|
|
PushWakeRunner(
|
|
(budget) => resolvePushWake(
|
|
store: store,
|
|
relayHeaders: relayHeaders,
|
|
timeouts: PushWakeTimeouts(total: budget),
|
|
),
|
|
),
|
|
);
|
|
androidPushWakeChannel.setMethodCallHandler(
|
|
(call) => switch (call.method) {
|
|
wakeMethod => handler.wake(remaining: wakeRemaining(call.arguments)),
|
|
_ => throw MissingPluginException('Unknown ${call.method}'),
|
|
},
|
|
);
|
|
await androidPushWakeChannel.invokeMethod<void>(wakeReadyMethod);
|
|
}
|
|
|
|
Future<ShownMessageStore> _openShownMessageStore() async {
|
|
final support = await getApplicationSupportDirectory();
|
|
return DirectoryShownMessageStore(
|
|
Directory('${support.path}/shown_messages'),
|
|
);
|
|
}
|
|
|
|
/// Headers for the relays that require the account's federated identity
|
|
/// (NIP-FI), read the way the foreground app reads them. The wake is a
|
|
/// background caller: it mints its own assertion, never records a failure for
|
|
/// the foreground to show, and sends none when it can't get one, so the relay
|
|
/// decides.
|
|
Future<Map<String, String>> Function(Community) _headlessRelayHeaders() {
|
|
final organizationOrigin = OrganizationEnvironment.origin;
|
|
// The foreground app owns these session records; a wake only reads them.
|
|
final auth = HostedRelayAuth(
|
|
BuilderlabAccountClient(readOnlySession: true),
|
|
organization: organizationOrigin == null
|
|
? null
|
|
: BuilderlabAccountClient(
|
|
accountOrigin: organizationOrigin,
|
|
readOnlySession: true,
|
|
),
|
|
);
|
|
return (community) => auth.headers(
|
|
RelayConfig(baseUrl: community.relayUrl, nsec: community.nsec),
|
|
background: true,
|
|
);
|
|
}
|