Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead: Redesign the function `compare_people_for_relevance`.

N-Shar-maa3ed41efa085
diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.ts
index 5b43d6f8f73..e8a4cedaa78 100644
--- a/web/src/typeahead_helper.ts
+++ b/web/src/typeahead_helper.ts
@@ -217,7 +217,6 @@ export function compare_by_pms(user_a: User, user_b: User): number {
// recent DM message history with a user, but does have some older
// message history that's outside the "recent messages only"
// data set powering people.get_recipient_count.
- /* istanbul ignore next */
if (a_is_partner && !b_is_partner) {
return -1;
} else if (!a_is_partner && b_is_partner) {
@@ -243,7 +242,7 @@ export function compare_by_pms(user_a: User, user_b: User): number {
export function compare_people_for_relevance(
person_a: UserOrMention,
person_b: UserOrMention,
- tertiary_compare: (user_a: User, user_b: User) => number,
+ compare_by_current_conversation?: (user_a: User, user_b: User) => number,
current_stream_id?: number,
): number {
// give preference to "all", "everyone" or "stream"
@@ -284,17 +283,14 @@ export function compare_people_for_relevance(
}
}
- // give preference to direct message partners if both (are)/(are not) subscribers
- const a_is_partner = pm_conversations.is_partner(person_a.user_id);
- const b_is_partner = pm_conversations.is_partner(person_b.user_id);
-
- if (a_is_partner && !b_is_partner) {
- return -1;
- } else if (!a_is_partner && b_is_partner) {
- return 1;
+ if (compare_by_current_conversation !== undefined) {
+ const preference = compare_by_current_conversation(person_a, person_b);
+ if (preference !== 0) {
+ return preference;
+ }
}
- return tertiary_compare(person_a, person_b);
+ return compare_by_pms(person_a, person_b);
}
export function sort_people_for_relevance(
@@ -308,9 +304,7 @@ export function sort_people_for_relevance(
current_stream = stream_data.get_sub_by_id(current_stream_id);
}
if (!current_stream) {
- objs.sort((person_a, person_b) =>
- compare_people_for_relevance(person_a, person_b, compare_by_pms),
- );
+ objs.sort((person_a, person_b) => compare_people_for_relevance(person_a, person_b));
} else {
objs.sort((person_a, person_b) =>
compare_people_for_relevance(
diff --git a/web/tests/typeahead_helper.test.js b/web/tests/typeahead_helper.test.js
index 5bf7b2c3148..bad7301e1b1 100644
--- a/web/tests/typeahead_helper.test.js
+++ b/web/tests/typeahead_helper.test.js
@@ -432,9 +432,9 @@ test("sort_recipients", () => {
]);
});
@@ -455,7 +455,7 @@ test("sort_recipients all mention", () => {
current_topic: "Linux topic",
});
- assertSameEmails(results, [all_obj, a_bot, a_user, b_user_1, b_user_2, b_user_3, b_bot, zman]);
+ assertSameEmails(results, [all_obj, a_user, a_bot, b_user_1, b_user_2, b_user_3, zman, b_bot]);
});
test("sort_recipients pm counts", () => {
@@ -482,8 +482,8 @@ test("sort_recipients pm counts", () => {
assert.deepEqual(get_typeahead_result("b", linux_sub.stream_id, "Linux topic"), [
@@ -578,6 +578,32 @@ test("sort_recipients subscribers", () => {
assert.deepEqual(recipients_email, expected);
});
+test("sort_recipients recent senders", () => {
+ // b_user_2 is the only recent sender, b_user_3 is the only pm partner
+ // and all are subscribed to the stream Linux.
+ const small_matches = [b_user_1, b_user_2, b_user_3];
+ peer_data.add_subscriber(linux_sub.stream_id, b_user_1.user_id);
+ peer_data.add_subscriber(linux_sub.stream_id, b_user_2.user_id);
+ peer_data.add_subscriber(linux_sub.stream_id, b_user_3.user_id);
+ recent_senders.process_stream_message({
+ sender_id: b_user_2.user_id,
+ stream_id: linux_sub.stream_id,
+ topic: "Linux topic",
+ id: (next_id += 1),
+ });
+ pm_conversations.set_partner(b_user_3.user_id);
+ const recipients = th.sort_recipients({
+ users: small_matches,
+ query: "b",
+ current_stream_id: linux_sub.stream_id,
+ current_topic: "Linux topic",
+ });
+ const recipients_email = recipients.map((person) => person.email);
+ // Prefer recent sender over pm partner
+ assert.deepEqual(recipients_email, expected);
+});
+
test("sort_recipients pm partners", () => {
// b_user_3 is a pm partner and b_user_2 is not and
// both are not subscribed to the stream Linux.