fix(mobile): bind notification replies to the account they were typed for (#9845)

A notification reply goes out as whatever account its community signs
with when the reply is sent. No sign-in path replaces a community's
account in place today: signing in again as another account adds a new
community, and hosted re-entry refuses a different key. But nothing tied
a reply to the account its notification was for, so a future in-place
credential replacement would let a reply typed for the earlier account
go out as the new one, or land in its drafts.

Each reply is now tied to the account its notification was for:

- The notification service extension records that account in the reply
context, and iOS carries it through the reply journal, so a reply
recovered on a later launch keeps it too.
- The app sends a reply, or keeps it as a draft, only while the
community still signs as that account (the key it signs with). It checks
again after connecting.
- Otherwise the reply ends as lost: iOS shows "Reply not sent" with the
text, and no draft is kept. This is the same path a reply takes when its
community has been removed ([ADR
0004](docs/adr/0004-notification-reply.md), updated here).
- A reply to a notification posted by an earlier build has no recorded
account, so it is checked only by its community, as before.

### Regressions

- A community whose notification snapshot has no account key offers no
reply action. Every signed-in community has one, since the snapshot
falls back to the key derived from its secret.
- No extra work on the send path beyond a key comparison.

### Validation

-
[notification_reply_test.dart](https://github.com/block/buzz/blob/notification-reply-account/mobile/test/features/channels/notification_reply_test.dart):
a live reply for another account and a recovered one for another
community end as lost with their text kept nowhere; a reply whose
community's signing key is replaced while the relay connects (same
community and stored key) is neither sent nor drafted; a reply from an
earlier build without an account is sent.
-
[BuzzNotificationReplyTests.swift](https://github.com/block/buzz/blob/notification-reply-account/mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzNotificationReplyTests.swift):
the resolver records the notification's account, lowercased. An empty
account is rejected, and the account survives the journal round trip.
- No simulator run: the reply action needs a real push resolved by the
notification service extension, which the simulator can't receive. The
change is covered at both layers by the tests above, and the iOS app
builds.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
Tom Brow
2026-11-20 20:23:59 -08:00
committed by GitHub
parent 52cbc61131
commit d1236080eb
7 changed files with 148 additions and 24 deletions

View File

@@ -30,6 +30,7 @@ final _community = Community(
id: 'community',
name: 'Example',
relayUrl: 'https://relay.example.org',
pubkey: 'me',
addedAt: DateTime.utc(2026),
);
@@ -37,6 +38,7 @@ final _elsewhere = Community(
id: 'elsewhere',
name: 'Elsewhere',
relayUrl: 'https://other.example.org',
pubkey: 'me elsewhere',
addedAt: DateTime.utc(2026),
);
@@ -67,6 +69,7 @@ Map<String, String> _reply(
String? replyRootId,
int createdAt = 1000,
bool inSolo = false,
String? recipientPubkey,
}) => {
'id': id,
'text': 'reply $id',
@@ -75,6 +78,8 @@ Map<String, String> _reply(
'channelId': channelId,
'threadRootId': ?threadRootId,
'senderPubkey': _sender,
'recipientPubkey':
recipientPubkey ?? (communityId == 'elsewhere' ? 'me elsewhere' : 'me'),
'replyRootId': ?replyRootId,
'messageCreatedAt': '$createdAt',
if (inSolo) 'inSolo': 'true',
@@ -143,6 +148,23 @@ void main() {
// Once iOS has ended a reply as not sent, it never goes out.
_reply('18'),
// A community signed in again as another account on the same relay
// keeps its ID, but the reply was typed for the earlier account: it
// is never sent or kept as the new one's draft, so iOS shows its text
// in a notification, as for a gone community.
_reply('19', recipientPubkey: 'earlier account'),
{
..._reply(
'20',
communityId: 'elsewhere',
recipientPubkey: 'earlier account',
),
'recovered': 'true',
},
// A notification from an earlier build doesn't say whose it was, so
// only its community is checked.
{..._reply('21')}..remove('recipientPubkey'),
];
// What iOS hears, in order, alongside each message sent.
final log = <String>[];
@@ -187,11 +209,14 @@ void main() {
final sender = _RecordingSender(onSend: (text) => log.add('sent $text'));
final session = _Session();
var active = _community;
var communities = [_community, _elsewhere];
final container = ProviderContainer(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
authProvider.overrideWith(_Auth.new),
communityListProvider.overrideWith(_Communities.new),
communityListProvider.overrideWith(
() => _Communities(() => communities),
),
communityStorageProvider.overrideWithValue(_Storage()),
activeCommunityProvider.overrideWith((ref) async => active),
channelsProvider.overrideWith(
@@ -235,6 +260,9 @@ void main() {
'15': 'unknown',
'16': 'no draft',
'18': 'draft in $_message',
'19': 'no draft',
'20': 'no draft',
'21': 'sent',
});
// iOS knows a reply may go out before anything is published.
expect(log, [
@@ -243,6 +271,8 @@ void main() {
'sent reply $id',
],
'may publish 18',
'may publish 21',
'sent reply 21',
]);
expect(sender.calls, [
('general', 'reply 1', _message, _message, _sender),
@@ -250,6 +280,7 @@ void main() {
('solo', 'reply 3', null, null, _sender),
('left', 'reply 10', _message, _message, ''),
('lost', 'reply 11', _message, _message, _sender),
('general', 'reply 21', _message, _message, _sender),
]);
// Read only up to each notified message, so newer ones stay unread.
expect(container.read(readStateProvider).contexts, {
@@ -267,10 +298,13 @@ void main() {
elsewhere,
contains('"key":"own","channel_id":"own","text":"reply 13"'),
);
// The gone community's reply is kept nowhere.
expect([
for (final key in prefs.getKeys()) '${prefs.get(key)}',
], everyElement(isNot(contains('reply 16'))));
// The gone community's reply is kept nowhere, nor is another
// account's.
for (final id in ['16', '19', '20']) {
expect([
for (final key in prefs.getKeys()) '${prefs.get(key)}',
], everyElement(isNot(contains('reply $id'))));
}
expect(
drafts.textFor(composeDraftKey('broken', threadHeadId: _message)),
'reply 5',
@@ -331,6 +365,36 @@ void main() {
contains('"text":"reply 8\\n\\nreply 9"'),
);
expect(prefs.getString(_elsewhereDrafts), isNot(contains('reply 9')));
// Credentials replaced in place while the relay connects: the same
// community and stored key, but it now signs as another account. The
// reply is never sent or kept as that account's draft.
final keys = nostr.Keys.generate();
final signedIn = _community.copyWith(
pubkey: keys.public,
nsec: keys.nsec,
);
final replaced = signedIn.copyWith(nsec: nostr.Keys.generate().nsec);
active = signedIn;
communities = [signedIn, _elsewhere];
container
..invalidate(activeCommunityProvider)
..invalidate(communityListProvider);
await container.read(activeCommunityProvider.future);
session.onConnect = () async {
active = replaced;
communities = [replaced, _elsewhere];
container
..invalidate(activeCommunityProvider)
..invalidate(communityListProvider);
await container.read(activeCommunityProvider.future);
};
await signalReply(_reply('22', recipientPubkey: keys.public));
expect(outcomes['22'], 'no draft');
expect(sender.calls.last.$2, 'reply 7');
expect([
for (final key in prefs.getKeys()) '${prefs.get(key)}',
], everyElement(isNot(contains('reply 22'))));
},
);
@@ -485,8 +549,12 @@ class _Storage extends CommunityStorage {
}
class _Communities extends CommunityListNotifier {
_Communities([this.communities]);
final List<Community> Function()? communities;
@override
Future<List<Community>> build() async => [_community, _elsewhere];
Future<List<Community>> build() async =>
communities?.call() ?? [_community, _elsewhere];
}
class _Session extends RelaySessionNotifier {