Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose: Fix sorting of wildcard mentions in case of PMs.
Somesh Ranjand00fa63aa404
diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.jsindex 955374daf17..971e15789ec 100644--- a/frontend_tests/node_tests/composebox_typeahead.js+++ b/frontend_tests/node_tests/composebox_typeahead.js@@ -1486,6 +1486,7 @@ test("content_highlighter", ({override_rewire}) => {});test("filter_and_sort_mentions (normal)", () => {+ compose_state.set_message_type("stream");const is_silent = false;const suggestions = ct.filter_and_sort_mentions(is_silent, "al");diff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.jsindex 81700a944cd..5d73d0b6950 100644--- a/frontend_tests/node_tests/typeahead_helper.js+++ b/frontend_tests/node_tests/typeahead_helper.js@@ -13,7 +13,7 @@ const recent_senders = zrequire("recent_senders");const peer_data = zrequire("peer_data");const people = zrequire("people");const stream_data = zrequire("stream_data");-+const compose_state = zrequire("compose_state");const emoji = zrequire("emoji");const pygments_data = zrequire("../generated/pygments_data.json");const actual_pygments_data = {...pygments_data};@@ -402,6 +402,7 @@ test("sort_recipients", () => {});test("sort_recipients all mention", () => {+ compose_state.set_message_type("stream");const all_obj = ct.broadcast_mentions()[0];assert.equal(all_obj.email, "all");assert.equal(all_obj.is_broadcast, true);@@ -493,6 +494,7 @@ test("sort_recipients dup bots", () => {});test("sort_recipients dup alls", () => {+ compose_state.set_message_type("stream");const all_obj = ct.broadcast_mentions()[0];// full_name starts with same character but emails are 'all'@@ -509,6 +511,22 @@ test("sort_recipients dup alls", () => {assertSameEmails(recipients, expected);});+test("sort_recipients dup alls private", () => {+ compose_state.set_message_type("private");+ const all_obj = ct.broadcast_mentions()[0];++ // full_name starts with same character but emails are 'all'+ const test_objs = [all_obj, a_user, all_obj];++ const recipients = th.sort_recipients({+ users: test_objs,+ query: "a",+ });++ const expected = [a_user, all_obj, all_obj];+ assertSameEmails(recipients, expected);+});+test("sort_recipients subscribers", () => {// b_user_2 is a subscriber and b_user_1 is not.const small_matches = [b_user_2, b_user_1];@@ -538,11 +556,12 @@ test("sort_recipients pm partners", () => {assert.deepEqual(recipients_email, expected);});-test("sort broadcast mentions", () => {+test("sort broadcast mentions for stream message type", () => {// test the normal case, which is that the// broadcast mentions are already sorted (we// actually had a bug where the sort would// randomly rearrange them)+ compose_state.set_message_type("stream");const results = th.sort_people_for_relevance(ct.broadcast_mentions().reverse(), "", "");assert.deepEqual(@@ -565,10 +584,32 @@ test("sort broadcast mentions", () => {);});-test("test compare directly", () => {+test("sort broadcast mentions for private message type", () => {+ compose_state.set_message_type("private");+ const results = th.sort_people_for_relevance(ct.broadcast_mentions().reverse(), "", "");++ assert.deepEqual(+ results.map((r) => r.email),+ ["all", "everyone"],+ );++ const test_objs = Array.from(ct.broadcast_mentions()).reverse();+ test_objs.unshift(zman);+ test_objs.push(a_user);++ const results2 = th.sort_people_for_relevance(test_objs, "", "");++ assert.deepEqual(+ results2.map((r) => r.email),+ [a_user.email, zman.email, "all", "everyone"],+ );+});++test("test compare directly for stream message type", () => {// This is important for ensuring test coverage.// We don't technically need it now, but our test// coverage is subject to the whims of how JS sorts.+ compose_state.set_message_type("stream");const all_obj = ct.broadcast_mentions()[0];assert.equal(th.compare_people_for_relevance(all_obj, all_obj), 0);@@ -576,6 +617,15 @@ test("test compare directly", () => {assert.equal(th.compare_people_for_relevance(zman, all_obj), 1);});+test("test compare directly for private message", () => {+ compose_state.set_message_type("private");+ const all_obj = ct.broadcast_mentions()[0];++ assert.equal(th.compare_people_for_relevance(all_obj, all_obj), 0);+ assert.equal(th.compare_people_for_relevance(all_obj, zman), 1);+ assert.equal(th.compare_people_for_relevance(zman, all_obj), -1);+});+test("highlight_with_escaping", () => {function highlight(query, item) {const regex = th.build_highlight_regex(query);diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.jsindex 07c0bdf55e0..b9a22e27317 100644--- a/static/js/typeahead_helper.js+++ b/static/js/typeahead_helper.js@@ -6,6 +6,7 @@ import * as typeahead from "../shared/js/typeahead";import render_typeahead_list_item from "../templates/typeahead_list_item.hbs";import * as buddy_data from "./buddy_data";+import * as compose_state from "./compose_state";import * as people from "./people";import * as pm_conversations from "./pm_conversations";import * as recent_senders from "./recent_senders";@@ -14,7 +15,6 @@ import * as stream_data from "./stream_data";import * as user_groups from "./user_groups";import * as user_status from "./user_status";import * as util from "./util";-// Returns an array of private message recipients, removing empty elements.// For example, "a,,b, " => ["a", "b"]export function get_cleaned_pm_recipients(query_string) {@@ -186,13 +186,24 @@ export function compare_people_for_relevance(// We use is_broadcast for a quick check. It will// true for all/everyone/stream and undefined (falsy)// for actual people.- if (person_a.is_broadcast) {- if (person_b.is_broadcast) {- return person_a.idx - person_b.idx;+ if (compose_state.get_message_type() !== "private") {+ if (person_a.is_broadcast) {+ if (person_b.is_broadcast) {+ return person_a.idx - person_b.idx;+ }+ return -1;+ } else if (person_b.is_broadcast) {+ return 1;+ }+ } else {+ if (person_a.is_broadcast) {+ if (person_b.is_broadcast) {+ return person_a.idx - person_b.idx;+ }+ return 1;+ } else if (person_b.is_broadcast) {+ return -1;}- return -1;- } else if (person_b.is_broadcast) {- return 1;}// Now handle actual people users.