mirror of
https://github.com/block/buzz.git
synced 2026-11-25 10:42:26 +00:00
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>
641 lines
23 KiB
Dart
641 lines
23 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:buzz/shared/drafts/compose_drafts_provider.dart';
|
|
import 'package:buzz/features/channels/channel.dart';
|
|
import 'package:buzz/features/channels/channel_management_provider.dart';
|
|
import 'package:buzz/features/channels/channels_provider.dart';
|
|
import 'package:buzz/features/channels/notification_reply.dart';
|
|
import 'package:buzz/features/channels/send_message_provider.dart';
|
|
import 'package:buzz/shared/auth/auth.dart';
|
|
import 'package:buzz/shared/push/push_bridge.dart';
|
|
import 'package:buzz/shared/read_state/read_state_format.dart';
|
|
import 'package:buzz/shared/read_state/read_state_provider.dart';
|
|
import 'package:buzz/shared/relay/relay.dart';
|
|
import 'package:clock/clock.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:buzz/shared/theme/theme_provider.dart';
|
|
import 'package:fake_async/fake_async.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
const _push = MethodChannel('buzz/push');
|
|
final _root = 'a' * 64;
|
|
final _message = 'b' * 64;
|
|
final _sender = 'c' * 64;
|
|
|
|
final _community = Community(
|
|
id: 'community',
|
|
name: 'Example',
|
|
relayUrl: 'https://relay.example.org',
|
|
pubkey: 'me',
|
|
addedAt: DateTime.utc(2026),
|
|
);
|
|
|
|
final _elsewhere = Community(
|
|
id: 'elsewhere',
|
|
name: 'Elsewhere',
|
|
relayUrl: 'https://other.example.org',
|
|
pubkey: 'me elsewhere',
|
|
addedAt: DateTime.utc(2026),
|
|
);
|
|
|
|
const _selectedDrafts = 'compose_drafts_v1:https://relay.example.org:anon';
|
|
const _elsewhereDrafts = 'compose_drafts_v1:https://other.example.org:anon';
|
|
|
|
Channel _channel(String id, {bool solo = false, bool member = true}) => Channel(
|
|
id: id,
|
|
name: id,
|
|
description: '',
|
|
channelType: 'stream',
|
|
visibility: solo ? 'private' : 'open',
|
|
createdBy: 'owner',
|
|
createdAt: DateTime.utc(2026),
|
|
memberCount: 2,
|
|
isMember: member,
|
|
labels: solo ? const ['buzz.solo'] : const [],
|
|
);
|
|
|
|
ChannelMember _member(String pubkey) =>
|
|
ChannelMember(pubkey: pubkey, role: 'member', joinedAt: DateTime.utc(2026));
|
|
|
|
Map<String, String> _reply(
|
|
String id, {
|
|
String communityId = 'community',
|
|
String channelId = 'general',
|
|
String? threadRootId,
|
|
String? replyRootId,
|
|
int createdAt = 1000,
|
|
bool inSolo = false,
|
|
String? recipientPubkey,
|
|
}) => {
|
|
'id': id,
|
|
'text': 'reply $id',
|
|
'eventId': _message,
|
|
'communityId': communityId,
|
|
'channelId': channelId,
|
|
'threadRootId': ?threadRootId,
|
|
'senderPubkey': _sender,
|
|
'recipientPubkey':
|
|
recipientPubkey ?? (communityId == 'elsewhere' ? 'me elsewhere' : 'me'),
|
|
'replyRootId': ?replyRootId,
|
|
'messageCreatedAt': '$createdAt',
|
|
if (inSolo) 'inSolo': 'true',
|
|
'deadline':
|
|
'${clock.now().add(const Duration(seconds: 25)).millisecondsSinceEpoch}',
|
|
};
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
test(
|
|
'replies typed into notifications are sent, read and reported',
|
|
() async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final messenger =
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
|
final waiting = <Map<String, String>>[
|
|
// A top-level channel message: a new subthread under it.
|
|
_reply('1'),
|
|
// A message inside a thread: the subthread keeps the thread's root.
|
|
_reply('2', threadRootId: _root, replyRootId: _root, createdAt: 2000),
|
|
// A Solo chat takes no thread replies.
|
|
_reply('3', channelId: 'solo', createdAt: 3000),
|
|
// Another community's notification can't be answered without
|
|
// switching; its text is kept as a draft in that community, in the
|
|
// subthread under the notified message even though that community's
|
|
// channels aren't loaded.
|
|
_reply('4', communityId: 'elsewhere'),
|
|
_reply(
|
|
'12',
|
|
communityId: 'elsewhere',
|
|
channelId: 'nested',
|
|
threadRootId: _root,
|
|
replyRootId: _root,
|
|
),
|
|
// A notification that knew it was a Solo chat keeps it in the chat.
|
|
_reply('13', communityId: 'elsewhere', channelId: 'own', inSolo: true),
|
|
// A send that fails before publishing keeps the text in the
|
|
// subthread under the notified message, where the reply would go.
|
|
_reply(
|
|
'5',
|
|
channelId: 'broken',
|
|
threadRootId: _root,
|
|
replyRootId: _root,
|
|
),
|
|
// A channel the user isn't in isn't replied to, and its text goes
|
|
// after the draft already there.
|
|
_reply('6', channelId: 'outside'),
|
|
// A sender who has left isn't mentioned, but still gets the reply.
|
|
_reply('10', channelId: 'left'),
|
|
// A reply published without an acknowledgement may have gone out, so
|
|
// no draft is kept.
|
|
_reply('11', channelId: 'lost'),
|
|
// A reply an earlier launch never finished is never sent again: it's
|
|
// kept as a draft, or, if it may have gone out then, left alone.
|
|
{..._reply('14'), 'recovered': 'true'},
|
|
{..._reply('15'), 'recovered': 'true', 'publishing': 'true'},
|
|
// A community that's gone can't keep its draft, so iOS is told to
|
|
// show its text in a notification instead; it's settled, so it isn't
|
|
// retried on every launch.
|
|
{..._reply('16', communityId: 'gone'), 'recovered': 'true'},
|
|
|
|
// One whose community this build hides stays with iOS for a build
|
|
// that shows it.
|
|
{..._reply('17', communityId: 'hidden'), 'recovered': 'true'},
|
|
|
|
// 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>[];
|
|
installBuzzPushMethodHandler();
|
|
final outcomes = <String, Object>{};
|
|
final selected = <Object?>[];
|
|
messenger.setMockMethodCallHandler(_push, (call) async {
|
|
switch (call.method) {
|
|
case 'takePendingNotificationReplies':
|
|
final taken = [...waiting];
|
|
waiting.clear();
|
|
return taken;
|
|
case 'completeNotificationReply':
|
|
final args = call.arguments as Map;
|
|
outcomes[args['id'] as String] = args['unknown'] == true
|
|
? 'unknown'
|
|
: args['sent'] == true
|
|
? 'sent'
|
|
: args['noDraft'] == true
|
|
? 'no draft'
|
|
: 'draft in ${args['draftThreadRootId']}';
|
|
case 'markNotificationReplyPublishing':
|
|
log.add('may publish ${call.arguments}');
|
|
return call.arguments != '18';
|
|
case 'setSelectedCommunity':
|
|
selected.add(call.arguments);
|
|
}
|
|
return null;
|
|
});
|
|
addTearDown(() {
|
|
messenger.setMockMethodCallHandler(_push, null);
|
|
debugDefaultTargetPlatformOverride = null;
|
|
});
|
|
|
|
SharedPreferences.setMockInitialValues({
|
|
_selectedDrafts:
|
|
'[{"key":"outside:$_message","channel_id":"outside",'
|
|
'"thread_head_id":"$_message","text":"earlier draft",'
|
|
'"updated_at":1}]',
|
|
});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
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(() => communities),
|
|
),
|
|
communityStorageProvider.overrideWithValue(_Storage()),
|
|
activeCommunityProvider.overrideWith((ref) async => active),
|
|
channelsProvider.overrideWith(
|
|
() => _Channels([
|
|
_channel('general'),
|
|
_channel('solo', solo: true),
|
|
_channel('broken'),
|
|
_channel('outside', member: false),
|
|
_channel('left'),
|
|
_channel('lost'),
|
|
]),
|
|
),
|
|
channelMembersProvider.overrideWith((ref, channelId) async {
|
|
if (channelId == 'broken') throw StateError('members unavailable');
|
|
return [if (channelId != 'left') _member(_sender), _member('me')];
|
|
}),
|
|
sendMessageProvider.overrideWithValue(sender),
|
|
readStateProvider.overrideWith(_ReadState.new),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
container.listen(notificationReplySenderProvider, (_, _) {});
|
|
await pumpEventQueue();
|
|
final drafts = container.read(composeDraftsProvider.notifier);
|
|
|
|
expect(selected, ['community']);
|
|
expect(outcomes, {
|
|
'1': 'sent',
|
|
'2': 'sent',
|
|
'3': 'sent',
|
|
'4': 'draft in $_message',
|
|
'5': 'draft in $_message',
|
|
'6': 'draft in $_message',
|
|
'10': 'sent',
|
|
'11': 'unknown',
|
|
'12': 'draft in $_message',
|
|
'13': 'draft in null',
|
|
'14': 'draft in $_message',
|
|
'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, [
|
|
for (final id in ['1', '2', '3', '10', '11']) ...[
|
|
'may publish $id',
|
|
'sent reply $id',
|
|
],
|
|
'may publish 18',
|
|
'may publish 21',
|
|
'sent reply 21',
|
|
]);
|
|
expect(sender.calls, [
|
|
('general', 'reply 1', _message, _message, _sender),
|
|
('general', 'reply 2', _message, _root, _sender),
|
|
('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, {
|
|
'general': 1000,
|
|
threadContextKey(_root): 2000,
|
|
'solo': 3000,
|
|
'left': 1000,
|
|
});
|
|
expect(drafts.textFor(composeDraftKey('general')), isNull);
|
|
final elsewhere = prefs.getString(_elsewhereDrafts);
|
|
expect(elsewhere, contains('"key":"general:$_message"'));
|
|
expect(elsewhere, contains('"text":"reply 4"'));
|
|
expect(elsewhere, contains('"key":"nested:$_message"'));
|
|
expect(
|
|
elsewhere,
|
|
contains('"key":"own","channel_id":"own","text":"reply 13"'),
|
|
);
|
|
// 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',
|
|
);
|
|
expect(
|
|
drafts.textFor(composeDraftKey('outside', threadHeadId: _message)),
|
|
'earlier draft\n\nreply 6',
|
|
);
|
|
expect(
|
|
drafts.textFor(composeDraftKey('lost', threadHeadId: _message)),
|
|
isNull,
|
|
);
|
|
expect(
|
|
drafts.textFor(composeDraftKey('general', threadHeadId: _message)),
|
|
'reply 14\n\nreply 18',
|
|
);
|
|
|
|
Future<void> signalReply(Map<String, String> reply) async {
|
|
waiting.add(reply);
|
|
await messenger.handlePlatformMessage(
|
|
_push.name,
|
|
_push.codec.encodeMethodCall(
|
|
const MethodCall('notificationReplyAvailable'),
|
|
),
|
|
(_) {},
|
|
);
|
|
await pumpEventQueue();
|
|
}
|
|
|
|
// A reply arriving while the app runs is sent when iOS signals it.
|
|
await signalReply(_reply('7', channelId: 'solo'));
|
|
expect(outcomes['7'], 'sent');
|
|
|
|
// When the relay can't be reached in time, nothing is sent and the
|
|
// text is kept.
|
|
session.reachable = false;
|
|
await signalReply(_reply('8', channelId: 'solo'));
|
|
expect(outcomes['8'], 'draft in null');
|
|
expect(sender.calls.last.$2, 'reply 7');
|
|
expect(drafts.textFor(composeDraftKey('solo')), 'reply 8');
|
|
|
|
// A community switch while the relay connects stops the reply before
|
|
// it reaches the other community's relay, and keeps the text in the
|
|
// reply's own community.
|
|
session.reachable = true;
|
|
session.onConnect = () async {
|
|
active = _elsewhere;
|
|
container.invalidate(activeCommunityProvider);
|
|
await container.read(activeCommunityProvider.future);
|
|
};
|
|
// The other community's channels say nothing about this one, so the
|
|
// notification's Solo flag places the draft.
|
|
await signalReply(_reply('9', channelId: 'solo', inSolo: true));
|
|
expect(outcomes['9'], 'draft in null');
|
|
expect(sender.calls.last.$2, 'reply 7');
|
|
expect(
|
|
prefs.getString(_selectedDrafts),
|
|
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'))));
|
|
},
|
|
);
|
|
|
|
test('a reply publishes only while its outcome can still be reported', () {
|
|
fakeAsync((async) {
|
|
SharedPreferences.setMockInitialValues({});
|
|
late SharedPreferences prefs;
|
|
SharedPreferences.getInstance().then((value) => prefs = value);
|
|
async.flushMicrotasks();
|
|
final gate = RelayRateLimitGate(now: async.getClock(DateTime(2026)).now);
|
|
final session = _Session(rateLimitGate: gate);
|
|
final relay = _Relay(session);
|
|
var members = Completer<List<ChannelMember>>();
|
|
final send = SendMessage(
|
|
signedEventRelay: SignedEventRelay(
|
|
session: session,
|
|
nsec: nostr.Keys.generate().nsec,
|
|
),
|
|
fetchMembers: (_) => members.future,
|
|
readUserCache: () => const {},
|
|
addLocalMessage: (_, _) {},
|
|
completeLocalMessage: (_, _) {},
|
|
removeLocalMessage: (_, _) {},
|
|
);
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
savedPrefsProvider.overrideWithValue(prefs),
|
|
authProvider.overrideWith(_Auth.new),
|
|
communityListProvider.overrideWith(_Communities.new),
|
|
activeCommunityProvider.overrideWith((ref) async => _community),
|
|
channelsProvider.overrideWith(() => _Channels([_channel('general')])),
|
|
channelMembersProvider.overrideWith(
|
|
(ref, channelId) async => [_member(_sender)],
|
|
),
|
|
sendMessageProvider.overrideWithValue(send),
|
|
readStateProvider.overrideWith(_ReadState.new),
|
|
relaySessionProvider.overrideWith(() => session),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
final ref = container.read(_refProvider);
|
|
container.read(relaySessionProvider);
|
|
session.debugAttachSocketForTest(relay);
|
|
final drafts = container.read(composeDraftsProvider.notifier);
|
|
NotificationReplyResult? result;
|
|
void reply(String id) {
|
|
result = null;
|
|
sendNotificationReply(
|
|
ref,
|
|
BuzzNotificationReply.fromArguments(_reply(id))!,
|
|
).then((value) => result = value);
|
|
}
|
|
|
|
// The message send's own member lookup finishes too late to hear the
|
|
// relay's OK in time, so the reply is never published and its text is
|
|
// kept where it would have gone.
|
|
reply('1');
|
|
async.elapse(const Duration(seconds: 25));
|
|
expect(result?.outcome, NotificationReplyOutcome.notSent);
|
|
expect(
|
|
drafts.textFor(composeDraftKey('general', threadHeadId: _message)),
|
|
'reply 1',
|
|
);
|
|
members.complete([_member(_sender)]);
|
|
async.flushMicrotasks();
|
|
expect(relay.events, isEmpty);
|
|
String? draft() =>
|
|
drafts.textFor(composeDraftKey('general', threadHeadId: _message));
|
|
|
|
// Resumed after a suspension, the lookup finishes past the cutoff
|
|
// before the cutoff's timer runs, and the clock still stops the reply.
|
|
members = Completer();
|
|
reply('5');
|
|
async.flushMicrotasks();
|
|
async.elapseBlocking(const Duration(seconds: 20));
|
|
members.complete([_member(_sender)]);
|
|
async.flushMicrotasks();
|
|
expect(relay.events, isEmpty);
|
|
expect(result?.outcome, NotificationReplyOutcome.notSent);
|
|
expect(draft(), 'reply 1\n\nreply 5');
|
|
async.elapse(const Duration(seconds: 25));
|
|
|
|
// Held by a rate limit past its deadline, a reply is reported not sent,
|
|
// and never goes out once the limit lifts.
|
|
members = Completer()..complete([_member(_sender)]);
|
|
gate.activate(60);
|
|
reply('2');
|
|
async.elapse(const Duration(seconds: 25));
|
|
expect(result?.outcome, NotificationReplyOutcome.notSent);
|
|
expect(draft(), 'reply 1\n\nreply 5\n\nreply 2');
|
|
async.elapse(const Duration(seconds: 60));
|
|
expect(relay.events, isEmpty);
|
|
|
|
// A reply the relay refuses wasn't sent, so its text is kept.
|
|
relay.answer = (id) => ['OK', id, false, 'blocked: not allowed'];
|
|
reply('3');
|
|
async.elapse(const Duration(seconds: 25));
|
|
expect(relay.events.single['content'], 'reply 3');
|
|
expect(result?.outcome, NotificationReplyOutcome.notSent);
|
|
expect(draft(), 'reply 1\n\nreply 5\n\nreply 2\n\nreply 3');
|
|
|
|
// A published reply whose OK never comes may have gone out, so it
|
|
// leaves no draft to resend.
|
|
relay.answer = (_) => null;
|
|
reply('4');
|
|
async.elapse(const Duration(seconds: 25));
|
|
expect(relay.events.last['content'], 'reply 4');
|
|
expect(result?.outcome, NotificationReplyOutcome.unknown);
|
|
expect(draft(), 'reply 1\n\nreply 5\n\nreply 2\n\nreply 3');
|
|
});
|
|
});
|
|
}
|
|
|
|
final _refProvider = Provider<Ref>((ref) => ref);
|
|
|
|
class _Auth extends AuthNotifier {
|
|
@override
|
|
Future<AuthState> build() async =>
|
|
AuthState(status: AuthStatus.authenticated, community: _community);
|
|
}
|
|
|
|
class _Channels extends ChannelsNotifier {
|
|
_Channels(this._channels);
|
|
final List<Channel> _channels;
|
|
@override
|
|
Future<List<Channel>> build() async => _channels;
|
|
}
|
|
|
|
class _ReadState extends ReadStateNotifier {
|
|
@override
|
|
ReadStateState build() => const ReadStateState(
|
|
isReady: true,
|
|
pubkey: 'me',
|
|
contexts: {},
|
|
version: 1,
|
|
);
|
|
|
|
@override
|
|
void markContextRead(
|
|
String contextId,
|
|
int unixTimestamp, {
|
|
bool clearForcedMessages = false,
|
|
bool explicit = false,
|
|
}) {
|
|
state = state.copyWithContext(contextId, unixTimestamp);
|
|
}
|
|
}
|
|
|
|
class _Storage extends CommunityStorage {
|
|
@override
|
|
Future<bool> keeps(String id) async => id == 'hidden';
|
|
}
|
|
|
|
class _Communities extends CommunityListNotifier {
|
|
_Communities([this.communities]);
|
|
final List<Community> Function()? communities;
|
|
|
|
@override
|
|
Future<List<Community>> build() async =>
|
|
communities?.call() ?? [_community, _elsewhere];
|
|
}
|
|
|
|
class _Session extends RelaySessionNotifier {
|
|
_Session({super.rateLimitGate});
|
|
var reachable = true;
|
|
Future<void> Function()? onConnect;
|
|
|
|
@override
|
|
SessionState build() =>
|
|
const SessionState(status: SessionStatus.disconnected);
|
|
@override
|
|
Future<bool> connectForBackgroundWork({required Duration timeout}) async {
|
|
await onConnect?.call();
|
|
return reachable;
|
|
}
|
|
}
|
|
|
|
class _RecordingSender implements SendMessage {
|
|
_RecordingSender({required this.onSend});
|
|
final void Function(String text) onSend;
|
|
final calls = <(String, String, String?, String?, String?)>[];
|
|
@override
|
|
Future<void> call({
|
|
required String channelId,
|
|
required String content,
|
|
String? parentEventId,
|
|
String? rootEventId,
|
|
List<String>? mentionPubkeys,
|
|
Channel? channel,
|
|
List<List<String>> mediaTags = const [],
|
|
FutureOr<void> Function(NostrEvent)? beforePublish,
|
|
void Function()? validateDelivery,
|
|
Future<bool> Function()? mayPublish,
|
|
}) async {
|
|
if (mayPublish != null && !await mayPublish()) {
|
|
throw StateError('no longer wanted');
|
|
}
|
|
calls.add((
|
|
channelId,
|
|
content,
|
|
parentEventId,
|
|
rootEventId,
|
|
mentionPubkeys?.join(','),
|
|
));
|
|
onSend(content);
|
|
if (channelId == 'lost') throw TimeoutException('no OK');
|
|
}
|
|
}
|
|
|
|
/// A connected relay socket that answers each EVENT with [answer]'s reply,
|
|
/// or not at all when it gives null.
|
|
class _Relay extends RelaySocket {
|
|
_Relay(this._session)
|
|
: super(
|
|
wsUrl: 'ws://localhost',
|
|
nsec: null,
|
|
onMessage: (_) {},
|
|
onConnected: () {},
|
|
onDisconnected: (_) {},
|
|
);
|
|
|
|
final RelaySessionNotifier _session;
|
|
final events = <Map<String, dynamic>>[];
|
|
List<dynamic>? Function(String id) answer = (id) => ['OK', id, true, ''];
|
|
|
|
@override
|
|
SocketState get state => SocketState.connected;
|
|
|
|
@override
|
|
void send(List<dynamic> payload) {
|
|
if (payload case ['EVENT', final Map<String, dynamic> event]) {
|
|
events.add(event);
|
|
final reply = answer(event['id'] as String);
|
|
if (reply != null) _session.debugHandleSocketMessageForTest(reply);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> disconnect() async {}
|
|
|
|
@override
|
|
void dispose() {}
|
|
}
|