mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
## Summary A new message shifts the timeline and can unmount the long-pressed message row while its actions sheet or emoji picker is open. Then: - tapping "+" in the actions sheet did nothing, because the picker opened from the row's dead context (after [#9820 emoji picker crash](https://github.com/block/buzz/pull/9820), it is a silent no-op instead of a crash), and - a pick made after the row went away read through the row's disposed `WidgetRef`, so the reaction was dropped. The actions sheet and the timeline "+" pill now capture the page's `ProviderContainer` and its navigator's context when they open. The picker opens from the navigator, and reaction dispatch reads through the container. `toggleReaction` and `armReactionBurst` take a container, and grouped system rows read through one too. The reactions tray's Add button keeps working after the row is gone, since its callback holds the container and navigator it captured at build time. Reactions are also bound to the community that was active when the surface opened: the actions sheet and the reaction details tray (Flutter sheet or native iOS tray) capture it when they open, and the timeline "+" pill when it is tapped. If a notification switches community meanwhile, a quick tile, picker pick, tray toggle or tray Add is dropped instead of being recorded, animated or published as the newly active account. `ChannelActions.isCommunityValid` now uses the same `RelayConfig.isSameCommunity` comparison. Failure notices for these reactions show from the captured navigator's context, so they still appear when the row is gone. ## Regressions None expected. The picker attaches to the same navigator as before (the row's nearest navigator), and the providers read are the same ones, from the same `ProviderScope`. ## Validation - New regression tests in `message_actions_row_rebuild_tests.dart`: the row unmounts before "+" is tapped, and while the native picker is open. Both failed on main (picker never presented, pick lost) and pass with the fix. Two more switch community while the sheet is open, then tap a quick tile or pick from "+", and check nothing is added, recorded or armed. - New test in the reaction row combined tests: the row unmounts while the native reactions tray is open, and its Add still opens the picker. Another switches community while the tray is open and checks that a tray toggle and Add are both dropped. - Automated simulator app test (iOS 27 sim): opened a channel, long-pressed a message, tapped "+", picked an emoji in the native picker, and saw the reaction land. Then toggled it off from the reactions sheet. Live edits and reactions from another client arrived during the run without issue. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
167 lines
6.1 KiB
Dart
167 lines
6.1 KiB
Dart
import 'package:buzz/shared/crypto/event_signing.dart';
|
|
import 'package:buzz/shared/http/app_http_client.dart';
|
|
import 'package:flutter/foundation.dart' show visibleForTesting;
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
import '../hosted/hosted_relay_auth.dart';
|
|
import '../community/community_provider.dart';
|
|
import 'relay_client.dart';
|
|
|
|
/// Relay connection configuration.
|
|
///
|
|
/// In the pure-nostr world the only secrets the app cares about are:
|
|
/// - `baseUrl` — where the relay lives (used for WS + media upload)
|
|
/// - `nsec` — the user's signing key (drives NIP-42 AUTH and event sigs)
|
|
class RelayConfig {
|
|
const RelayConfig({required String baseUrl, String? nsec})
|
|
: _baseUrl = baseUrl,
|
|
_nsec = nsec;
|
|
|
|
/// Relay origin exactly as the active community stored it.
|
|
final String _baseUrl;
|
|
|
|
/// Normalized Nostr secret key shared by identity, signing, and NIP-42 AUTH.
|
|
String? get nsec => _nsec?.trim();
|
|
|
|
final String? _nsec;
|
|
|
|
/// The origin as persisted, before scheme canonicalization.
|
|
///
|
|
/// Exists solely so identity-scoped storage keys written before [baseUrl]
|
|
/// was canonicalized stay reachable — see [readMigratedPref]. Never use it
|
|
/// for network I/O; [baseUrl] and [wsUrl] are the addresses to connect to.
|
|
String get storedOrigin => _baseUrl;
|
|
|
|
/// Whether [other] signs as the same account on the same relay, so work
|
|
/// started under one config can still be sent under the other.
|
|
bool isSameCommunity(RelayConfig other) =>
|
|
baseUrl == other.baseUrl && nsec == other.nsec;
|
|
|
|
/// Relay origin as an HTTP(S) URL.
|
|
///
|
|
/// Communities are persisted with whichever scheme their onboarding flow
|
|
/// used: device pairing stores `https://` (it rejects anything else), while
|
|
/// an invite join stores the `wss://` relay URL carried by the invite link.
|
|
/// Every consumer treats this as an HTTP origin — [wsUrl], the `/query`
|
|
/// endpoint, media upload and Blossom auth — so a `wss://` base silently
|
|
/// degrades all of them. Folding the websocket schemes back here keeps both
|
|
/// onboarding paths equivalent, including for already-persisted communities.
|
|
///
|
|
/// Derived rather than normalized in the constructor so that the constructor
|
|
/// stays `const`: the compile-time fallback below relies on canonicalization
|
|
/// to keep its identity stable across rebuilds, and Riverpod's default
|
|
/// `updateShouldNotify` is `previous != next`, which falls back to identity
|
|
/// here. A fresh instance per rebuild would resubscribe every listener.
|
|
String get baseUrl {
|
|
final uri = Uri.tryParse(_baseUrl);
|
|
if (uri == null) return _baseUrl;
|
|
final scheme = switch (uri.scheme) {
|
|
'wss' => 'https',
|
|
'ws' => 'http',
|
|
_ => null,
|
|
};
|
|
return scheme == null ? _baseUrl : uri.replace(scheme: scheme).toString();
|
|
}
|
|
|
|
/// Derive the websocket URL from the HTTP base URL.
|
|
String get wsUrl {
|
|
final uri = Uri.parse(baseUrl);
|
|
final scheme = uri.scheme == 'https' ? 'wss' : 'ws';
|
|
return uri.replace(scheme: scheme).toString();
|
|
}
|
|
}
|
|
|
|
/// Compile-time environment config via --dart-define.
|
|
///
|
|
/// Run with:
|
|
/// flutter run --dart-define=BUZZ_RELAY_URL=http://localhost:3000 \
|
|
/// --dart-define=BUZZ_PUSH_GATEWAY_URL=http://localhost:8080
|
|
///
|
|
/// Or create a `.env.json` and use --dart-define-from-file=.env.json
|
|
class Env {
|
|
static const relayUrl = String.fromEnvironment(
|
|
'BUZZ_RELAY_URL',
|
|
defaultValue: 'http://localhost:3000',
|
|
);
|
|
|
|
/// Optional gateway origin. Without it this artifact has no push capability.
|
|
static String get pushGatewayUrl =>
|
|
debugPushGatewayUrlOverride ?? _compiledPushGatewayUrl;
|
|
|
|
static const _compiledPushGatewayUrl = String.fromEnvironment(
|
|
'BUZZ_PUSH_GATEWAY_URL',
|
|
);
|
|
|
|
/// Replaces the compiled gateway origin in tests, so a plain `flutter test`
|
|
/// covers both push-capable and push-less builds without a `--dart-define`.
|
|
/// `test/flutter_test_config.dart` sets it for every test file.
|
|
@visibleForTesting
|
|
static String? debugPushGatewayUrlOverride;
|
|
|
|
/// Whether this artifact can offer push notification enrollment.
|
|
static bool get pushGatewayConfigured => pushGatewayUrl != '';
|
|
}
|
|
|
|
class RelayConfigNotifier extends Notifier<RelayConfig> {
|
|
@override
|
|
RelayConfig build() {
|
|
// Push lease bookkeeping updates the community while using its live
|
|
// session. Only connection-context changes may restart that session.
|
|
final context = ref.watch(
|
|
activeCommunityProvider.select((value) {
|
|
final active = value.value;
|
|
return (id: active?.id, relayUrl: active?.relayUrl, nsec: active?.nsec);
|
|
}),
|
|
);
|
|
if (context.relayUrl != null) {
|
|
return RelayConfig(baseUrl: context.relayUrl!, nsec: context.nsec);
|
|
}
|
|
|
|
// Fallback to compile-time env config (dev mode).
|
|
return const RelayConfig(baseUrl: Env.relayUrl);
|
|
}
|
|
|
|
void update({required String baseUrl, String? nsec}) {
|
|
state = RelayConfig(baseUrl: baseUrl, nsec: nsec);
|
|
}
|
|
}
|
|
|
|
final relayConfigProvider = NotifierProvider<RelayConfigNotifier, RelayConfig>(
|
|
RelayConfigNotifier.new,
|
|
);
|
|
|
|
/// Derive the hex pubkey from a bech32 nsec, or null on any failure.
|
|
String? pubkeyFromNsec(String? nsec) {
|
|
if (nsec == null || nsec.isEmpty) return null;
|
|
try {
|
|
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
|
|
if (privkeyHex.isEmpty) return null;
|
|
return derivePublicKeyHex(privkeyHex);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// The current user's hex pubkey, derived from the active community nsec.
|
|
final myPubkeyProvider = Provider<String?>((ref) {
|
|
final config = ref.watch(relayConfigProvider);
|
|
return pubkeyFromNsec(config.nsec);
|
|
});
|
|
|
|
/// Provides a [RelayClient] that reacts to config changes.
|
|
///
|
|
/// Only used for the media upload HTTP endpoint now — all data flow goes
|
|
/// through the relay WebSocket session.
|
|
final relayClientProvider = Provider<RelayClient>((ref) {
|
|
final config = ref.watch(relayConfigProvider);
|
|
final client = RelayClient(
|
|
baseUrl: config.baseUrl,
|
|
httpClient: ref.watch(hostedHttpClientFactoryProvider)(
|
|
traffic: HttpTraffic.media,
|
|
),
|
|
);
|
|
ref.onDispose(client.dispose);
|
|
return client;
|
|
});
|