Skip to content

Commit b4aa278

Browse files
committed
autocomplete: Add "alphabetical order" criterion
In @-mention autocomplete, users are suggested based on: 1. Recent activity in the current topic/stream. 2. Recent DM conversations. 3. Human vs. Bot users (human users come first). 4. Alphabetical order. Fixes: #228
1 parent 979fe13 commit b4aa278

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/model/autocomplete.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ class MentionAutocompleteView extends ChangeNotifier {
249249
} else if (userA.isBot && !userB.isBot) {
250250
return 1;
251251
}
252-
return 0;
252+
253+
final userAName = store.autocompleteViewManager.autocompleteDataCache
254+
.normalizedNameForUser(userA);
255+
final userBName = store.autocompleteViewManager.autocompleteDataCache
256+
.normalizedNameForUser(userB);
257+
return userAName.compareTo(userBName);
253258
}
254259

255260
/// Determines which of the two users has more recent activity (messages sent

test/model/autocomplete_test.dart

+22-7
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,17 @@ void main() {
508508
}
509509

510510
test('TopicNarrow: topic recency > stream recency > DM recency '
511-
'> human vs. bot user', () async {
511+
'> human vs. bot user > alphabetical order', () async {
512512
final users = [
513-
eg.user(),
513+
eg.user(fullName: 'Z'),
514514
eg.user(),
515515
eg.user(isBot: true),
516516
eg.user(),
517517
eg.user(),
518518
eg.user(),
519519
eg.user(isBot: true),
520+
eg.user(fullName: 'ab'),
521+
eg.user(fullName: 'bc'),
520522
];
521523
final stream = eg.stream();
522524
final narrow = TopicNarrow(stream.streamId, 'this');
@@ -533,16 +535,20 @@ void main() {
533535
checkPrecedes(narrow, users[2], users.skip(3));
534536
checkRankEqual(narrow, [users[3], users[4]]);
535537
checkPrecedes(narrow, users[5], users.skip(6));
538+
checkPrecedes(narrow, users[7], users.skip(8));
536539
});
537540

538-
test('ChannelNarrow: stream recency > DM recency > human vs. bot user', () async {
541+
test('ChannelNarrow: stream recency > DM recency > human vs. bot user '
542+
'> alphabetical order', () async {
539543
final users = [
540-
eg.user(isBot: true),
544+
eg.user(isBot: true, fullName: 'Z'),
541545
eg.user(),
542546
eg.user(),
543547
eg.user(),
544548
eg.user(),
545549
eg.user(isBot: true),
550+
eg.user(fullName: 'ab', isBot: true),
551+
eg.user(fullName: 'bc', isBot: true),
546552
];
547553
final stream = eg.stream();
548554
final narrow = ChannelNarrow(stream.streamId);
@@ -557,17 +563,20 @@ void main() {
557563
checkPrecedes(narrow, users[1], users.skip(2));
558564
checkRankEqual(narrow, [users[2], users[3]]);
559565
checkPrecedes(narrow, users[4], users.skip(5));
566+
checkPrecedes(narrow, users[6], users.skip(7));
560567
});
561568

562569
test('DmNarrow: DM recency > this-conversation recency or stream recency '
563-
'or human vs. bot user', () async {
570+
'or human vs. bot user or alphabetical order', () async {
564571
final users = [
565-
eg.user(isBot: true),
572+
eg.user(isBot: true, fullName: 'Z'),
566573
eg.user(),
567574
eg.user(),
568575
eg.user(),
569576
eg.user(),
570577
eg.user(isBot: true),
578+
eg.user(fullName: 'ab'),
579+
eg.user(fullName: 'bc'),
571580
];
572581
await prepare(users: users, messages: [
573582
eg.dmMessage(from: users[3], to: [eg.selfUser]),
@@ -588,6 +597,7 @@ void main() {
588597
checkPrecedes(narrow, users[1], users.skip(3));
589598
checkPrecedes(narrow, users[2], users.skip(3));
590599
checkPrecedes(narrow, users[4], users.skip(5));
600+
checkPrecedes(narrow, users[6], users.skip(7));
591601
}
592602
});
593603

@@ -626,6 +636,8 @@ void main() {
626636
eg.user(userId: 5, fullName: 'User Five'),
627637
eg.user(userId: 6, fullName: 'User Six', isBot: true),
628638
eg.user(userId: 7, fullName: 'User Seven'),
639+
eg.user(userId: 8, fullName: 'User Xy', isBot: true),
640+
eg.user(userId: 9, fullName: 'User Xz', isBot: true),
629641
];
630642

631643
await prepare(users: users, messages: [
@@ -642,8 +654,9 @@ void main() {
642654
// 1. Users most recent in the current topic/stream.
643655
// 2. Users most recent in the DM conversations.
644656
// 3. Human vs. Bot users (human users come first).
657+
// 4. Alphabetical order.
645658
check(await getResults(topicNarrow, MentionAutocompleteQuery('')))
646-
.deepEquals([1, 5, 4, 2, 3, 7, 6]);
659+
.deepEquals([1, 5, 4, 2, 7, 3, 6, 8, 9]);
647660

648661
// Check the ranking applies also to results filtered by a query.
649662
check(await getResults(topicNarrow, MentionAutocompleteQuery('t')))
@@ -652,6 +665,8 @@ void main() {
652665
.deepEquals([5, 4]);
653666
check(await getResults(topicNarrow, MentionAutocompleteQuery('s')))
654667
.deepEquals([7, 6]);
668+
check(await getResults(topicNarrow, MentionAutocompleteQuery('user x')))
669+
.deepEquals([8, 9]);
655670
});
656671
});
657672
}

0 commit comments

Comments
 (0)