mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
On iOS, a notification that shows a channel or DM message now offers
**Reply**. The user types a reply in the notification and sends it
without opening the app.
- The reply goes in a subthread under the notified message (its thread
root is the message's own root, or the message itself when top-level). A
Solo chat takes no thread replies, so there it goes at the chat's top
level.
- It always mentions the notified message's sender, so the person or
agent being answered is notified. Typed `@name` text stays plain, since
a notification has no mention autocomplete.
- It is sent by the app's own send path. iOS starts the Flutter engine
when it wakes the app in the background, and the reply reopens the relay
connection (closed after the background grace period), waiting a bounded
time for it before sending.
- A sent reply marks the conversation read **up to the notified
message** (newer messages stay unread) and clears that notification and
earlier ones from the same conversation.
- An unsent reply (offline, relay error, timeout, a channel the user
isn't in) keeps the typed text as a draft, after any draft already
there, and shows a "Reply not sent" notification that opens the
conversation.
For now only notifications from the community selected in the app offer
Reply: replying must not switch communities, and the app can only reach
the selected one. Forum posts and fallback notifications that show no
message offer none. The design and the options considered are in
`docs/adr/0004-notification-reply.md`.
### Before / after
Long-pressing a message notification (synthetic names and text). Before
has no reply field (captured by sending the notification without its new
reply category, which is how main presents it); after shows one with
Send.
| Before | After |
| --- | --- |
|

|

|
### Demo
With the app backgrounded past the relay grace period, replying from the
notification lands the reply in a subthread under the message:
https://github.com/user-attachments/assets/37df7e28-aa46-4521-938e-8af5faa77b83
### Regressions
Nothing existing changes unless the user taps Reply. Notifications gain
a category and a small reply context in `userInfo`; tapping a
notification still opens the conversation as before. The relay reconnect
only happens for a pending reply and then falls back to the normal
background disconnect, so idle background behavior and network use are
unchanged. Reply failures end in a draft and a "Reply not sent"
notification rather than touching other state.
### Validation
- End to end on an iOS 27 simulator against a real community: with the
app backgrounded past the relay grace period, replied from the
notification; the reply appeared in a subthread under the message,
mentioning its sender. The demo above was recorded before the review
fixes (session hold, community fence, origin-community drafts, composer
draft sync); the same run was repeated on the current head, 9b9c8b0, and
the reply again appeared in the subthread with no "Reply not sent"
notification.
- Human testing is waived for this repo's mobile client; the simulator
run above replaces it. Not yet exercised on a physical device or on a
cold launch (app not running).
- Added tests: a Dart journey covering top-level, in-thread and Solo
replies, another community (draft kept in that community), a failed
send, a channel the user isn't in (draft appended), a reply arriving
while running, an unreachable relay, and a community switch while
connecting (not sent, draft kept in the reply's community); a
relay-session test that a background-work hold keeps the grace
disconnect off, including one already waiting on cleanup, until released
or its limit; a composer step showing a draft appended elsewhere appears
in the mounted composer and survives its next edit; a relay-session test
for reconnecting a paused session and timing out (fails without the
fix); Swift tests for reply context encoding, NIP-10 root parsing
aligned with the app, and request validation.
- `relay_session.dart` was at its 1200-line cap, so the background
connect lives in a new part file and three existing test hooks moved
unchanged into another.
- Full `mobile-test`, `mobile-check` and BuzzPushKit `swift test` pass.
---------
Signed-off-by: Tom Brow <tomb@squareup.com>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
237 lines
6.9 KiB
Dart
237 lines
6.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:buzz/features/channels/channel_management_provider.dart';
|
|
import 'package:buzz/features/channels/channels_provider.dart';
|
|
import 'package:buzz/shared/relay/relay.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
test(
|
|
'opening a DM finishes while the full channel list is stalled',
|
|
() async {
|
|
final keys = nostr.Keys.generate();
|
|
final session = _DmSession(keys.public);
|
|
final container = ProviderContainer(
|
|
retry: (_, _) => null,
|
|
overrides: [
|
|
relaySessionProvider.overrideWith(() => session),
|
|
myPubkeyProvider.overrideWithValue(keys.public),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
container.listen(channelsProvider, (_, _) {});
|
|
await session.listStarted.future;
|
|
var valid = true;
|
|
final actions = container.read(
|
|
Provider(
|
|
(ref) => ChannelActions(
|
|
ref: ref,
|
|
session: session,
|
|
signedEventRelay: SignedEventRelay(
|
|
session: session,
|
|
nsec: keys.nsec,
|
|
),
|
|
currentPubkey: keys.public,
|
|
isCommunityValid: () => valid,
|
|
),
|
|
),
|
|
);
|
|
|
|
final opened = await actions
|
|
.openDm(pubkeys: [_peer], waitForChannelList: false)
|
|
.timeout(const Duration(seconds: 2));
|
|
expect(opened.id, _dmId);
|
|
expect(opened.isDm, isTrue);
|
|
expect(opened.isMember, isTrue);
|
|
expect(opened.participantPubkeys, [keys.public, _peer]);
|
|
expect(session.published.single.kind, 41010);
|
|
expect(session.published.single.tags, [
|
|
['p', _peer],
|
|
]);
|
|
// Composers find it before the list has it, so sends there can check
|
|
// mentions and address the DM.
|
|
expect(
|
|
container.read(channelsProvider.notifier).channelById(_dmId)?.id,
|
|
_dmId,
|
|
);
|
|
|
|
// The sidebar still reconciles, in the background, once the stalled
|
|
// initial load settles.
|
|
session.stalledList.complete(const []);
|
|
await pumpEventQueue();
|
|
expect(session.listLoads, 2);
|
|
session.stalledList = Completer();
|
|
|
|
// A DM the relay confirms but the caller cannot read fails visibly.
|
|
session.member = false;
|
|
await expectLater(
|
|
actions.openDm(pubkeys: [_peer], waitForChannelList: false),
|
|
throwsStateError,
|
|
);
|
|
|
|
// A reopen whose unhide snapshot the relay failed to publish fails
|
|
// visibly instead of opening a DM the sidebar still hides.
|
|
session.member = true;
|
|
session.hidden = {_dmId};
|
|
await expectLater(
|
|
actions.openDm(pubkeys: [_peer], waitForChannelList: false),
|
|
throwsStateError,
|
|
);
|
|
expect(session.visibilityReads, greaterThan(0));
|
|
// A newly created DM cannot be in an older snapshot, so it skips that
|
|
// read and cannot fail on it after the relay created the DM.
|
|
session.created = true;
|
|
final reads = session.visibilityReads;
|
|
expect(
|
|
(await actions.openDm(pubkeys: [_peer], waitForChannelList: false)).id,
|
|
_dmId,
|
|
);
|
|
expect(session.visibilityReads, reads);
|
|
session.created = false;
|
|
session.hidden = {};
|
|
|
|
// Hidden-DM resurfacing keeps waiting for the list that clears its
|
|
// hidden state, so it cannot outrun its own retry coalescing.
|
|
session.member = true;
|
|
var resurfaced = false;
|
|
unawaited(
|
|
actions.openDm(pubkeys: [_peer]).then((_) => resurfaced = true),
|
|
);
|
|
await pumpEventQueue();
|
|
expect(resurfaced, isFalse);
|
|
|
|
// A community switch during the targeted read cancels the open.
|
|
session.onRead = () => valid = false;
|
|
await expectLater(
|
|
actions.openDm(pubkeys: [_peer], waitForChannelList: false),
|
|
throwsStateError,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
const _dmId = 'dm-id';
|
|
const _peer =
|
|
'1111111111111111111111111111111111111111111111111111111111111111';
|
|
|
|
class _DmSession extends RelaySessionNotifier {
|
|
_DmSession(this.self);
|
|
final String self;
|
|
bool member = true;
|
|
Set<String> hidden = {};
|
|
bool created = false;
|
|
int visibilityReads = 0;
|
|
void Function()? onRead;
|
|
final published = <NostrEvent>[];
|
|
Completer<List<NostrEvent>> stalledList = Completer();
|
|
final listStarted = Completer<void>();
|
|
int listLoads = 0;
|
|
|
|
@override
|
|
SessionState build() => const SessionState(status: SessionStatus.connected);
|
|
|
|
@override
|
|
Future<List<NostrEvent>> queryRelay(
|
|
List<NostrFilter> filters, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async {
|
|
// Every full channel-list load starts with this membership query.
|
|
expect(filters.single.tags, {
|
|
'#p': [self],
|
|
});
|
|
listLoads++;
|
|
if (!listStarted.isCompleted) listStarted.complete();
|
|
return stalledList.future;
|
|
}
|
|
|
|
@override
|
|
Future<NostrEvent> publish(
|
|
NostrEvent event, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
Future<bool> Function()? mayPublish,
|
|
}) async {
|
|
published.add(event);
|
|
return NostrEvent(
|
|
id: event.id,
|
|
pubkey: event.pubkey,
|
|
createdAt: event.createdAt,
|
|
kind: event.kind,
|
|
tags: event.tags,
|
|
content:
|
|
'response:${jsonEncode({'channel_id': _dmId, 'created': created})}',
|
|
sig: event.sig,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<List<NostrEvent>> fetchHistory(
|
|
NostrFilter filter, {
|
|
Duration timeout = const Duration(seconds: 8),
|
|
}) async {
|
|
if (filter.kinds.single == EventKind.dmVisibility) {
|
|
visibilityReads++;
|
|
expect(filter.tags, {
|
|
'#p': [self],
|
|
});
|
|
return [
|
|
NostrEvent(
|
|
id: 'visibility',
|
|
pubkey: 'relay',
|
|
createdAt: 1,
|
|
kind: EventKind.dmVisibility,
|
|
tags: [
|
|
['p', self],
|
|
for (final id in hidden) ['h', id],
|
|
],
|
|
content: '',
|
|
sig: '',
|
|
),
|
|
];
|
|
}
|
|
expect(filter.tags, {
|
|
'#d': [_dmId],
|
|
});
|
|
onRead?.call();
|
|
// Serve like a relay: newest first, one limit across every kind. A stale
|
|
// metadata revision from before the d_tag backfill still lingers.
|
|
final events = [
|
|
_event(39000, 3, [
|
|
['name', 'DM'],
|
|
['t', 'dm'],
|
|
['private'],
|
|
['hidden'],
|
|
['p', self],
|
|
['p', _peer],
|
|
]),
|
|
_event(39000, 2, [
|
|
['name', 'DM'],
|
|
['t', 'dm'],
|
|
['private'],
|
|
]),
|
|
_event(39002, 1, [
|
|
if (member) ['p', self, '', 'member'],
|
|
['p', _peer, '', 'member'],
|
|
]),
|
|
].where((e) => filter.kinds.contains(e.kind)).toList();
|
|
return events.take(filter.limit).toList();
|
|
}
|
|
}
|
|
|
|
NostrEvent _event(int kind, int createdAt, List<List<String>> tags) =>
|
|
NostrEvent(
|
|
id: '$kind-$createdAt',
|
|
pubkey: 'relay',
|
|
createdAt: createdAt,
|
|
kind: kind,
|
|
tags: [
|
|
['d', _dmId],
|
|
...tags,
|
|
],
|
|
content: '',
|
|
sig: '',
|
|
);
|