Skip to content

Commit

Permalink
Prioritize stream subscribers in typeahead list.
Browse files Browse the repository at this point in the history
When autocompleting names using @mention, subscribers of the stream
would appear at the top of the list. Added new test module to reflect
this change.
  • Loading branch information
tommyip authored and showell committed Dec 21, 2016
1 parent 516ccd7 commit 400c8c1
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 7 deletions.
103 changes: 103 additions & 0 deletions frontend_tests/node_tests/typeahead_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
var th = require('js/typeahead_helper.js');

global.stub_out_jquery();

set_global('compose', {});
set_global('page_params', {is_zephyr_mirror_realm: false});

add_dependencies({
stream_data: 'js/stream_data.js',
people: 'js/people.js',
});

stream_data.create_streams([
{name: 'Dev', subscribed: true, color: 'blue', stream_id: 1},
{name: 'Linux', subscribed: true, color: 'red', stream_id: 2}
]);

var matches = [
{
email: "[email protected]",
full_name: "A zulip test bot",
is_admin: false,
is_bot: true,
user_id: 1,
}, {
email: "[email protected]",
full_name: "A zulip user",
is_admin: false,
is_bot: false,
user_id: 2,
}, {
email: "[email protected]",
full_name: "Bob 1",
is_admin: false,
is_bot: false,
user_id: 3,
}, {
email: "[email protected]",
full_name: "Bob 2",
is_admin: true,
is_bot: false,
user_id: 4,
}, {
email: "[email protected]",
full_name: "B bot",
is_admin: false,
is_bot: true,
user_id: 5,
}, {
email: "[email protected]",
full_name: "Zman",
is_admin: false,
is_bot: false,
user_id: 6,
}
];

_.each(matches, function (person) {
global.people.add_in_realm(person);
});

(function test_sort_recipients() {
function get_typeahead_result(query) {
var result = th.sort_recipients(global.people.get_realm_persons(), query);
return _.map(result, function (person) {
return person.email;
});
}

global.compose.stream_name = function () { return ""; };
assert.deepEqual(get_typeahead_result("b"), [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]);

global.compose.stream_name = function () { return "Dev"; };
var subscriber_email = "[email protected]";
stream_data.add_subscriber("Dev", people.get_user_id(subscriber_email));
assert.deepEqual(get_typeahead_result("b"), [
subscriber_email,
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]);

// No match
global.compose.stream_name = function () { return "Linux"; };
assert.deepEqual(get_typeahead_result("h"), [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]);

}());
41 changes: 34 additions & 7 deletions static/js/typeahead_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ function prefix_sort(query, objs, get_item) {

}

function split_by_subscribers(people) {
var subscribers = [];
var non_subscribers = [];
var current_stream = compose.stream_name();

if (current_stream === "") {
// If there is no stream specified, everyone is considered as a subscriber.
return {subs: people, non_subs: []};
}

_.each(people, function (person) {
if (person.email === "all" || person.email === "everyone") {
subscribers.push(person);
} else if (stream_data.user_is_subscribed(current_stream, person.email)) {
subscribers.push(person);
} else {
non_subscribers.push(person);
}
});

return {subs: subscribers, non_subs: non_subscribers};
}

exports.sorter = function (query, objs, get_item) {
var results = prefix_sort(query, objs, get_item);
return results.matches.concat(results.rest);
Expand Down Expand Up @@ -126,18 +149,22 @@ exports.compare_by_pms = function (user_a, user_b) {
return 1;
};

exports.sort_by_pms = function (objs) {
objs.sort(exports.compare_by_pms);
return objs;
exports.sort_for_at_mentioning = function (objs) {
var objs_split = split_by_subscribers(objs);

var subs_sorted = objs_split.subs.sort(exports.compare_by_pms);
var non_subs_sorted = objs_split.non_subs.sort(exports.compare_by_pms);
return subs_sorted.concat(non_subs_sorted);
};

exports.sort_recipients = function (matches, query) {
var name_results = prefix_sort(query, matches, function (x) { return x.full_name; });
var email_results = prefix_sort(query, name_results.rest, function (x) { return x.email; });
var matches_sorted_by_pms =
exports.sort_by_pms(name_results.matches.concat(email_results.matches));
var rest_sorted_by_pms = exports.sort_by_pms(email_results.rest);
return matches_sorted_by_pms.concat(rest_sorted_by_pms);

var matches_sorted =
exports.sort_for_at_mentioning(name_results.matches.concat(email_results.matches));
var rest_sorted = exports.sort_for_at_mentioning(email_results.rest);
return matches_sorted.concat(rest_sorted);
};

exports.sort_emojis = function (matches, query) {
Expand Down

0 comments on commit 400c8c1

Please sign in to comment.