search: Extract 'Sent by me' suggestion logic into separate method.

This also adds a nice test suite for it.
This commit is contained in:
Sampriti Panda
2017-01-11 23:50:31 +05:30
committed by Tim Abbott
parent c613e510ed
commit e666a62c6a
2 changed files with 92 additions and 4 deletions

View File

@@ -245,6 +245,62 @@ people.init();
assert.equal(describe('stream:devel'), 'Narrow to stream <strong>devel</strong>');
}());
(function test_sent_by_me_suggestions() {
global.stream_data.subscribed_streams = function () {
return [];
};
global.narrow.stream = function () {
return undefined;
};
var query = '';
var suggestions = search.get_suggestions(query);
assert(suggestions.strings.indexOf('sender:bob@zulip.com') !== -1);
assert.equal(suggestions.lookup_table['sender:bob@zulip.com'].description,
'Sent by me');
query = 'sender';
suggestions = search.get_suggestions(query);
var expected = [
"sender",
"sender:bob@zulip.com",
];
assert.deepEqual(suggestions.strings, expected);
query = 'from';
suggestions = search.get_suggestions(query);
expected = [
"from",
"from:bob@zulip.com",
];
assert.deepEqual(suggestions.strings, expected);
query = 'sender:bob@zulip.com';
suggestions = search.get_suggestions(query);
expected = [
"sender:bob@zulip.com",
"is:private",
];
assert.deepEqual(suggestions.strings, expected);
query = 'from:bob@zulip.com';
suggestions = search.get_suggestions(query);
expected = [
"from:bob@zulip.com",
"is:private",
];
assert.deepEqual(suggestions.strings, expected);
query = 'sent';
suggestions = search.get_suggestions(query);
expected = [
"sent",
"sender:bob@zulip.com",
];
assert.deepEqual(suggestions.strings, expected);
}());
(function test_topic_suggestions() {
var suggestions;
var expected;

View File

@@ -346,10 +346,6 @@ function get_special_filter_suggestions(query, operators) {
search_string: 'is:alerted',
description: 'Alerted messages',
},
{
search_string: 'sender:' + page_params.email,
description: 'Sent by me',
},
];
query = query.toLowerCase();
@@ -368,6 +364,39 @@ function get_special_filter_suggestions(query, operators) {
return suggestions;
}
function get_sent_by_me_suggestions(query, operators) {
if (operators.length >= 2) {
return [];
}
var sender_query = 'sender:' + page_params.email;
var from_query = 'from:' + page_params.email;
var description = 'Sent by me';
query = query.toLowerCase();
if (query === sender_query || query === from_query) {
return [];
} else if (query === '' ||
sender_query.indexOf(query) === 0 ||
description.toLowerCase().indexOf(query) === 0) {
return [
{
search_string: sender_query,
description: description,
},
];
} else if (from_query.indexOf(query) === 0) {
return [
{
search_string: from_query,
description: description,
},
];
}
return [];
}
exports.get_suggestions = function (query) {
// This method works in tandem with the typeahead library to generate
// search suggestions. If you want to change its behavior, be sure to update
@@ -386,6 +415,9 @@ exports.get_suggestions = function (query) {
suggestions = get_special_filter_suggestions(query, operators);
result = result.concat(suggestions);
suggestions = get_sent_by_me_suggestions(query, operators);
result = result.concat(suggestions);
suggestions = get_stream_suggestions(operators);
result = result.concat(suggestions);