mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
fix(mobile): let closing punctuation select an exact mention name
A comma typed straight after `@Bob` or `@Quinn` became part of the query and closed the chooser: `@Bob,` was notified only through the typed-name fallback, and `@Quinn,`, a person outside the channel, was sent with no recipient and no prompt (scenario 06). Closing punctuation typed directly after the query now selects the exact, unique name exactly as Space does (portable mention rules, section 5; recommendation 7), and the typed character stays after the chip in place of the space: `@bob,` becomes a bound Bob chip followed by `,` with no extra space. The selecting characters are buzz-app's `mentionTerminator` set, Space, `.` `,` `!` `?` `;` `:` `)` and the closing quotes `'` `’` `"` `”`, plus `]` and `}` for the brackets a query opens after. `mentionTerminator` in `mention_rules.dart` holds the set; the composer listener applies the same exact-and-unique check it already ran for Space, through the folded name matching, so an ambiguous `@Sam,` and an inexact `@Bo,` are not selected and the character joins the query, and a hyphen is never a terminator. `insertMention` takes the separator to keep after the label, defaulting to the space. A directory-only person goes through the existing chip and outside-channel prompt flow exactly as a tapped pick does: `@Quinn,` is a Quinn chip, and sending meets the prompt. The edit sheet and focus handling are unchanged. Tests: the chip journey selects Bob with each terminator and sends the chip text with Bob tagged, and keeps `@bo,` and `@bob-` unselected; the search journey selects a found Quinn with a comma and sends through the outside-channel prompt as a reference; the namesake tests check that a comma never selects a shared name. Both journeys fail when the terminator set is reduced to Space. `dart format`, `flutter analyze` and the full mobile `flutter test` suite pass; two full runs each hit one unrelated timing flake (relay socket liveness, community snapshot cleanup) that passes solo and alongside the changed tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Matt Toohey <contact@matttoohey.com>
This commit is contained in:
@@ -341,8 +341,9 @@ class ComposeBar extends HookConsumerWidget {
|
||||
final isModifyingText = useRef(false);
|
||||
final lastObservedEditingValue = useRef(controller.value);
|
||||
|
||||
// Insert a selected mention into the text field as a chip bound to it.
|
||||
void insertMention(MentionCandidate candidate) {
|
||||
// Insert a selected mention into the text field as a chip bound to it,
|
||||
// followed by a space or by the closing punctuation that selected it.
|
||||
void insertMention(MentionCandidate candidate, {String separator = ' '}) {
|
||||
ref
|
||||
.read(mentionHistoryProvider.notifier)
|
||||
.remember(channelId, candidate.pubkey);
|
||||
@@ -359,6 +360,7 @@ class ComposeBar extends HookConsumerWidget {
|
||||
start: start,
|
||||
end: cursor,
|
||||
candidate: candidate,
|
||||
separator: separator,
|
||||
);
|
||||
} finally {
|
||||
isModifyingText.value = false;
|
||||
@@ -422,24 +424,30 @@ class ComposeBar extends HookConsumerWidget {
|
||||
open = null;
|
||||
}
|
||||
|
||||
// Space after an exact, unique name selects it (section 5).
|
||||
// Space or closing punctuation typed straight after an exact, unique
|
||||
// name selects it (section 5); the typed character stays after the
|
||||
// chip. Otherwise the character joins the query.
|
||||
final previousQuery = mentionQuery.value;
|
||||
if (open != null &&
|
||||
previousQuery != null &&
|
||||
previousQuery.trim().isNotEmpty &&
|
||||
open.start == mentionStartIdx.value &&
|
||||
open.query == '$previousQuery ' &&
|
||||
open.query.length == previousQuery.length + 1 &&
|
||||
open.query.startsWith(previousQuery) &&
|
||||
text.length == previousValue.text.length + 1) {
|
||||
final chosen = _spaceMention(
|
||||
ref,
|
||||
channelId: channelId,
|
||||
query: previousQuery,
|
||||
opening: mentionOpening.value,
|
||||
shown: shownMentions.value,
|
||||
viewer: currentPubkey,
|
||||
);
|
||||
final typed = open.query.substring(previousQuery.length);
|
||||
final chosen = mentionTerminator(typed)
|
||||
? _spaceMention(
|
||||
ref,
|
||||
channelId: channelId,
|
||||
query: previousQuery,
|
||||
opening: mentionOpening.value,
|
||||
shown: shownMentions.value,
|
||||
viewer: currentPubkey,
|
||||
)
|
||||
: null;
|
||||
if (chosen != null) {
|
||||
insertMention(chosen);
|
||||
insertMention(chosen, separator: typed);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,16 +134,18 @@ class _MarkdownEditingController extends TextEditingController {
|
||||
if (unchanged) notifyListeners();
|
||||
}
|
||||
|
||||
/// Replaces the `@query` at `[start, end)` with `@Label ` bound to
|
||||
/// [candidate], caret after the space. Mentions after it move with the
|
||||
/// text in the same notification.
|
||||
/// Replaces the `@query` at `[start, end)` with `@Label` and [separator]
|
||||
/// (a space, or the closing punctuation that selected the name) bound to
|
||||
/// [candidate], caret after the separator. Mentions after it move with
|
||||
/// the text in the same notification.
|
||||
void insertMention({
|
||||
required int start,
|
||||
required int end,
|
||||
required MentionCandidate candidate,
|
||||
String separator = ' ',
|
||||
}) {
|
||||
final label = candidate.label;
|
||||
final replacement = '@$label ';
|
||||
final replacement = '@$label$separator';
|
||||
final next = TextEditingValue(
|
||||
text: text.replaceRange(start, end, replacement),
|
||||
selection: TextSelection.collapsed(offset: start + replacement.length),
|
||||
|
||||
@@ -246,6 +246,19 @@ String? exactMention(Iterable<MentionChoice> choices, String query) {
|
||||
return count == 1 ? found : null;
|
||||
}
|
||||
|
||||
final _mentionTerminators = RegExp(r'''^[ .,!?;:)\]}'’"”]$''', unicode: true);
|
||||
|
||||
/// Section 5: whether [character], typed straight after a query, selects
|
||||
/// its exact, unique name as Space does. Besides Space: closing punctuation,
|
||||
/// a closing bracket for the ones a query opens after, or a closing quote,
|
||||
/// so `@Bob,` and `@Bob's` mention Bob. The client keeps the character after
|
||||
/// the mention in place of the space. Where Space would not select, the
|
||||
/// character is typed into the query instead: `'` after `Pat O` while
|
||||
/// `Pat O'Brien` is known stays in the query. Any other character, such as
|
||||
/// a hyphen, is never a terminator.
|
||||
bool mentionTerminator(String character) =>
|
||||
_mentionTerminators.hasMatch(character);
|
||||
|
||||
/// An open `@` query: `[start, end)` covers `@` and the query text.
|
||||
typedef MentionQuery = ({int start, int end, String query});
|
||||
|
||||
|
||||
@@ -423,4 +423,29 @@ void exactMentionTests() {
|
||||
);
|
||||
expect(find.textContaining('is ambiguous'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('closing punctuation never selects a shared name', (
|
||||
tester,
|
||||
) async {
|
||||
await mountComposer(
|
||||
tester,
|
||||
members(),
|
||||
(_, _, {mediaTags = const []}) async {},
|
||||
);
|
||||
final field = find.byType(TextField);
|
||||
await tester.enterText(field, '@scout');
|
||||
await tester.pumpAndSettle();
|
||||
expect(scoutRows(), findsNWidgets(2));
|
||||
await tester.enterText(field, '@scout,');
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.widget<TextField>(field).controller!.text, '@scout,');
|
||||
expect(
|
||||
find.byKey(const ValueKey('composer-person-mention-chip')),
|
||||
findsNothing,
|
||||
);
|
||||
// Dispose before draining the chooser's pending search debounce, as the
|
||||
// restore scenarios above do.
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pump(mentionSearchDebounce * 2);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -216,6 +216,52 @@ void mentionChipTests() {
|
||||
await tester.pumpAndSettle();
|
||||
expect(sent, [alice]);
|
||||
|
||||
// Closing punctuation typed straight after an exact, unique name
|
||||
// selects it as Space does, and stays after the chip in place of the
|
||||
// space (recommendation 7): `@bob,` is a bound Bob followed by `,`.
|
||||
for (final mark in [
|
||||
',',
|
||||
'.',
|
||||
';',
|
||||
':',
|
||||
'!',
|
||||
'?',
|
||||
')',
|
||||
']',
|
||||
'}',
|
||||
"'",
|
||||
'’',
|
||||
'"',
|
||||
'”',
|
||||
]) {
|
||||
sent = null;
|
||||
await tester.enterText(find.byType(TextField), 'ping @bob');
|
||||
await tester.pump();
|
||||
await tester.enterText(find.byType(TextField), 'ping @bob$mark');
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.text, 'ping @Bob$mark', reason: mark);
|
||||
expect(controller.selection.baseOffset, 'ping @Bob$mark'.length);
|
||||
expect(personChip, findsOneWidget, reason: mark);
|
||||
await tester.tap(find.byIcon(BuzzIcons.arrowUp));
|
||||
await tester.pumpAndSettle();
|
||||
expect(sentText, 'ping @Bob$mark');
|
||||
expect(sent, [bob], reason: mark);
|
||||
}
|
||||
|
||||
// An inexact name (`@bo,`) and a character that is not a terminator
|
||||
// (`@bob-`) keep the chooser's query: nothing is selected.
|
||||
for (final text in ['@bo,', '@bob-']) {
|
||||
await tester.enterText(
|
||||
find.byType(TextField),
|
||||
text.substring(0, text.length - 1),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.enterText(find.byType(TextField), text);
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.text, text);
|
||||
expect(personChip, findsNothing, reason: text);
|
||||
}
|
||||
|
||||
// Typed, unpicked names keep their existing fallback: an exact unique
|
||||
// member name notifies, after a bracket as after a space.
|
||||
sent = null;
|
||||
|
||||
@@ -48,6 +48,8 @@ void mentionSearchTests() {
|
||||
'the directory is searched only while a query could still name someone',
|
||||
(tester) async {
|
||||
final searched = <String>[];
|
||||
String? sentText;
|
||||
List<List<String>>? sentTags;
|
||||
await tester.pumpWidget(
|
||||
_buildComposeBar(
|
||||
uploadService: _testUploadService(nostr.Keys.generate().nsec),
|
||||
@@ -70,7 +72,10 @@ void mentionSearchTests() {
|
||||
person,
|
||||
];
|
||||
},
|
||||
onSend: (_, _, {mediaTags = const <List<String>>[]}) async {},
|
||||
onSend: (text, _, {mediaTags = const <List<String>>[]}) async {
|
||||
sentText = text;
|
||||
sentTags = mediaTags;
|
||||
},
|
||||
),
|
||||
);
|
||||
await _expandComposer(tester);
|
||||
@@ -106,6 +111,37 @@ void mentionSearchTests() {
|
||||
expect(row('Quinn'), findsOneWidget);
|
||||
expect(row('not in channel'), findsOneWidget);
|
||||
|
||||
// Closing punctuation after a person the directory found selects them
|
||||
// as Space does once the search has finished (recommendation 7), and
|
||||
// the send meets the outside-channel prompt as after a tapped pick.
|
||||
await type(tester, '@qu', 'inn');
|
||||
expect(row('Quinn'), findsOneWidget);
|
||||
await tester.enterText(field, '@quinn,');
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.text, '@Quinn,');
|
||||
expect(
|
||||
find.byKey(const ValueKey('composer-person-mention-chip')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(popover, findsNothing);
|
||||
await tester.tap(find.byIcon(BuzzIcons.arrowUp));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Mention people outside this channel?'), findsOneWidget);
|
||||
// Send without inviting: Quinn is referenced, not notified.
|
||||
await tester.tap(
|
||||
find
|
||||
.descendant(
|
||||
of: find.byType(AlertDialog),
|
||||
matching: find.byType(TextButton),
|
||||
)
|
||||
.first,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(sentText, '@Quinn,');
|
||||
expect(sentTags, contains(orderedEquals(['mention', quinn.pubkey])));
|
||||
// The prompt took focus; the composer is a capsule again.
|
||||
await _expandComposer(tester);
|
||||
|
||||
// Multi-word names are searched word by word: a second word that
|
||||
// continues no local name still goes to the directory, and one that
|
||||
// continues a found name keeps that person listed. Names fold accents
|
||||
|
||||
Reference in New Issue
Block a user