2013-07-30 23:02:10 +02:00
|
|
|
var search_suggestion = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
function phrase_match(phrase, q) {
|
|
|
|
// match "tes" to "test" and "stream test" but not "hostess"
|
|
|
|
var i;
|
|
|
|
q = q.toLowerCase();
|
|
|
|
|
2013-08-07 15:40:47 +02:00
|
|
|
phrase = phrase.toLowerCase();
|
|
|
|
if (phrase.indexOf(q) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
var parts = phrase.split(' ');
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < parts.length; i += 1) {
|
2013-08-07 15:40:47 +02:00
|
|
|
if (parts[i].indexOf(q) === 0) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stream_matches_query(stream_name, q) {
|
|
|
|
return phrase_match(stream_name, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
function highlight_person(query, person) {
|
|
|
|
var hilite = typeahead_helper.highlight_query_in_phrase;
|
|
|
|
return hilite(query, person.full_name) + " <" + hilite(query, person.email) + ">";
|
|
|
|
}
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function match_criteria(operators, criteria) {
|
|
|
|
var filter = new Filter(operators);
|
|
|
|
return _.any(criteria, function (cr) {
|
|
|
|
if (_.has(cr, 'operand')) {
|
|
|
|
return filter.has_operand(cr.operator, cr.operand);
|
|
|
|
}
|
|
|
|
return filter.has_operator(cr.operator);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-05 02:58:05 +02:00
|
|
|
function compare_by_huddle(huddle) {
|
|
|
|
huddle = _.map(huddle.slice(0, -1), function (person) {
|
|
|
|
person = people.get_by_email(person);
|
|
|
|
if (person) {
|
|
|
|
return person.user_id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Construct dict for all huddles, so we can lookup each's recency
|
|
|
|
var huddles = activity.get_huddles();
|
|
|
|
var huddle_dict = {};
|
|
|
|
for (var i = 0; i < huddles.length; i += 1) {
|
|
|
|
huddle_dict[huddles[i]] = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (person1, person2) {
|
|
|
|
var huddle1 = huddle.concat(person1.user_id).sort().join(',');
|
|
|
|
var huddle2 = huddle.concat(person2.user_id).sort().join(',');
|
|
|
|
|
|
|
|
// If not in the dict, assign an arbitrarily high index
|
|
|
|
var score1 = huddle_dict[huddle1] || 100;
|
|
|
|
var score2 = huddle_dict[huddle2] || 100;
|
|
|
|
var diff = score1 - score2;
|
|
|
|
|
|
|
|
if (diff !== 0) {
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
return typeahead_helper.compare_by_pms(person1, person2);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:10:13 +02:00
|
|
|
function get_stream_suggestions(last, operators) {
|
|
|
|
if (!(last.operator === 'stream' || last.operator === 'search'
|
|
|
|
|| last.operator === '')) {
|
|
|
|
return [];
|
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 07:10:13 +02:00
|
|
|
var invalid = [
|
|
|
|
{operator: 'stream'},
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
{operator: 'pm-with'},
|
|
|
|
];
|
|
|
|
if (match_criteria(operators, invalid)) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-03 07:10:13 +02:00
|
|
|
var query = last.operand;
|
2013-08-15 21:11:07 +02:00
|
|
|
var streams = stream_data.subscribed_streams();
|
2013-07-30 23:02:10 +02:00
|
|
|
|
|
|
|
streams = _.filter(streams, function (stream) {
|
|
|
|
return stream_matches_query(stream, query);
|
|
|
|
});
|
|
|
|
|
|
|
|
streams = typeahead_helper.sorter(query, streams);
|
|
|
|
|
|
|
|
var objs = _.map(streams, function (stream) {
|
2017-06-06 02:28:12 +02:00
|
|
|
var prefix = 'stream';
|
2017-06-24 03:36:04 +02:00
|
|
|
var highlighted_stream = typeahead_helper.highlight_with_escaping(query, stream);
|
2013-07-30 23:02:10 +02:00
|
|
|
var description = prefix + ' ' + highlighted_stream;
|
2014-02-06 16:35:46 +01:00
|
|
|
var term = {
|
|
|
|
operator: 'stream',
|
2017-01-12 00:17:43 +01:00
|
|
|
operand: stream,
|
2014-02-06 16:35:46 +01:00
|
|
|
};
|
|
|
|
var search_string = Filter.unparse([term]);
|
2013-07-30 23:02:10 +02:00
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
function get_group_suggestions(all_persons, last, operators) {
|
2017-06-03 09:19:57 +02:00
|
|
|
if (last.operator !== 'pm-with' ) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
var invalid = [
|
|
|
|
{operator: 'stream'},
|
|
|
|
];
|
|
|
|
if (match_criteria(operators, invalid)) {
|
2017-02-05 22:11:18 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
var operand = last.operand;
|
|
|
|
var negated = last.negated;
|
2017-02-05 22:11:18 +01:00
|
|
|
|
|
|
|
// The operand has the form "part1,part2,pa", where all but the last part
|
|
|
|
// are emails, and the last part is an arbitrary query.
|
|
|
|
//
|
|
|
|
// We only generate group suggestions when there's more than one part, and
|
|
|
|
// we only use the last part to generate suggestions.
|
|
|
|
var all_but_last_part;
|
|
|
|
var last_part;
|
|
|
|
|
|
|
|
var last_comma_index = operand.lastIndexOf(',');
|
|
|
|
if (last_comma_index < 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither all_but_last_part nor last_part include the final comma.
|
|
|
|
all_but_last_part = operand.slice(0, last_comma_index);
|
|
|
|
last_part = operand.slice(last_comma_index + 1);
|
|
|
|
|
|
|
|
// We don't suggest a person if their email is already present in the
|
|
|
|
// operand (not including the last part).
|
2017-07-04 22:14:42 +02:00
|
|
|
var parts = all_but_last_part.split(',').concat(people.my_current_email());
|
2017-06-23 08:39:37 +02:00
|
|
|
var persons = _.filter(all_persons, function (person) {
|
2017-02-05 22:11:18 +01:00
|
|
|
if (_.contains(parts, person.email)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-23 08:39:37 +02:00
|
|
|
return (last_part === '') || people.person_matches_query(person, last_part);
|
2017-02-05 22:11:18 +01:00
|
|
|
});
|
|
|
|
|
2017-07-05 02:58:05 +02:00
|
|
|
persons.sort(compare_by_huddle(parts));
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
// Take top 15 persons, since they're ordered by pm_recipient_count.
|
|
|
|
persons = persons.slice(0, 15);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
|
|
|
var prefix = Filter.operator_to_prefix('pm-with', negated);
|
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
var suggestions = _.map(persons, function (person) {
|
2017-02-05 22:11:18 +01:00
|
|
|
var term = {
|
|
|
|
operator: 'pm-with',
|
|
|
|
operand: all_but_last_part + ',' + person.email,
|
|
|
|
negated: negated,
|
|
|
|
};
|
|
|
|
var name = highlight_person(last_part, person);
|
|
|
|
var description = prefix + ' ' + all_but_last_part + ',' + name;
|
|
|
|
var terms = [term];
|
|
|
|
if (negated) {
|
|
|
|
terms = [{operator: 'is', operand: 'private'}, term];
|
|
|
|
}
|
|
|
|
var search_string = Filter.unparse(terms);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
// Possible args for autocomplete_operator: pm-with, sender, from
|
2017-06-23 08:39:37 +02:00
|
|
|
function get_person_suggestions(all_persons, last, operators, autocomplete_operator) {
|
2017-06-03 09:19:57 +02:00
|
|
|
if (last.operator === "is" && last.operand === "private") {
|
|
|
|
// Interpret 'is:private' as equivalent to 'pm-with:'
|
|
|
|
last = {operator: "pm-with", operand: "", negated: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = last.operand;
|
|
|
|
|
|
|
|
// Only accept queries who match the specified operator, or no operator (search)
|
|
|
|
if (!(last.operator === 'search' || last.operator === autocomplete_operator)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be especially strict about the less common "from" operator.
|
|
|
|
if (autocomplete_operator === 'from' && last.operator !== 'from') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalid;
|
|
|
|
if (autocomplete_operator === 'pm-with') {
|
|
|
|
invalid = [{operator: 'pm-with'}, {operator: 'stream'}];
|
|
|
|
} else {
|
|
|
|
// If not pm-with, then this must either be 'sender' or 'from'
|
|
|
|
invalid = [{operator: 'sender'}, {operator: 'from'}];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match_criteria(operators, invalid)) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
var persons = _.filter(all_persons, function (person) {
|
|
|
|
return people.person_matches_query(person, query);
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
persons.sort(typeahead_helper.compare_by_pms);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
var prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated);
|
2013-10-07 20:48:25 +02:00
|
|
|
|
2017-06-23 08:39:37 +02:00
|
|
|
var objs = _.map(persons, function (person) {
|
2013-07-30 23:02:10 +02:00
|
|
|
var name = highlight_person(query, person);
|
|
|
|
var description = prefix + ' ' + name;
|
2017-06-03 09:19:57 +02:00
|
|
|
var terms = [{
|
|
|
|
operator: autocomplete_operator,
|
|
|
|
operand: person.email,
|
|
|
|
negated: last.negated,
|
|
|
|
}];
|
|
|
|
if (autocomplete_operator === 'pm-with' && last.negated) {
|
|
|
|
// In the special case of '-pm-with', add 'is:private' before it
|
|
|
|
// because we assume the user still wants to narrow to PMs
|
|
|
|
terms.unshift({operator: 'is', operand: 'private'});
|
|
|
|
}
|
|
|
|
var search_string = Filter.unparse(terms);
|
2013-07-30 23:02:10 +02:00
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
2013-08-06 19:08:39 +02:00
|
|
|
function get_default_suggestion(operators) {
|
|
|
|
// Here we return the canonical suggestion for the full query that the
|
|
|
|
// user typed. (The caller passes us the parsed query as "operators".)
|
2017-06-03 04:32:25 +02:00
|
|
|
if (operators.length === 0) {
|
|
|
|
return {description: '', search_string: ''};
|
|
|
|
}
|
2013-08-22 01:29:28 +02:00
|
|
|
var search_string = Filter.unparse(operators);
|
2014-02-13 20:59:15 +01:00
|
|
|
var description = Filter.describe(operators);
|
2013-07-30 23:02:10 +02:00
|
|
|
description = Handlebars.Utils.escapeExpression(description);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
}
|
|
|
|
|
2017-06-03 22:54:35 +02:00
|
|
|
function get_topic_suggestions(last, operators) {
|
|
|
|
if (last.operator === '') {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-03 22:54:35 +02:00
|
|
|
var invalid = [
|
|
|
|
{operator: 'pm-with'},
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
{operator: 'topic'},
|
|
|
|
];
|
|
|
|
|
|
|
|
if (match_criteria(operators, invalid)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var operator = Filter.canonicalize_operator(last.operator);
|
|
|
|
var operand = last.operand;
|
|
|
|
var negated = (operator === 'topic') && (last.negated);
|
2013-07-30 23:02:10 +02:00
|
|
|
var stream;
|
|
|
|
var guess;
|
2017-06-03 22:54:35 +02:00
|
|
|
var filter = new Filter(operators);
|
|
|
|
var suggest_operators = [];
|
2013-07-30 23:02:10 +02:00
|
|
|
|
|
|
|
// stream:Rome -> show all Rome topics
|
|
|
|
// stream:Rome topic: -> show all Rome topics
|
|
|
|
// stream:Rome f -> show all Rome topics with a word starting in f
|
|
|
|
// stream:Rome topic:f -> show all Rome topics with a word starting in f
|
|
|
|
// stream:Rome topic:f -> show all Rome topics with a word starting in f
|
|
|
|
|
|
|
|
// When narrowed to a stream:
|
|
|
|
// topic: -> show all topics in current stream
|
|
|
|
// foo -> show all topics in current stream with words starting with foo
|
|
|
|
|
|
|
|
// If somebody explicitly types search:, then we might
|
|
|
|
// not want to suggest topics, but I feel this is a very
|
2013-08-22 01:29:28 +02:00
|
|
|
// minor issue, and Filter.parse() is currently lossy
|
2013-07-30 23:02:10 +02:00
|
|
|
// in terms of telling us whether they provided the operator,
|
2014-02-10 20:53:38 +01:00
|
|
|
// i.e. "foo" and "search:foo" both become [{operator: 'search', operand: 'foo'}].
|
2013-07-30 23:02:10 +02:00
|
|
|
switch (operator) {
|
|
|
|
case 'stream':
|
|
|
|
guess = '';
|
|
|
|
stream = operand;
|
2017-06-03 22:54:35 +02:00
|
|
|
suggest_operators.push(last);
|
2013-07-30 23:02:10 +02:00
|
|
|
break;
|
|
|
|
case 'topic':
|
|
|
|
case 'search':
|
|
|
|
guess = operand;
|
|
|
|
if (filter.has_operator('stream')) {
|
|
|
|
stream = filter.operands('stream')[0];
|
|
|
|
} else {
|
2017-04-25 15:25:31 +02:00
|
|
|
stream = narrow_state.stream();
|
2017-06-03 22:54:35 +02:00
|
|
|
suggest_operators.push({operator: 'stream', operand: stream});
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-24 15:15:28 +02:00
|
|
|
var stream_id = stream_data.get_stream_id(stream);
|
|
|
|
if (!stream_id) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-07-24 18:22:37 +02:00
|
|
|
var topics = topic_data.get_recent_names(stream_id);
|
2013-08-20 16:47:27 +02:00
|
|
|
|
2017-07-24 15:15:28 +02:00
|
|
|
if (!topics || !topics.length) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2016-10-28 18:47:57 +02:00
|
|
|
// Be defensive here in case stream_data.get_recent_topics gets
|
|
|
|
// super huge, but still slice off enough topics to find matches.
|
2013-08-08 15:36:57 +02:00
|
|
|
topics = topics.slice(0, 300);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
|
|
|
if (guess !== '') {
|
|
|
|
topics = _.filter(topics, function (topic) {
|
|
|
|
return phrase_match(topic, guess);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-25 20:26:46 +01:00
|
|
|
topics = topics.slice(0, 10);
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
// Just use alphabetical order. While recency and read/unreadness of
|
2016-10-28 19:07:25 +02:00
|
|
|
// topics do matter in some contexts, you can get that from the left sidebar,
|
2013-07-30 23:02:10 +02:00
|
|
|
// and I'm leaning toward high scannability for autocompletion. I also don't
|
|
|
|
// care about case.
|
|
|
|
topics.sort();
|
|
|
|
|
|
|
|
return _.map(topics, function (topic) {
|
2014-03-04 22:52:13 +01:00
|
|
|
var topic_term = {operator: 'topic', operand: topic, negated: negated};
|
2017-06-03 22:54:35 +02:00
|
|
|
var operators = suggest_operators.concat([topic_term]);
|
2013-08-22 01:29:28 +02:00
|
|
|
var search_string = Filter.unparse(operators);
|
2014-02-13 20:59:15 +01:00
|
|
|
var description = Filter.describe(operators);
|
2013-07-30 23:02:10 +02:00
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function get_operator_subset_suggestions(operators) {
|
2013-07-30 23:02:10 +02:00
|
|
|
// For stream:a topic:b search:c, suggest:
|
|
|
|
// stream:a topic:b
|
|
|
|
// stream:a
|
|
|
|
if (operators.length < 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var i;
|
|
|
|
var suggestions = [];
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = operators.length - 1; i >= 1; i -= 1) {
|
2013-07-30 23:02:10 +02:00
|
|
|
var subset = operators.slice(0, i);
|
2013-08-22 01:29:28 +02:00
|
|
|
var search_string = Filter.unparse(subset);
|
2014-02-13 20:59:15 +01:00
|
|
|
var description = Filter.describe(subset);
|
2013-07-30 23:02:10 +02:00
|
|
|
var suggestion = {description: description, search_string: search_string};
|
|
|
|
suggestions.push(suggestion);
|
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function get_special_filter_suggestions(last, operators) {
|
2013-07-30 23:02:10 +02:00
|
|
|
var suggestions = [
|
|
|
|
{
|
|
|
|
search_string: 'is:private',
|
2017-06-06 02:28:12 +02:00
|
|
|
description: 'private messages',
|
2017-06-03 04:32:25 +02:00
|
|
|
invalid: [
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
{operator: 'stream'},
|
|
|
|
{operator: 'pm-with'},
|
|
|
|
{operator: 'in'},
|
|
|
|
],
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'is:starred',
|
2017-06-06 02:28:12 +02:00
|
|
|
description: 'starred messages',
|
2017-06-03 04:32:25 +02:00
|
|
|
invalid: [
|
|
|
|
{operator: 'is', operand: 'starred'},
|
|
|
|
],
|
2013-07-30 23:02:10 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'is:mentioned',
|
2017-01-12 00:17:43 +01:00
|
|
|
description: '@-mentions',
|
2017-06-03 04:32:25 +02:00
|
|
|
invalid: [
|
|
|
|
{operator: 'is', operand: 'mentioned'},
|
|
|
|
],
|
2013-07-30 23:02:10 +02:00
|
|
|
},
|
2013-08-30 21:15:01 +02:00
|
|
|
{
|
|
|
|
search_string: 'is:alerted',
|
2017-06-06 02:28:12 +02:00
|
|
|
description: 'alerted messages',
|
2017-06-03 04:32:25 +02:00
|
|
|
invalid: [
|
|
|
|
{operator: 'is', operand: 'alerted'},
|
|
|
|
],
|
2013-08-30 21:15:01 +02:00
|
|
|
},
|
2017-06-19 00:33:24 +02:00
|
|
|
{
|
|
|
|
search_string: 'is:unread',
|
|
|
|
description: 'unread messages',
|
|
|
|
invalid: [
|
|
|
|
{operator: 'is', operand: 'unread'},
|
|
|
|
],
|
|
|
|
},
|
2013-07-30 23:02:10 +02:00
|
|
|
];
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
var last_string = Filter.unparse([last]).toLowerCase();
|
2013-07-30 23:02:10 +02:00
|
|
|
suggestions = _.filter(suggestions, function (s) {
|
2017-06-03 04:32:25 +02:00
|
|
|
if (match_criteria(operators, s.invalid)) {
|
|
|
|
return false;
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
2017-06-03 04:32:25 +02:00
|
|
|
if (last_string === '') {
|
2013-07-30 23:02:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-03 04:32:25 +02:00
|
|
|
return (s.search_string.toLowerCase().indexOf(last_string) === 0) ||
|
|
|
|
(s.description.toLowerCase().indexOf(last_string) === 0);
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
// Only show home if there's an empty bar
|
|
|
|
if (operators.length === 0 && last_string === '') {
|
|
|
|
suggestions.unshift({search_string: '', description: 'Home'});
|
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2017-06-03 05:33:02 +02:00
|
|
|
function get_sent_by_me_suggestions(last, operators) {
|
|
|
|
var last_string = Filter.unparse([last]).toLowerCase();
|
2017-01-20 19:22:25 +01:00
|
|
|
var sender_query = 'sender:' + people.my_current_email();
|
|
|
|
var from_query = 'from:' + people.my_current_email();
|
2017-06-06 02:28:12 +02:00
|
|
|
var description = 'sent by me';
|
2017-06-03 05:33:02 +02:00
|
|
|
var invalid = [
|
|
|
|
{operator: 'sender'},
|
|
|
|
{operator: 'from'},
|
|
|
|
];
|
2017-01-11 19:20:31 +01:00
|
|
|
|
2017-06-03 05:33:02 +02:00
|
|
|
if (match_criteria(operators, invalid)) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [];
|
2017-06-03 05:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (last.operator === '' || sender_query.indexOf(last_string) === 0 ||
|
|
|
|
'sender:me'.indexOf(last_string) === 0 || last_string === 'sent') {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
search_string: sender_query,
|
|
|
|
description: description,
|
|
|
|
},
|
|
|
|
];
|
2017-06-03 05:33:02 +02:00
|
|
|
} else if (from_query.indexOf(last_string) === 0 || 'sender:me'.indexOf(last_string) === 0) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
search_string: from_query,
|
|
|
|
description: description,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-05 09:54:56 +02:00
|
|
|
function get_containing_suggestions(last) {
|
|
|
|
if (!(last.operator === 'search' || last.operator === 'has')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var choices = ['link', 'image', 'attachment'];
|
|
|
|
choices = _.filter(choices, function (choice) {
|
|
|
|
return phrase_match(choice, last.operand);
|
|
|
|
});
|
|
|
|
|
|
|
|
return _.map(choices, function (choice) {
|
2017-06-15 09:10:52 +02:00
|
|
|
var op = [{operator: 'has', operand: choice, negated: last.negated}];
|
2017-06-05 09:54:56 +02:00
|
|
|
var search_string = Filter.unparse(op);
|
|
|
|
var description = Filter.describe(op);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:07:12 +02:00
|
|
|
function get_operator_suggestions(last) {
|
|
|
|
if (!(last.operator === 'search')) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var negated = false;
|
|
|
|
if (last.operand.indexOf("-") === 0) {
|
|
|
|
negated = true;
|
|
|
|
last.operand = last.operand.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var choices = ['stream', 'topic', 'pm-with', 'sender', 'near', 'has', 'from'];
|
|
|
|
choices = _.filter(choices, function (choice) {
|
|
|
|
return phrase_match(choice, last.operand);
|
|
|
|
});
|
|
|
|
|
|
|
|
return _.map(choices, function (choice) {
|
|
|
|
var op = [{operator: choice, operand: '', negated: negated}];
|
|
|
|
var search_string = Filter.unparse(op);
|
|
|
|
var description = Filter.describe(op);
|
|
|
|
return {description: description, search_string: search_string};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function attach_suggestions(result, base, suggestions) {
|
|
|
|
_.each(suggestions, function (suggestion) {
|
|
|
|
if (base.description.length > 0) {
|
|
|
|
suggestion.search_string = base.search_string + " " + suggestion.search_string;
|
|
|
|
suggestion.description = base.description + ", " + suggestion.description;
|
|
|
|
}
|
|
|
|
result.push(suggestion);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
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
|
|
|
|
// the tests. Its API is partly shaped by the typeahead library, which wants
|
|
|
|
// us to give it strings only, but we also need to return our caller a hash
|
|
|
|
// with information for subsequent callbacks.
|
|
|
|
var result = [];
|
|
|
|
var suggestion;
|
2017-06-03 04:32:25 +02:00
|
|
|
var base; //base, default suggestion
|
2013-07-30 23:02:10 +02:00
|
|
|
var suggestions;
|
|
|
|
|
|
|
|
// Add an entry for narrow by operators.
|
2013-08-22 01:29:28 +02:00
|
|
|
var operators = Filter.parse(query);
|
2017-06-03 04:32:25 +02:00
|
|
|
var last = {operator: '', operand: '', negated: false};
|
|
|
|
if (operators.length > 0) {
|
|
|
|
last = operators.slice(-1)[0];
|
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
// Display the default first
|
|
|
|
if (last.operator !== '') {
|
|
|
|
suggestion = get_default_suggestion(operators);
|
|
|
|
result = [suggestion];
|
|
|
|
}
|
|
|
|
|
|
|
|
var base_operators = [];
|
|
|
|
if (operators.length > 1) {
|
|
|
|
base_operators = operators.slice(0, -1);
|
|
|
|
}
|
|
|
|
base = get_default_suggestion(base_operators);
|
|
|
|
|
|
|
|
// Get all individual suggestions, and then attach_suggestions
|
|
|
|
// mutates the list 'result' to add a properly-formatted suggestion
|
|
|
|
suggestions = get_special_filter_suggestions(last, base_operators);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 05:33:02 +02:00
|
|
|
suggestions = get_sent_by_me_suggestions(last, base_operators);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2017-01-11 19:20:31 +01:00
|
|
|
|
2017-06-03 07:10:13 +02:00
|
|
|
suggestions = get_stream_suggestions(last, base_operators);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2016-11-01 19:37:19 +01:00
|
|
|
var persons = people.get_all_persons();
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
suggestions = get_person_suggestions(persons, last, base_operators, 'pm-with');
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
suggestions = get_person_suggestions(persons, last, base_operators, 'sender');
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
suggestions = get_person_suggestions(persons, last, base_operators, 'from');
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
suggestions = get_group_suggestions(persons, last, base_operators);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 22:54:35 +02:00
|
|
|
suggestions = get_topic_suggestions(last, base_operators);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-15 09:07:12 +02:00
|
|
|
suggestions = get_operator_suggestions(last);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
|
|
|
|
2017-06-05 09:54:56 +02:00
|
|
|
suggestions = get_containing_suggestions(last);
|
|
|
|
attach_suggestions(result, base, suggestions);
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
suggestions = get_operator_subset_suggestions(operators);
|
2013-07-30 23:02:10 +02:00
|
|
|
result = result.concat(suggestions);
|
|
|
|
|
2017-06-06 02:28:12 +02:00
|
|
|
_.each(result, function (sug) {
|
|
|
|
var first = sug.description.charAt(0).toUpperCase();
|
|
|
|
sug.description = first + sug.description.slice(1);
|
|
|
|
});
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
// Typeahead expects us to give it strings, not objects, so we maintain our own hash
|
|
|
|
// back to our objects, and we also filter duplicates here.
|
|
|
|
var lookup_table = {};
|
|
|
|
var unique_suggestions = [];
|
|
|
|
_.each(result, function (obj) {
|
|
|
|
if (!lookup_table[obj.search_string]) {
|
|
|
|
lookup_table[obj.search_string] = obj;
|
|
|
|
unique_suggestions.push(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var strings = _.map(unique_suggestions, function (obj) {
|
|
|
|
return obj.search_string;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
strings: strings,
|
2017-01-12 00:17:43 +01:00
|
|
|
lookup_table: lookup_table,
|
2013-07-30 23:02:10 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = search_suggestion;
|
|
|
|
}
|