search_suggestion: Use person matching function from people.js.

Instead of having a custom (duplicate) matching function in
search suggestion, it was refactored to use the function in
people.js. This also gets the diacritic-ignoring feature
of the function in people.js.

Fixes #5315.
This commit is contained in:
Cory Lynch 2017-06-23 02:39:37 -04:00 committed by showell
parent 46d224f021
commit 090d7487cf
2 changed files with 23 additions and 22 deletions

View File

@ -622,10 +622,8 @@ init();
}()); }());
(function test_stream_completion() { (function test_stream_completion() {
var query = 'stream:of';
global.stream_data.subscribed_streams = function () { global.stream_data.subscribed_streams = function () {
return ['office']; return ['office', 'dev help'];
}; };
global.narrow_state.stream = function () { global.narrow_state.stream = function () {
@ -634,13 +632,20 @@ init();
global.stream_data.populate_stream_topics_for_tests({}); global.stream_data.populate_stream_topics_for_tests({});
var query = 'stream:of';
var suggestions = search.get_suggestions(query); var suggestions = search.get_suggestions(query);
var expected = [ var expected = [
"stream:of", "stream:of",
"stream:office", "stream:office",
]; ];
assert.deepEqual(suggestions.strings, expected);
query = 'hel';
suggestions = search.get_suggestions(query);
expected = [
"hel",
"stream:dev+help",
];
assert.deepEqual(suggestions.strings, expected); assert.deepEqual(suggestions.strings, expected);
}()); }());
@ -664,7 +669,7 @@ init();
var bob = { var bob = {
email: 'bob@zulip.com', email: 'bob@zulip.com',
user_id: 202, user_id: 202,
full_name: 'Bob Terry', full_name: 'Bob Térry',
}; };
var alice = { var alice = {
@ -689,7 +694,7 @@ init();
var expected = [ var expected = [
"te", "te",
"pm-with:bob@zulip.com", // bob TErry "pm-with:bob@zulip.com", // bob rry
"pm-with:ted@zulip.com", "pm-with:ted@zulip.com",
"sender:bob@zulip.com", "sender:bob@zulip.com",
"sender:ted@zulip.com", "sender:ted@zulip.com",

View File

@ -21,10 +21,6 @@ function phrase_match(phrase, q) {
return false; return false;
} }
function person_matches_query(person, q) {
return phrase_match(person.full_name, q) || phrase_match(person.email, q);
}
function stream_matches_query(stream_name, q) { function stream_matches_query(stream_name, q) {
return phrase_match(stream_name, q); return phrase_match(stream_name, q);
} }
@ -83,7 +79,7 @@ function get_stream_suggestions(last, operators) {
return objs; return objs;
} }
function get_group_suggestions(all_people, last, operators) { function get_group_suggestions(all_persons, last, operators) {
if (last.operator !== 'pm-with' ) { if (last.operator !== 'pm-with' ) {
return []; return [];
} }
@ -118,21 +114,21 @@ function get_group_suggestions(all_people, last, operators) {
// We don't suggest a person if their email is already present in the // We don't suggest a person if their email is already present in the
// operand (not including the last part). // operand (not including the last part).
var parts = all_but_last_part.split(','); var parts = all_but_last_part.split(',');
var people = _.filter(all_people, function (person) { var persons = _.filter(all_persons, function (person) {
if (_.contains(parts, person.email)) { if (_.contains(parts, person.email)) {
return false; return false;
} }
return (last_part === '') || person_matches_query(person, last_part); return (last_part === '') || people.person_matches_query(person, last_part);
}); });
people.sort(typeahead_helper.compare_by_pms); persons.sort(typeahead_helper.compare_by_pms);
// Take top 15 people, since they're ordered by pm_recipient_count. // Take top 15 persons, since they're ordered by pm_recipient_count.
people = people.slice(0, 15); persons = persons.slice(0, 15);
var prefix = Filter.operator_to_prefix('pm-with', negated); var prefix = Filter.operator_to_prefix('pm-with', negated);
var suggestions = _.map(people, function (person) { var suggestions = _.map(persons, function (person) {
var term = { var term = {
operator: 'pm-with', operator: 'pm-with',
operand: all_but_last_part + ',' + person.email, operand: all_but_last_part + ',' + person.email,
@ -152,7 +148,7 @@ function get_group_suggestions(all_people, last, operators) {
} }
// Possible args for autocomplete_operator: pm-with, sender, from // Possible args for autocomplete_operator: pm-with, sender, from
function get_person_suggestions(all_people, last, operators, autocomplete_operator) { function get_person_suggestions(all_persons, last, operators, autocomplete_operator) {
if (last.operator === "is" && last.operand === "private") { if (last.operator === "is" && last.operand === "private") {
// Interpret 'is:private' as equivalent to 'pm-with:' // Interpret 'is:private' as equivalent to 'pm-with:'
last = {operator: "pm-with", operand: "", negated: false}; last = {operator: "pm-with", operand: "", negated: false};
@ -182,15 +178,15 @@ function get_person_suggestions(all_people, last, operators, autocomplete_operat
return []; return [];
} }
var people = _.filter(all_people, function (person) { var persons = _.filter(all_persons, function (person) {
return person_matches_query(person, query); return people.person_matches_query(person, query);
}); });
people.sort(typeahead_helper.compare_by_pms); persons.sort(typeahead_helper.compare_by_pms);
var prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated); var prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated);
var objs = _.map(people, function (person) { var objs = _.map(persons, function (person) {
var name = highlight_person(query, person); var name = highlight_person(query, person);
var description = prefix + ' ' + name; var description = prefix + ' ' + name;
var terms = [{ var terms = [{