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>
220 lines
7.8 KiB
Dart
220 lines
7.8 KiB
Dart
import 'package:buzz/shared/drafts/compose_drafts_provider.dart';
|
|
import 'package:buzz/shared/relay/relay.dart';
|
|
import 'package:buzz/shared/theme/theme_provider.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class _FixedRelayConfigNotifier extends RelayConfigNotifier {
|
|
final RelayConfig _config;
|
|
_FixedRelayConfigNotifier(this._config);
|
|
|
|
@override
|
|
RelayConfig build() => _config;
|
|
}
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
Future<ProviderContainer> containerWithPrefs({
|
|
String relayUrl = 'https://relay-a.example',
|
|
String? pubkey = 'pk_a',
|
|
}) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
savedPrefsProvider.overrideWithValue(prefs),
|
|
relayConfigProvider.overrideWith(
|
|
() => _FixedRelayConfigNotifier(RelayConfig(baseUrl: relayUrl)),
|
|
),
|
|
myPubkeyProvider.overrideWithValue(pubkey),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
return container;
|
|
}
|
|
|
|
test(
|
|
'an invite-joined community keeps its drafts after origin canonicalization',
|
|
() async {
|
|
// Written by a build that stored the invite link's wss:// origin
|
|
// verbatim; RelayConfig now canonicalizes that to https://, so the key
|
|
// the app computes no longer matches the key on disk.
|
|
const legacyKey = 'compose_drafts_v1:wss://relay-a.example:pk_a';
|
|
const canonicalKey = 'compose_drafts_v1:https://relay-a.example:pk_a';
|
|
SharedPreferences.setMockInitialValues({
|
|
legacyKey:
|
|
'[{"key":"ch1","channel_id":"ch1","text":"unsent work",'
|
|
'"updated_at":1700000000}]',
|
|
});
|
|
|
|
final container = await containerWithPrefs(
|
|
relayUrl: 'wss://relay-a.example',
|
|
);
|
|
|
|
final drafts = container.read(composeDraftsProvider);
|
|
expect(drafts, hasLength(1), reason: 'draft survives the upgrade');
|
|
expect(drafts.single.text, 'unsent work');
|
|
|
|
await Future<void>.delayed(Duration.zero);
|
|
final prefs = await SharedPreferences.getInstance();
|
|
expect(prefs.getString(canonicalKey), isNotNull);
|
|
expect(prefs.getString(legacyKey), isNull);
|
|
},
|
|
);
|
|
|
|
test('composeDraftKey separates channel and thread composers', () {
|
|
expect(composeDraftKey('ch1'), 'ch1');
|
|
expect(composeDraftKey('ch1', threadHeadId: 't1'), 'ch1:t1');
|
|
});
|
|
|
|
test('save adds a draft and textFor returns it', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final container = await containerWithPrefs();
|
|
final notifier = container.read(composeDraftsProvider.notifier);
|
|
|
|
notifier.save(key: 'ch1', channelId: 'ch1', text: 'hello there');
|
|
|
|
final drafts = container.read(composeDraftsProvider);
|
|
expect(drafts, hasLength(1));
|
|
expect(drafts.single.text, 'hello there');
|
|
expect(notifier.textFor('ch1'), 'hello there');
|
|
});
|
|
|
|
test('empty text removes the draft', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final container = await containerWithPrefs();
|
|
final notifier = container.read(composeDraftsProvider.notifier);
|
|
|
|
notifier.save(key: 'ch1', channelId: 'ch1', text: 'hello');
|
|
notifier.save(key: 'ch1', channelId: 'ch1', text: ' ');
|
|
|
|
expect(container.read(composeDraftsProvider), isEmpty);
|
|
});
|
|
|
|
test('drafts persist across container restarts', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final first = await containerWithPrefs();
|
|
first
|
|
.read(composeDraftsProvider.notifier)
|
|
.save(key: 'ch1:t1', channelId: 'ch1', threadHeadId: 't1', text: 'wip');
|
|
|
|
final second = await containerWithPrefs();
|
|
final restored = second.read(composeDraftsProvider);
|
|
expect(restored, hasLength(1));
|
|
expect(restored.single.channelId, 'ch1');
|
|
expect(restored.single.threadHeadId, 't1');
|
|
expect(restored.single.text, 'wip');
|
|
});
|
|
|
|
test('remove deletes by key', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final container = await containerWithPrefs();
|
|
final notifier = container.read(composeDraftsProvider.notifier);
|
|
|
|
notifier.save(key: 'ch1', channelId: 'ch1', text: 'hello');
|
|
notifier.remove('ch1');
|
|
|
|
expect(container.read(composeDraftsProvider), isEmpty);
|
|
});
|
|
|
|
test('malformed persisted json is ignored', () async {
|
|
SharedPreferences.setMockInitialValues({
|
|
'compose_drafts_v1:https://relay-a.example:pk_a': '{not valid',
|
|
});
|
|
final container = await containerWithPrefs();
|
|
expect(container.read(composeDraftsProvider), isEmpty);
|
|
});
|
|
|
|
test('drafts are isolated per community', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final communityA = await containerWithPrefs(
|
|
relayUrl: 'https://relay-a.example',
|
|
);
|
|
communityA
|
|
.read(composeDraftsProvider.notifier)
|
|
.save(key: 'ch1', channelId: 'ch1', text: 'secret from A');
|
|
|
|
// Same channel id in another community must not see A's draft.
|
|
final communityB = await containerWithPrefs(
|
|
relayUrl: 'https://relay-b.example',
|
|
);
|
|
expect(communityB.read(composeDraftsProvider), isEmpty);
|
|
expect(
|
|
communityB.read(composeDraftsProvider.notifier).textFor('ch1'),
|
|
isNull,
|
|
);
|
|
|
|
// A's own store is untouched.
|
|
final communityAAgain = await containerWithPrefs(
|
|
relayUrl: 'https://relay-a.example',
|
|
);
|
|
expect(
|
|
communityAAgain.read(composeDraftsProvider.notifier).textFor('ch1'),
|
|
'secret from A',
|
|
);
|
|
});
|
|
|
|
test('drafts are isolated per account pubkey', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final accountA = await containerWithPrefs(pubkey: 'pk_a');
|
|
accountA
|
|
.read(composeDraftsProvider.notifier)
|
|
.save(key: 'ch1', channelId: 'ch1', text: 'account A draft');
|
|
|
|
// Same relay, different account: a colliding channel id must not
|
|
// restore the other account's draft.
|
|
final accountB = await containerWithPrefs(pubkey: 'pk_b');
|
|
expect(accountB.read(composeDraftsProvider), isEmpty);
|
|
expect(
|
|
accountB.read(composeDraftsProvider.notifier).textFor('ch1'),
|
|
isNull,
|
|
);
|
|
});
|
|
|
|
test('in-place identity change rebuilds onto the new store', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
savedPrefsProvider.overrideWithValue(prefs),
|
|
// Real derivation path: myPubkeyProvider derives from the config's
|
|
// nsec, so an in-place config change simulates an account/community
|
|
// switch without restarting the container.
|
|
relayConfigProvider.overrideWith(
|
|
() => _FixedRelayConfigNotifier(
|
|
const RelayConfig(baseUrl: 'https://relay-a.example'),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
// Seed a draft for the initial identity (nsec-less config → 'anon').
|
|
container
|
|
.read(composeDraftsProvider.notifier)
|
|
.save(key: 'ch1', channelId: 'ch1', text: 'first identity draft');
|
|
expect(container.read(composeDraftsProvider), hasLength(1));
|
|
|
|
// Switch community in place: the drafts provider must rebuild against
|
|
// the new identity's (empty) store, not keep the old state.
|
|
container
|
|
.read(relayConfigProvider.notifier)
|
|
.update(baseUrl: 'https://relay-b.example');
|
|
expect(container.read(composeDraftsProvider), isEmpty);
|
|
expect(
|
|
container.read(composeDraftsProvider.notifier).textFor('ch1'),
|
|
isNull,
|
|
);
|
|
|
|
// Switching back restores the original identity's draft.
|
|
container
|
|
.read(relayConfigProvider.notifier)
|
|
.update(baseUrl: 'https://relay-a.example');
|
|
expect(
|
|
container.read(composeDraftsProvider.notifier).textFor('ch1'),
|
|
'first identity draft',
|
|
);
|
|
});
|
|
}
|