diff --git a/mobile/lib/features/channels/channel_management_provider.dart b/mobile/lib/features/channels/channel_management_provider.dart index 8e4398ee82..718cf0eab0 100644 --- a/mobile/lib/features/channels/channel_management_provider.dart +++ b/mobile/lib/features/channels/channel_management_provider.dart @@ -24,8 +24,24 @@ export 'channel_metadata_updates.dart'; part 'channel_huddle_actions.dart'; part 'channel_management_actions.dart'; -String _relayErrorMessage(Object error) => - error.toString().replaceFirst('Exception: ', ''); +/// The text of [error] without the `Exception: ` a plain [Exception] adds. +/// A relay rejection keeps its message verbatim, and a lost acknowledgement +/// drops the prefix only from its cause. Named types keep their whole name: +/// `TimeoutException: ...` stays as is. +String _relayErrorMessage(Object error) { + if (error is RelayEventRejectedException) return error.message; + final text = error.toString(); + final cause = error is RelayAckLostError ? error.cause : null; + if (cause != null) { + final causeText = cause.toString(); + if (text.endsWith(causeText)) { + return text.substring(0, text.length - causeText.length) + + _relayErrorMessage(cause); + } + } + const prefix = 'Exception: '; + return text.startsWith(prefix) ? text.substring(prefix.length) : text; +} /// Raised when one or more kind:9000 adds were rejected, keyed by pubkey. /// diff --git a/mobile/test/features/channels/channel_management_provider_test.dart b/mobile/test/features/channels/channel_management_provider_test.dart index 6598681c22..1827149be5 100644 --- a/mobile/test/features/channels/channel_management_provider_test.dart +++ b/mobile/test/features/channels/channel_management_provider_test.dart @@ -489,6 +489,51 @@ void main() { }, ); + test('add-members failures keep each error name but drop a bare ' + 'Exception prefix', () async { + final keys = nostr.Keys.generate(); + final timedOut = 'a' * 64; + final rejected = 'b' * 64; + final lost = 'c' * 64; + final session = _RecordingPublishRelaySession() + ..publishErrors.addAll({ + timedOut: RelayAckTimeoutError('Event e not acknowledged'), + rejected: const RelayEventRejectedException( + 'restricted: Exception: sponsor approval required', + ), + lost: RelayAckLostError(Exception('App moved to background')), + }); + final actionsProvider = Provider((ref) { + return ChannelActions( + ref: ref, + session: session, + signedEventRelay: SignedEventRelay(session: session, nsec: keys.nsec), + currentPubkey: keys.public, + ); + }); + final container = ProviderContainer(retry: (_, _) => null); + addTearDown(container.dispose); + + await expectLater( + container + .read(actionsProvider) + .addMembers( + channelId: _channelId, + pubkeys: [timedOut, rejected, lost], + role: 'member', + ), + throwsA( + isA().having((e) => e.failures, 'failures', { + timedOut: 'TimeoutException: Event e not acknowledged', + rejected: 'restricted: Exception: sponsor approval required', + lost: + 'Connection lost before the relay acknowledged the event: ' + 'App moved to background', + }), + ), + ); + }); + test('reactions tag the reacted-to message author', () async { final keys = nostr.Keys.generate(); final session = _RecordingPublishRelaySession(); @@ -945,6 +990,9 @@ const _memberPubkey = class _RecordingPublishRelaySession extends RelaySessionNotifier { final publishedEvents = []; final queryFilters = []; + + /// Errors to throw instead of publishing, keyed by the event's `p` tag. + final publishErrors = {}; int historyQueryCount = 0; @override @@ -956,6 +1004,12 @@ class _RecordingPublishRelaySession extends RelaySessionNotifier { Duration timeout = const Duration(seconds: 8), Future Function()? mayPublish, }) async { + final target = event.tags + .where((tag) => tag.length > 1 && tag[0] == 'p') + .map((tag) => tag[1]) + .firstOrNull; + final error = publishErrors[target]; + if (error != null) throw error; publishedEvents.add(event); return event; }