Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typeahead: Make typeahead_helper.sort_recipients more readable.
m-e-l-u-h-a-n22b1f6e1ab5c
diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.jsindex 4f359eb5446..077d701c256 100644--- a/frontend_tests/node_tests/composebox_typeahead.js+++ b/frontend_tests/node_tests/composebox_typeahead.js@@ -757,12 +757,12 @@ test("initialize", (override) => {assert.equal(matcher(query, deactivated_user), true);function sorter(query, people) {- return typeahead_helper.sort_recipients(- people,+ return typeahead_helper.sort_recipients({+ users: people,query,- compose_state.stream_name(),- compose_state.topic(),- );+ current_stream: compose_state.stream_name(),+ current_topic: compose_state.topic(),+ });}// The sorter's output has the items that match the query from thediff --git a/frontend_tests/node_tests/typeahead_helper.js b/frontend_tests/node_tests/typeahead_helper.jsindex 48e0126262d..96edd01bc30 100644--- a/frontend_tests/node_tests/typeahead_helper.js+++ b/frontend_tests/node_tests/typeahead_helper.js@@ -301,12 +301,12 @@ test("sort_languages", () => {});function get_typeahead_result(query, current_stream, current_topic) {- const result = th.sort_recipients(- people.get_realm_users(),+ const result = th.sort_recipients({+ users: people.get_realm_users(),query,current_stream,current_topic,- );+ });return result.map((person) => person.email);}@@ -411,7 +411,12 @@ test("sort_recipients all mention", () => {// Test person email is "all" or "everyone"const test_objs = matches.concat([all_obj]);- const results = th.sort_recipients(test_objs, "a", "Linux", "Linux topic");+ const results = th.sort_recipients({+ users: test_objs,+ query: "a",+ current_stream: "Linux",+ current_topic: "Linux topic",+ });assertSameEmails(results, [all_obj, a_bot, a_user, b_user_1, b_user_2, b_user_3, b_bot, zman]);});@@ -467,7 +472,12 @@ test("sort_recipients pm counts", () => {test("sort_recipients dup bots", () => {const dup_objects = matches.concat([a_bot]);- const recipients = th.sort_recipients(dup_objects, "b", "", "");+ const recipients = th.sort_recipients({+ users: dup_objects,+ query: "b",+ current_stream: "",+ current_topic: "",+ });const recipients_email = recipients.map((person) => person.email);const expected = [@@ -488,7 +498,12 @@ test("sort_recipients dup alls", () => {// full_name starts with same character but emails are 'all'const test_objs = [all_obj, a_user, all_obj];- const recipients = th.sort_recipients(test_objs, "a", "Linux", "Linux topic");+ const recipients = th.sort_recipients({+ users: test_objs,+ query: "a",+ current_stream: "Linux",+ current_topic: "Linux topic",+ });const expected = [all_obj, all_obj, a_user];assertSameEmails(recipients, expected);@@ -497,7 +512,12 @@ test("sort_recipients dup alls", () => {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];- const recipients = th.sort_recipients(small_matches, "b", "Dev", "Dev topic");+ const recipients = th.sort_recipients({+ users: small_matches,+ query: "b",+ current_stream: "Dev",+ current_topic: "Dev topic",+ });const recipients_email = recipients.map((person) => person.email);const expected = ["[email protected]", "[email protected]"];assert.deepEqual(recipients_email, expected);@@ -507,7 +527,12 @@ test("sort_recipients pm partners", () => {// b_user_3 is a pm partner and b_user_2 is not and// both are not subscribered to the stream Linux.const small_matches = [b_user_3, b_user_2];- const recipients = th.sort_recipients(small_matches, "b", "Linux", "Linux topic");+ const recipients = th.sort_recipients({+ users: small_matches,+ query: "b",+ current_stream: "Linux",+ current_topic: "Linux topic",+ });const recipients_email = recipients.map((person) => person.email);const expected = ["[email protected]", "[email protected]"];assert.deepEqual(recipients_email, expected);diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.jsindex 41f6a1e6d68..d55c8179920 100644--- a/static/js/composebox_typeahead.js+++ b/static/js/composebox_typeahead.js@@ -496,14 +496,14 @@ export function get_person_suggestions(query, opts) {filtered_persons = filter_persons(people.get_realm_users());}- return typeahead_helper.sort_recipients(- filtered_persons,+ return typeahead_helper.sort_recipients({+ users: filtered_persons,query,- opts.stream,- opts.topic,- filtered_groups,+ current_stream: opts.stream,+ current_topic: opts.topic,+ groups: filtered_groups,max_num_items,- );+ });}export function get_stream_topic_data(hacky_this) {diff --git a/static/js/pill_typeahead.js b/static/js/pill_typeahead.jsindex ed3354d9379..45b80f5cac2 100644--- a/static/js/pill_typeahead.js+++ b/static/js/pill_typeahead.js@@ -91,8 +91,9 @@ export function set_up(input, pills, opts) {return person_matcher(query, item);},sorter(matches) {- if (include_streams(this.query)) {- return typeahead_helper.sort_streams(matches, this.query.trim().slice(1));+ const query = this.query;+ if (include_streams(query)) {+ return typeahead_helper.sort_streams(matches, query.trim().slice(1));}const users = matches.filter((ele) => people.is_known_user(ele));@@ -100,14 +101,14 @@ export function set_up(input, pills, opts) {if (include_user_groups) {groups = matches.filter((ele) => user_groups.is_user_group(ele));}- return typeahead_helper.sort_recipients(+ return typeahead_helper.sort_recipients({users,- this.query,- "",- undefined,+ query,+ current_stream: "",+ current_topic: undefined,groups,- undefined,- );+ max_num_items: undefined,+ });},updater(item) {if (include_streams(this.query)) {diff --git a/static/js/typeahead_helper.js b/static/js/typeahead_helper.jsindex 0fd2a62c9a4..e292ffc0aa9 100644--- a/static/js/typeahead_helper.js+++ b/static/js/typeahead_helper.js@@ -287,14 +287,14 @@ export function sort_languages(matches, query) {return retain_unique_language_aliases(results.matches.concat(results.rest));}-export function sort_recipients(+export function sort_recipients({users,query,current_stream,current_topic,groups = [],max_num_items = 20,-) {+}) {function sort_relevance(items) {return sort_people_for_relevance(items, current_stream, current_topic);}