From 87d78072a1aac0c4f469ca8858f90f0ff77c605e Mon Sep 17 00:00:00 2001 From: Logan Johnson Date: Mon, 7 Sep 2026 11:07:23 -0400 Subject: [PATCH] fix(mobile): keep unbound qualified mentions non-notifying Signed-off-by: Logan Johnson --- .../lib/shared/mentions/mention_bindings.dart | 18 +++++++++++++----- .../compose_bar_test/exact_mention_tests.dart | 19 +++++++++++++++++++ .../message_content_exact_mentions_test.dart | 17 +++++++++++++++++ .../mentions/mention_bindings_test.dart | 7 ++++++- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/mobile/lib/shared/mentions/mention_bindings.dart b/mobile/lib/shared/mentions/mention_bindings.dart index 0ecee7ddaf..56b12b2a9d 100644 --- a/mobile/lib/shared/mentions/mention_bindings.dart +++ b/mobile/lib/shared/mentions/mention_bindings.dart @@ -29,8 +29,14 @@ List<({int start, int end, String label})> mentionOccurrences( final matches = {}; for (final label in labels) { if (label.isEmpty) continue; + // A qualifier and reservation suffix belong to the literal even when no + // candidate binds it. Never fall back to a shorter, different recipient. + final suffix = + RegExp(r' \([0-9a-f]{64}\)$', caseSensitive: false).hasMatch(label) + ? r'(?! (?:[2-9]|[1-9][0-9]+)(?=[\s,;.!?:)\]}*_]|$))' + : ''; final pattern = RegExp( - '(?:^|\\s|[*_]{1,3}|\\|\\|)(@${RegExp.escape(label)})(?=\\|\\||[\\s,;.!?:)\\]}*_]|\$)', + '(?:^|\\s|[*_]{1,3}|\\|\\|)(@${RegExp.escape(label)})(?! \\([0-9a-f]{64}\\))$suffix(?=\\|\\||[\\s,;.!?:)\\]}*_]|\$)', caseSensitive: false, ); for (final match in pattern.allMatches(text)) { @@ -70,12 +76,14 @@ Map> renderedMentionBindings( ).allMatches(content)) { final key = match.group(2)!.toLowerCase(); final label = match.group(0)!.substring(1).toLowerCase(); - if (!keys.contains(key) || - !mentionOccurrences(content, [ - label, - ]).any((range) => range.start == match.start)) { + if (!mentionOccurrences(content, [ + label, + ]).any((range) => range.start == match.start)) { continue; } + // Untagged qualified literals are recognition blockers, not bindings. + bindings.putIfAbsent(label, () => {}); + if (!keys.contains(key)) continue; qualified.add(( label: label, base: match.group(1)!.toLowerCase(), diff --git a/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart b/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart index 136897f4d4..fed5600cf5 100644 --- a/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart +++ b/mobile/test/features/channels/compose_bar_test/exact_mention_tests.dart @@ -59,6 +59,25 @@ void exactMentionTests() { }, ); + testWidgets('unbound qualified text cannot notify a shorter member alias', ( + tester, + ) async { + List? sent; + await tester.pumpWidget( + _buildComposeBar( + uploadService: _testUploadService(nostr.Keys.generate().nsec), + members: [members().first], + onSend: (_, keys, {mediaTags = const []}) async => sent = keys, + ), + ); + await _expandComposer(tester); + await tester.enterText(find.byType(TextField), '@Scout ($second)'); + await tester.pumpAndSettle(); + await tester.tap(find.byIcon(LucideIcons.arrowUp)); + await tester.pumpAndSettle(); + expect(sent, isEmpty); + }); + testWidgets('ambiguous typed names fail visibly without clearing the draft', ( tester, ) async { diff --git a/mobile/test/features/channels/message_content_exact_mentions_test.dart b/mobile/test/features/channels/message_content_exact_mentions_test.dart index 6f9bde3d60..5b11c502c6 100644 --- a/mobile/test/features/channels/message_content_exact_mentions_test.dart +++ b/mobile/test/features/channels/message_content_exact_mentions_test.dart @@ -3,6 +3,23 @@ import 'package:buzz/features/channels/message_content.dart'; import '../../helpers/widget_helpers.dart'; void main() { + testWidgets('untagged qualifiers cannot become shorter clickable aliases', ( + tester, + ) async { + await tester.pumpWidget( + WidgetHelpers.testable( + child: MessageContent( + content: '@Scout (${'b' * 64})', + mentionNames: {'a' * 64: 'Scout'}, + onMentionTap: (_) => fail('unbound text is not a profile target'), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('Scout'), findsNothing); + expect(tester.takeException(), isNull); + }); + testWidgets('namesake chips resolve exact tagged keys, not tag order', ( tester, ) async { diff --git a/mobile/test/shared/mentions/mention_bindings_test.dart b/mobile/test/shared/mentions/mention_bindings_test.dart index 8b0af126c2..e73a64e5d7 100644 --- a/mobile/test/shared/mentions/mention_bindings_test.dart +++ b/mobile/test/shared/mentions/mention_bindings_test.dart @@ -23,6 +23,11 @@ void main() { ); expect(mentionOccurrences('@A @B', ['A @B', 'B']).single.label, 'A @B'); expect(mentionOccurrences('mail@Scout', ['Scout']), isEmpty); + expect(mentionOccurrences('@Scout ($second)', ['Scout']), isEmpty); + expect( + mentionOccurrences('@Scout ($second) 2', ['Scout ($second)']), + isEmpty, + ); }); test( 'tagged qualified identity narrows a namesake independently of tag order', @@ -49,7 +54,7 @@ void main() { renderedMentionBindings('@Scout ($second)', { first: 'Scout', })['scout ($second)'], - isNull, + isEmpty, ); expect( renderedMentionBindings('@Old ($second)', {