Skip to content

Commit c1cf4f0

Browse files
square-tombclaude
andcommitted
fix(mobile): refresh the relay assertion before building reaction sheet headers
The reaction sheet builds its native avatar and custom emoji headers from the synchronous assertion cache, which reads as empty once the cached assertion is inside its refresh margin even though the live socket is still valid. An enforcing relay would then refuse those native image loads. Refresh the assertion through HostedRelayAuth.headers before the sheet is built, as the emoji picker already does, and send without it only on an AccountFailure. Add a test that opens the sheet with the cached assertion inside its refresh margin. Co-Authored-By: Claude Sonnet <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 0826a8c commit c1cf4f0

2 files changed

Lines changed: 185 additions & 8 deletions

File tree

‎mobile/lib/features/channels/reaction_row.dart‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import '../../shared/widgets/app_list_card_item.dart';
1111
import '../../shared/widgets/avatar_image.dart';
1212
import '../../shared/widgets/modal_presentation.dart';
1313
import '../../shared/widgets/native_message_presentation.dart';
14+
import '../../shared/hosted/builderlab_account.dart';
15+
import '../../shared/hosted/hosted_relay_auth.dart';
1416
import '../../shared/hosted/native_media_headers.dart';
17+
import '../../shared/relay/relay_provider.dart';
1518
import '../../shared/custom_emoji/custom_emoji_render.dart';
1619
import '../../shared/emoji/emoji_burst.dart';
1720
import '../../shared/emoji/emoji_avatar.dart';
@@ -725,6 +728,18 @@ Future<bool> _showNativeReactionDetails(
725728
var addRequested = false;
726729
final handled = await NativeMessagePresentation.withLease(() async {
727730
final container = ProviderScope.containerOf(context, listen: false);
731+
final theme = Theme.of(context);
732+
// The native headers read the cached assertion synchronously, and that
733+
// cache reads as empty inside its refresh margin while the live socket is
734+
// still valid. Refresh it first so avatars and custom emoji are not refused.
735+
try {
736+
await container
737+
.read(hostedRelayAuthProvider)
738+
.headers(container.read(relayConfigProvider));
739+
} on AccountFailure {
740+
// Send without the assertion; the relay decides, and sign-in is offered
741+
// elsewhere.
742+
}
728743
final nativeMediaHeaders = container.read(nativeMediaHeadersProvider);
729744
final requestId = DateTime.now().microsecondsSinceEpoch.toString();
730745
List<String> pubkeys() =>
@@ -837,17 +852,15 @@ Future<bool> _showNativeReactionDetails(
837852

838853
reactions.addListener(updateReactions);
839854
try {
840-
final sheetColors = utilitySurfaceThemeData(
841-
Theme.of(context),
842-
).colorScheme;
855+
final sheetColors = utilitySurfaceThemeData(theme).colorScheme;
843856
final pending = NativeMessagePresentation.present('reactions', {
844857
'requestId': requestId,
845858
'sheetColor': sheetColors.surface.toARGB32(),
846859
'foregroundColor': sheetColors.primary.toARGB32(),
847-
'outlineColor': Theme.of(
848-
context,
849-
).colorScheme.onSurface.withValues(alpha: 0.25).toARGB32(),
850-
'dark': Theme.of(context).brightness == Brightness.dark,
860+
'outlineColor': theme.colorScheme.onSurface
861+
.withValues(alpha: 0.25)
862+
.toARGB32(),
863+
'dark': theme.brightness == Brightness.dark,
851864
'canAddReaction': onAddReaction != null,
852865
'addReactionIconCodePoint': BuzzIcons.smilePlus.codePoint,
853866
'profiles': profiles(),

‎mobile/test/features/channels/reaction_row_test.dart‎

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:buzz/features/channels/channel_management_provider.dart';
22
import 'package:buzz/shared/custom_emoji/custom_emoji_render.dart';
33
import 'package:buzz/shared/emoji/native_emoji_glyph.dart';
4-
import 'package:buzz/shared/relay/media_image.dart';
54
import 'package:http/http.dart' as http;
65
import 'package:http/testing.dart';
76
import 'package:buzz/shared/emoji/emoji_avatar.dart';
@@ -12,7 +11,13 @@ import 'package:buzz/features/channels/channel_identity_names_provider.dart';
1211
import 'package:buzz/shared/identity_names/identity_names.dart';
1312
import 'package:buzz/features/channels/message_long_press_region.dart';
1413
import 'package:buzz/features/channels/reaction_row.dart';
14+
import 'package:buzz/shared/hosted/builderlab_account.dart';
15+
import 'package:buzz/shared/hosted/hosted_relay_auth.dart';
1516
import 'package:buzz/shared/hosted/native_media_headers.dart';
17+
import 'package:buzz/shared/relay/relay.dart';
18+
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
19+
import 'package:nostr/nostr.dart' as nostr;
20+
import 'dart:convert';
1621
import 'package:buzz/features/channels/timeline_message.dart';
1722
import 'package:buzz/shared/emoji/emoji_burst.dart';
1823
import 'package:buzz/shared/emoji/emoji_data.dart';
@@ -389,6 +394,156 @@ void main() {
389394
},
390395
);
391396

397+
testWidgets(
398+
'native reaction sheet refreshes an assertion inside its refresh margin',
399+
(tester) async {
400+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
401+
addTearDown(() => debugDefaultTargetPlatformOverride = null);
402+
FlutterSecureStorage.setMockInitialValues({});
403+
final keys = nostr.Keys.generate();
404+
final organization = Uri.parse('https://org.example');
405+
final config = RelayConfig(
406+
baseUrl: 'https://relay.example',
407+
nsec: keys.nsec,
408+
);
409+
final expiry = DateTime.now().toUtc().add(const Duration(days: 1));
410+
await const FlutterSecureStorage().write(
411+
key: 'buzz_hosted_session_org_example_v1',
412+
value: jsonEncode({
413+
'origin': organization.toString(),
414+
'credential': 'secret',
415+
'subject': 'member',
416+
'expires_at': expiry.toIso8601String(),
417+
}),
418+
);
419+
var clock = DateTime.utc(2026, 11, 4, 12);
420+
var issued = 0;
421+
final auth = HostedRelayAuth(
422+
BuilderlabAccountClient(),
423+
organization: BuilderlabAccountClient(
424+
accountOrigin: organization,
425+
httpClient: MockClient((request) async {
426+
return switch (request.url.path) {
427+
'/api/goose/v1/auth/me' => http.Response(
428+
jsonEncode({
429+
'subject': 'member',
430+
'expires_at': expiry.toIso8601String(),
431+
}),
432+
200,
433+
),
434+
'/api/goose/v1/buzz/nostr-identities/current' => http.Response(
435+
jsonEncode({
436+
'identity': {'pubkey_hex': keys.public},
437+
}),
438+
200,
439+
),
440+
'/api/goose/v1/buzz/identity/assertions' => http.Response(
441+
jsonEncode({
442+
'assertion': 'token-${++issued}',
443+
'nostr_pubkey': keys.public,
444+
'expires_at': clock.millisecondsSinceEpoch ~/ 1000 + 120,
445+
}),
446+
200,
447+
),
448+
_ => http.Response('{}', 404),
449+
};
450+
}),
451+
),
452+
organizationRelay: (relay) => relay.host == 'relay.example',
453+
now: () => clock,
454+
);
455+
addTearDown(auth.dispose);
456+
// The live socket's assertion, issued when the session connected.
457+
expect(await auth.headers(config), {
458+
'Nostr-Federated-Identity': 'Bearer token-1',
459+
});
460+
final payloads = <Map<Object?, Object?>>[];
461+
final completion = Completer<Map<String, Object?>>();
462+
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
463+
NativeMessagePresentation.channel,
464+
(call) async {
465+
if (call.method == 'reactions') {
466+
payloads.add(call.arguments as Map<Object?, Object?>);
467+
return completion.future;
468+
}
469+
return <String, Object?>{};
470+
},
471+
);
472+
addTearDown(
473+
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
474+
NativeMessagePresentation.channel,
475+
null,
476+
),
477+
);
478+
await tester.pumpWidget(
479+
WidgetHelpers.testable(
480+
overrides: [
481+
emojiDatasetOrEmptyProvider.overrideWithValue(_dataset),
482+
relayConfigProvider.overrideWith(
483+
() => _FixedRelayConfigNotifier(config),
484+
),
485+
hostedRelayAuthProvider.overrideWithValue(auth),
486+
userCacheProvider.overrideWith(
487+
() => _FakeUserCacheNotifier({
488+
_alice: const UserProfile(
489+
pubkey: _alice,
490+
displayName: 'Honey',
491+
avatarUrl: _aliceAvatarUrl,
492+
),
493+
}),
494+
),
495+
channelIdentityNamesProvider('channel').overrideWith(
496+
(ref) => IdentityNameSources(
497+
profiles: ref.watch(userCacheProvider),
498+
).scope([_alice]),
499+
),
500+
],
501+
child: ReactionRow(
502+
messageId: _messageId,
503+
channelId: 'channel',
504+
reactions: [
505+
TimelineReaction(
506+
emoji: _fire,
507+
count: 1,
508+
reactedByCurrentUser: false,
509+
userPubkeys: [_alice],
510+
emojiUrl: _customEmojiUrl,
511+
),
512+
],
513+
onToggle: (_) {},
514+
),
515+
),
516+
);
517+
await tester.pumpAndSettle();
518+
// Rendering the row may itself fetch media through the hosted client,
519+
// so enter the margin only now (120s lifetime, 30s margin): the socket
520+
// is still valid, but the synchronous cache reads as empty.
521+
final issuedBeforeSheet = issued;
522+
clock = clock.add(const Duration(seconds: 100));
523+
expect(auth.cachedHeaders(config), isNull);
524+
await tester.longPress(
525+
find.byKey(const ValueKey('reaction-pill-$_fire')),
526+
);
527+
await tester.pumpAndSettle();
528+
529+
expect(issued, issuedBeforeSheet + 1);
530+
final payload = payloads.single;
531+
final avatar = (payload['profiles'] as Map)[_alice] as Map;
532+
expect(
533+
(avatar['headers'] as Map)['Nostr-Federated-Identity'],
534+
'Bearer token-$issued',
535+
);
536+
final reaction = (payload['reactions'] as List).single as Map;
537+
expect(
538+
(reaction['headers'] as Map)['Nostr-Federated-Identity'],
539+
'Bearer token-$issued',
540+
);
541+
completion.complete(<String, Object?>{});
542+
await tester.pumpAndSettle();
543+
debugDefaultTargetPlatformOverride = null;
544+
},
545+
);
546+
392547
testWidgets(
393548
'native hydration sends bounded diffs and preserves emoji avatars',
394549
(tester) async {
@@ -927,3 +1082,12 @@ void main() {
9271082
});
9281083
});
9291084
}
1085+
1086+
class _FixedRelayConfigNotifier extends RelayConfigNotifier {
1087+
_FixedRelayConfigNotifier(this._config);
1088+
1089+
final RelayConfig _config;
1090+
1091+
@override
1092+
RelayConfig build() => _config;
1093+
}

0 commit comments

Comments
 (0)