mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
## Summary Turning push notifications off could stay stuck forever. If the relay kept rejecting the opt-out tombstone, `setPushNotificationsEnabled(false)` swallowed the rejection, and the push bootstrap retried the tombstone on every connect, including after restarts. Settings showed the switch off the whole time, while the relay still held the lease. [docs/mobile-push-suppression.md](https://github.com/block/buzz/blob/main/docs/mobile-push-suppression.md) (rule 29) asks for the opposite: a bounded pending state, then a clear warning that notifications may continue, with no automatic retry, and the relay's actual setting on the next sync. - **Bounded pending.** An opt-out ends about 10 seconds after the tap, across every step (capability lookup, connect, authenticate, publish). While it runs, the row says "Turning off…" and the switch is held, so Off, On, Off can't race the first attempt. - **Definite vs. maybe stored.** A one-shot publish now says whether the relay could have seen the event: a refusal, or a failure before the EVENT went out, is definite; a lost acknowledgment after it went out is not. - **Refused: back to the confirmed setting.** When the relay never stored the tombstone, the saved setting goes back to On (the confirmed lease) and Settings shows "Could not confirm that notifications were turned off. Notifications may continue." with a **Retry** below it. The warning lasts for this app run; after a restart, Settings just shows On. - **Maybe stored: kept as a guard, then read back.** When the acknowledgment was lost, the saved opt-out stays as a guard (no lease or enrollment is published, and the community stays in the iOS notification snapshot), while Settings shows the confirmed On with the same warning. On the next sync, the app reads its own lease from the relay: an inactive lease or none confirms Off; an active one turns the switch back On and keeps the warning. A read that fails changes nothing and is tried again on the next connect. The read asks the relay for strong consistency, so a lagging replica can't answer with the lease the tombstone replaced. It fails if the active community changes while it waits, and it settles only the opt-out it was started for, so a Retry made during the read stays pending. - **No flash on success.** Settings reads the community from the community list, which changes in the same step as each opt-out result. The active-community projection trails it by a keychain read, which could show an accepted opt-out as failed for a frame. - **Explicit retry only.** Retry, or turning the switch off again, makes one attempt at a new generation. The bootstrap's automatic tombstone retry is removed. The warning is announced to screen readers when it appears, and Retry has its own label. This applies to every platform: the opt-out path is shared. ### Related issue None found. ### Testing - The Settings recovery journey covers a refusal restoring On with the warning, a restart that drops the warning and makes no attempt, an attempt that never answers (held switch, then the deadline, kept as a guard), a restart that keeps that warning, and a Retry that succeeds. It runs with loading, absent and failed capability, and on a small phone at text scale 2. - The toggle journey, with the real push bootstrap, covers a refusal (a reconnect neither retries nor reads), an unanswered attempt and a lease that's still active: the read on reconnect, a failed read that changes nothing, then Off or On from what the relay holds. - One-shot publish rows: a relay that stalls at authentication fails as never sent; one that stalls at OK, or closes after the EVENT, fails as maybe stored, all by the deadline. - Lease reader rows: the exact strong-read filter, an empty answer, and a community switch during the descriptor fetch or the query. A provider row holds a read while a Retry fails unanswered: the old read leaves the new opt-out pending. - A widget test with slow secure storage checks every frame of an accepted opt-out for the warning. - A relay database test reads back the author's inactive replacement lease with the filter the app uses, and the lease decryption is checked against the member's own and forged events. ### Screenshots After the relay refuses the opt-out (no relay push capability, so the toggle is reachable only because push is on). | Before | After | | --- | --- | | <img src="https://raw.githubusercontent.com/block/buzz/d2e3c753b3068a68728199096bf30e45e8948af7/pr-9854--before.png" width="300" alt="Before: switch shows off while notifications may continue"> | <img src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--after.png" width="300" alt="After: switch back on, with the warning and Retry below it"> | At text scale 2 on a small phone, Retry sits below the warning: <img src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--after-large-text.png" width="250" alt="Large text: warning readable, Retry below it"> While an opt-out attempt is in flight (new), the switch is held: <img src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--turning-off.png" width="300" alt="Notifications row with the subtitle Turning off and the switch held"> --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
217 lines
7.1 KiB
Dart
217 lines
7.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart';
|
|
import 'package:buzz/shared/relay/relay_call_error.dart';
|
|
import 'package:buzz/shared/relay/relay_closed_policy.dart';
|
|
import 'package:buzz/shared/relay/signed_event_relay.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
void main() {
|
|
// A relay that authenticates the socket, then answers the event with
|
|
// [reason].
|
|
Future<HttpServer> relayRefusing(String reason) async {
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
addTearDown(() => server.close(force: true));
|
|
server.listen((request) async {
|
|
final ws = await WebSocketTransformer.upgrade(request);
|
|
ws.add(jsonEncode(['AUTH', 'challenge']));
|
|
ws.listen((raw) {
|
|
final frame = jsonDecode(raw as String) as List<dynamic>;
|
|
final id = (frame[1] as Map<String, dynamic>)['id'];
|
|
if (frame[0] == 'AUTH') ws.add(jsonEncode(['OK', id, true, '']));
|
|
if (frame[0] == 'EVENT') ws.add(jsonEncode(['OK', id, false, reason]));
|
|
});
|
|
});
|
|
return server;
|
|
}
|
|
|
|
for (final (reason, reported) in [
|
|
('invalid: bad signature', true),
|
|
('invalid: event timestamp too far from server time', false),
|
|
('blocked: not a member', false),
|
|
]) {
|
|
test(
|
|
'a refusal "$reason" ${reported ? 'is' : 'is not'} reported',
|
|
() async {
|
|
final server = await relayRefusing(reason);
|
|
final keys = nostr.Keys.generate();
|
|
final reporter = _RecordingDiagnostics()..setMember(keys.public);
|
|
|
|
// A refusal is definite: the relay saw the event and didn't store it.
|
|
await expectLater(
|
|
submitSignedEventOnce(
|
|
wsUrl: 'ws://127.0.0.1:${server.port}',
|
|
nsec: keys.nsec,
|
|
kind: 1,
|
|
content: '',
|
|
tags: const [],
|
|
diagnostics: reporter,
|
|
),
|
|
throwsA(
|
|
allOf(isA<Exception>(), isNot(isA<SignedEventUnacknowledged>())),
|
|
),
|
|
);
|
|
|
|
expect(reporter.errors, [
|
|
if (reported)
|
|
(RelayInvalidRequest, DiagnosticOperation.relayRejection),
|
|
]);
|
|
},
|
|
);
|
|
}
|
|
|
|
// Whether a failure might have reached the relay decides whether a caller
|
|
// can treat the event as never stored. Each relay stalls at one step, and
|
|
// the attempt ends at its deadline.
|
|
for (final (stall, maybeStored) in [
|
|
('auth', false),
|
|
('ok', true),
|
|
('close after event', true),
|
|
]) {
|
|
test('a relay that stalls at $stall ends at the deadline, '
|
|
'${maybeStored ? 'maybe stored' : 'never sent'}', () async {
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
addTearDown(() => server.close(force: true));
|
|
final events = <Object?>[];
|
|
server.listen((request) async {
|
|
final ws = await WebSocketTransformer.upgrade(request);
|
|
ws.add(jsonEncode(['AUTH', 'challenge']));
|
|
ws.listen((raw) {
|
|
final frame = jsonDecode(raw as String) as List<dynamic>;
|
|
final id = (frame[1] as Map<String, dynamic>)['id'];
|
|
if (frame[0] == 'AUTH' && stall != 'auth') {
|
|
ws.add(jsonEncode(['OK', id, true, '']));
|
|
}
|
|
if (frame[0] == 'EVENT') {
|
|
events.add(id);
|
|
if (stall == 'close after event') unawaited(ws.close());
|
|
}
|
|
});
|
|
});
|
|
final started = DateTime.now();
|
|
|
|
final failure = submitSignedEventOnce(
|
|
wsUrl: 'ws://127.0.0.1:${server.port}',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
kind: 1,
|
|
content: '',
|
|
tags: const [],
|
|
deadline: started.add(const Duration(milliseconds: 500)),
|
|
);
|
|
|
|
await expectLater(
|
|
failure,
|
|
throwsA(
|
|
maybeStored
|
|
? isA<SignedEventUnacknowledged>()
|
|
: allOf(
|
|
isA<TimeoutException>(),
|
|
isNot(isA<SignedEventUnacknowledged>()),
|
|
),
|
|
),
|
|
);
|
|
expect(
|
|
DateTime.now().difference(started),
|
|
lessThan(const Duration(seconds: 3)),
|
|
);
|
|
expect(events, hasLength(maybeStored ? 1 : 0));
|
|
});
|
|
}
|
|
|
|
// A removed community's tombstone, say, signs with a key other than the
|
|
// active member's, and its report would name the wrong member. With no
|
|
// member set, as on first run, reports carry the install identifier.
|
|
for (final (member, reported) in [
|
|
(nostr.Keys.generate().public, false),
|
|
(null, true),
|
|
]) {
|
|
test('a refusal while the member is ${member ?? 'unset'} '
|
|
'${reported ? 'is' : 'is not'} reported', () async {
|
|
final server = await relayRefusing('invalid: bad signature');
|
|
final reporter = _RecordingDiagnostics()..setMember(member);
|
|
|
|
await expectLater(
|
|
submitSignedEventOnce(
|
|
wsUrl: 'ws://127.0.0.1:${server.port}',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
kind: 1,
|
|
content: '',
|
|
tags: const [],
|
|
diagnostics: reporter,
|
|
),
|
|
throwsA(isA<Exception>()),
|
|
);
|
|
|
|
expect(reporter.errors, [
|
|
if (reported) (RelayInvalidRequest, DiagnosticOperation.relayRejection),
|
|
]);
|
|
});
|
|
}
|
|
test('a deadline that passes while authenticating reaches the caller, '
|
|
'and the event is never sent', () async {
|
|
final events = <Object?>[];
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
addTearDown(() => server.close(force: true));
|
|
server.listen((request) async {
|
|
final ws = await WebSocketTransformer.upgrade(request);
|
|
ws.add(jsonEncode(['AUTH', 'challenge']));
|
|
ws.listen((raw) {
|
|
final frame = jsonDecode(raw as String) as List<dynamic>;
|
|
final id = (frame[1] as Map<String, dynamic>)['id'];
|
|
if (frame[0] == 'EVENT') events.add(id);
|
|
if (frame[0] == 'AUTH') {
|
|
// Authentication answers only after the caller's deadline.
|
|
Timer(const Duration(milliseconds: 600), () {
|
|
ws.add(jsonEncode(['OK', id, true, '']));
|
|
});
|
|
}
|
|
});
|
|
});
|
|
final watch = Stopwatch()..start();
|
|
|
|
await expectLater(
|
|
submitSignedEventOnce(
|
|
wsUrl: 'ws://127.0.0.1:${server.port}',
|
|
nsec: nostr.Keys.generate().nsec,
|
|
kind: 1,
|
|
content: '',
|
|
tags: const [],
|
|
timeout: const Duration(milliseconds: 200),
|
|
),
|
|
throwsA(
|
|
isA<RelayTimeoutException>().having(
|
|
(error) => error.call,
|
|
'call',
|
|
RelayCall.publish,
|
|
),
|
|
),
|
|
);
|
|
expect(watch.elapsed, lessThan(const Duration(milliseconds: 550)));
|
|
// Outlive the late authentication: an uncaught error would fail the test.
|
|
await Future<void>.delayed(const Duration(milliseconds: 800));
|
|
|
|
expect(events, isEmpty);
|
|
});
|
|
}
|
|
|
|
class _RecordingDiagnostics extends NoopDiagnosticReporter {
|
|
final errors = <(Type, DiagnosticOperation)>[];
|
|
String? _member;
|
|
|
|
@override
|
|
String? get member => _member;
|
|
|
|
@override
|
|
void setMember(String? pubkey) => _member = pubkey;
|
|
|
|
@override
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
}) => errors.add((error.runtimeType, operation));
|
|
}
|