2019-12-25 16:58:11 +01:00
|
|
|
exports.max_num_of_search_results = 12;
|
|
|
|
|
2013-07-30 23:02:10 +02:00
|
|
|
function stream_matches_query(stream_name, q) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return common.phrase_match(q, stream_name);
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2019-12-24 15:31:52 +01:00
|
|
|
function make_person_highlighter(query) {
|
|
|
|
const hilite = typeahead_helper.make_query_highlighter(query);
|
|
|
|
|
|
|
|
return function (person) {
|
|
|
|
if (settings_org.show_email()) {
|
|
|
|
return hilite(person.full_name) + " <" + hilite(person.email) + ">";
|
|
|
|
}
|
|
|
|
return hilite(person.full_name);
|
|
|
|
};
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
function match_criteria(operators, criteria) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const filter = new Filter(operators);
|
2017-06-03 04:32:25 +02:00
|
|
|
return _.any(criteria, function (cr) {
|
|
|
|
if (_.has(cr, 'operand')) {
|
|
|
|
return filter.has_operand(cr.operator, cr.operand);
|
|
|
|
}
|
|
|
|
return filter.has_operator(cr.operator);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:00:00 +02:00
|
|
|
function check_validity(last, operators, valid, invalid) {
|
|
|
|
// valid: list of strings valid for the last operator
|
|
|
|
// invalid: list of operators invalid for any previous operators except last.
|
|
|
|
if (valid.indexOf(last.operator) === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (match_criteria(operators, invalid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:59:17 +02:00
|
|
|
function format_as_suggestion(terms) {
|
|
|
|
return {
|
|
|
|
description: Filter.describe(terms),
|
|
|
|
search_string: Filter.unparse(terms),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
2019-11-02 00:06:25 +01:00
|
|
|
const huddles = activity.get_huddles();
|
|
|
|
const huddle_dict = {};
|
|
|
|
for (let i = 0; i < huddles.length; i += 1) {
|
2017-07-05 02:58:05 +02:00
|
|
|
huddle_dict[huddles[i]] = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (person1, person2) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const huddle1 = huddle.concat(person1.user_id).sort().join(',');
|
|
|
|
const huddle2 = huddle.concat(person2.user_id).sort().join(',');
|
2017-07-05 02:58:05 +02:00
|
|
|
|
|
|
|
// If not in the dict, assign an arbitrarily high index
|
2019-11-02 00:06:25 +01:00
|
|
|
const score1 = huddle_dict[huddle1] || 100;
|
|
|
|
const score2 = huddle_dict[huddle2] || 100;
|
|
|
|
const diff = score1 - score2;
|
2017-07-05 02:58:05 +02:00
|
|
|
|
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const valid = ['stream', 'search', ''];
|
|
|
|
const invalid = [
|
2017-06-03 07:10:13 +02:00
|
|
|
{operator: 'stream'},
|
2019-08-13 20:20:36 +02:00
|
|
|
{operator: 'streams'},
|
2017-06-03 07:10:13 +02:00
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
{operator: 'pm-with'},
|
|
|
|
];
|
2018-06-01 15:00:00 +02:00
|
|
|
if (!check_validity(last, operators, valid, invalid)) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const query = last.operand;
|
|
|
|
let 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);
|
|
|
|
|
2019-12-25 14:43:48 +01:00
|
|
|
const regex = typeahead_helper.build_highlight_regex(query);
|
|
|
|
const hilite = typeahead_helper.highlight_with_escaping_and_regex;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const objs = _.map(streams, function (stream) {
|
|
|
|
const prefix = 'stream';
|
2019-12-25 14:43:48 +01:00
|
|
|
const highlighted_stream = hilite(regex, stream);
|
2019-11-02 00:06:25 +01:00
|
|
|
const verb = last.negated ? 'exclude ' : '';
|
|
|
|
const description = verb + prefix + ' ' + highlighted_stream;
|
|
|
|
const term = {
|
2014-02-06 16:35:46 +01:00
|
|
|
operator: 'stream',
|
2017-01-12 00:17:43 +01:00
|
|
|
operand: stream,
|
2018-05-21 06:23:08 +02:00
|
|
|
negated: last.negated,
|
2014-02-06 16:35:46 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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) {
|
2018-06-01 15:00:00 +02:00
|
|
|
if (!check_validity(last, operators, ['pm-with'], [{operator: 'stream'}])) {
|
2017-02-05 22:11:18 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const operand = last.operand;
|
|
|
|
const 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.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_comma_index = operand.lastIndexOf(',');
|
2017-02-05 22:11:18 +01:00
|
|
|
if (last_comma_index < 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither all_but_last_part nor last_part include the final comma.
|
2019-10-26 00:11:05 +02:00
|
|
|
const all_but_last_part = operand.slice(0, last_comma_index);
|
|
|
|
const last_part = operand.slice(last_comma_index + 1);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
|
|
|
// We don't suggest a person if their email is already present in the
|
|
|
|
// operand (not including the last part).
|
2019-11-02 00:06:25 +01:00
|
|
|
const parts = all_but_last_part.split(',').concat(people.my_current_email());
|
|
|
|
let persons = _.filter(all_persons, function (person) {
|
2017-02-05 22:11:18 +01:00
|
|
|
if (_.contains(parts, person.email)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-06 18:50:09 +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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const prefix = Filter.operator_to_prefix('pm-with', negated);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2019-12-24 15:31:52 +01:00
|
|
|
const highlight_person = make_person_highlighter(last_part);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = _.map(persons, function (person) {
|
|
|
|
const term = {
|
2017-02-05 22:11:18 +01:00
|
|
|
operator: 'pm-with',
|
|
|
|
operand: all_but_last_part + ',' + person.email,
|
|
|
|
negated: negated,
|
|
|
|
};
|
2019-12-24 15:31:52 +01:00
|
|
|
const name = highlight_person(person);
|
2019-11-02 00:06:25 +01:00
|
|
|
const description = prefix + ' ' + Handlebars.Utils.escapeExpression(all_but_last_part) + ',' + name;
|
|
|
|
let terms = [term];
|
2017-02-05 22:11:18 +01:00
|
|
|
if (negated) {
|
|
|
|
terms = [{operator: 'is', operand: 'private'}, term];
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_string = Filter.unparse(terms);
|
2017-02-05 22:11:18 +01:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const query = last.operand;
|
2017-06-03 09:19:57 +02:00
|
|
|
|
|
|
|
// Be especially strict about the less common "from" operator.
|
|
|
|
if (autocomplete_operator === 'from' && last.operator !== 'from') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const valid = ['search', autocomplete_operator];
|
|
|
|
let invalid;
|
2017-06-03 09:19:57 +02:00
|
|
|
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'}];
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:00:00 +02:00
|
|
|
if (!check_validity(last, operators, valid, invalid)) {
|
2013-07-30 23:02:10 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const persons = _.filter(all_persons, function (person) {
|
2017-06-23 08:39:37 +02:00
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const prefix = Filter.operator_to_prefix(autocomplete_operator, last.negated);
|
2013-10-07 20:48:25 +02:00
|
|
|
|
2019-12-24 15:31:52 +01:00
|
|
|
const highlight_person = make_person_highlighter(query);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const objs = _.map(persons, function (person) {
|
2019-12-24 15:31:52 +01:00
|
|
|
const name = highlight_person(person);
|
2019-11-02 00:06:25 +01:00
|
|
|
const description = prefix + ' ' + name;
|
|
|
|
const terms = [{
|
2017-06-03 09:19:57 +02:00
|
|
|
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'});
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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) {
|
2018-07-02 22:30:18 +02:00
|
|
|
// Here we return the canonical suggestion for the last query that the
|
|
|
|
// user typed.
|
|
|
|
if (operators !== undefined && operators.length > 0) {
|
|
|
|
return format_as_suggestion(operators);
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
2018-07-02 22:30:18 +02:00
|
|
|
return false;
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 13:08:34 +02:00
|
|
|
function get_default_suggestion_legacy(operators) {
|
|
|
|
// Here we return the canonical suggestion for the full query that the
|
|
|
|
// user typed. (The caller passes us the parsed query as "operators".)
|
|
|
|
if (operators.length === 0) {
|
|
|
|
return {description: '', search_string: ''};
|
|
|
|
}
|
|
|
|
return format_as_suggestion(operators);
|
|
|
|
}
|
|
|
|
|
2017-06-03 22:54:35 +02:00
|
|
|
function get_topic_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const invalid = [
|
2017-06-03 22:54:35 +02:00
|
|
|
{operator: 'pm-with'},
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
{operator: 'topic'},
|
|
|
|
];
|
2018-06-01 15:00:00 +02:00
|
|
|
if (!check_validity(last, operators, ['stream', 'topic', 'search'], invalid)) {
|
2017-06-03 22:54:35 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const operator = Filter.canonicalize_operator(last.operator);
|
|
|
|
const operand = last.operand;
|
|
|
|
const negated = operator === 'topic' && last.negated;
|
|
|
|
let stream;
|
|
|
|
let guess;
|
|
|
|
const filter = new Filter(operators);
|
|
|
|
const 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = stream_data.get_stream_id(stream);
|
2017-07-24 15:15:28 +02:00
|
|
|
if (!stream_id) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let 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) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return common.phrase_match(guess, topic);
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_term = {operator: 'topic', operand: topic, negated: negated};
|
|
|
|
const operators = suggest_operators.concat([topic_term]);
|
2018-06-01 15:59:17 +02:00
|
|
|
return format_as_suggestion(operators);
|
2013-07-30 23:02:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
|
|
|
const suggestions = [];
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = operators.length - 1; i >= 1; i -= 1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const subset = operators.slice(0, i);
|
2018-06-01 15:59:17 +02:00
|
|
|
suggestions.push(format_as_suggestion(subset));
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
function get_special_filter_suggestions(last, operators, suggestions) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const is_search_operand_negated = last.operator === 'search' && last.operand[0] === '-';
|
2018-05-20 16:30:06 +02:00
|
|
|
// Negating suggestions on is_search_operand_negated is required for
|
|
|
|
// suggesting negated operators.
|
|
|
|
if (last.negated || is_search_operand_negated) {
|
|
|
|
suggestions = _.map(suggestions, function (suggestion) {
|
|
|
|
return {
|
|
|
|
search_string: '-' + suggestion.search_string,
|
|
|
|
description: 'exclude ' + suggestion.description,
|
|
|
|
invalid: suggestion.invalid,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2018-05-18 10:10:49 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_string = Filter.unparse([last]).toLowerCase();
|
2018-05-18 10:10:49 +02:00
|
|
|
suggestions = _.filter(suggestions, function (s) {
|
|
|
|
if (match_criteria(operators, s.invalid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (last_string === '') {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-20 16:30:06 +02:00
|
|
|
|
|
|
|
// returns the substring after the ":" symbol.
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestion_operand = s.search_string.substring(s.search_string.indexOf(":") + 1);
|
2018-05-20 16:30:06 +02:00
|
|
|
// e.g for `att` search query, `has:attachment` should be suggested.
|
2019-11-02 00:06:25 +01:00
|
|
|
const show_operator_suggestions = last.operator === 'search' && suggestion_operand.toLowerCase().indexOf(last_string) === 0;
|
2018-06-06 18:50:09 +02:00
|
|
|
return s.search_string.toLowerCase().indexOf(last_string) === 0 ||
|
2018-06-06 18:19:09 +02:00
|
|
|
show_operator_suggestions ||
|
2018-06-06 18:50:09 +02:00
|
|
|
s.description.toLowerCase().indexOf(last_string) === 0;
|
2018-05-18 10:10:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Only show home if there's an empty bar
|
|
|
|
if (operators.length === 0 && last_string === '') {
|
|
|
|
suggestions.unshift({search_string: '', description: 'All messages'});
|
|
|
|
}
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:20:36 +02:00
|
|
|
function get_streams_filter_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = [
|
2019-08-13 20:20:36 +02:00
|
|
|
{
|
|
|
|
search_string: 'streams:public',
|
2019-10-10 00:16:28 +02:00
|
|
|
description: 'All public streams in organization',
|
2019-08-13 20:20:36 +02:00
|
|
|
invalid: [
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
{operator: 'stream'},
|
|
|
|
{operator: 'group-pm-with'},
|
|
|
|
{operator: 'pm-with'},
|
|
|
|
{operator: 'in'},
|
|
|
|
{operator: 'streams'},
|
|
|
|
],
|
|
|
|
|
|
|
|
},
|
|
|
|
];
|
|
|
|
return get_special_filter_suggestions(last, operators, suggestions);
|
|
|
|
}
|
2018-05-18 10:10:49 +02:00
|
|
|
function get_is_filter_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = [
|
2013-07-30 23:02:10 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
];
|
2018-05-18 10:10:49 +02:00
|
|
|
return get_special_filter_suggestions(last, operators, suggestions);
|
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
function get_has_filter_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const suggestions = [
|
2018-05-18 10:10:49 +02:00
|
|
|
{
|
|
|
|
search_string: 'has:link',
|
|
|
|
description: 'messages with one or more link',
|
|
|
|
invalid: [
|
|
|
|
{operator: 'has', operand: 'link'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'has:image',
|
|
|
|
description: 'messages with one or more image',
|
|
|
|
invalid: [
|
|
|
|
{operator: 'has', operand: 'image'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
search_string: 'has:attachment',
|
|
|
|
description: 'messages with one or more attachment',
|
|
|
|
invalid: [
|
|
|
|
{operator: 'has', operand: 'attachment'},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
return get_special_filter_suggestions(last, operators, suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
}
|
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
|
2017-06-03 05:33:02 +02:00
|
|
|
function get_sent_by_me_suggestions(last, operators) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const last_string = Filter.unparse([last]).toLowerCase();
|
|
|
|
const negated = last.negated || last.operator === 'search' && last.operand[0] === '-';
|
|
|
|
const negated_symbol = negated ? '-' : '';
|
|
|
|
const verb = negated ? 'exclude ' : '';
|
|
|
|
|
|
|
|
const sender_query = negated_symbol + 'sender:' + people.my_current_email();
|
|
|
|
const from_query = negated_symbol + 'from:' + people.my_current_email();
|
|
|
|
const sender_me_query = negated_symbol + 'sender:me';
|
|
|
|
const from_me_query = negated_symbol + 'from:me';
|
|
|
|
const sent_string = negated_symbol + 'sent';
|
|
|
|
const description = verb + 'sent by me';
|
|
|
|
|
|
|
|
const invalid = [
|
2017-06-03 05:33:02 +02:00
|
|
|
{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 ||
|
2018-05-21 07:35:27 +02:00
|
|
|
sender_me_query.indexOf(last_string) === 0 || last_string === sent_string) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
search_string: sender_query,
|
|
|
|
description: description,
|
|
|
|
},
|
|
|
|
];
|
2018-05-21 07:35:27 +02:00
|
|
|
} else if (from_query.indexOf(last_string) === 0 || from_me_query.indexOf(last_string) === 0) {
|
2017-01-11 19:20:31 +01:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
search_string: from_query,
|
|
|
|
description: description,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:07:12 +02:00
|
|
|
function get_operator_suggestions(last) {
|
2018-04-06 20:43:34 +02:00
|
|
|
if (!(last.operator === 'search')) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
let last_operand = last.operand;
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let negated = false;
|
2018-05-20 16:30:06 +02:00
|
|
|
if (last_operand.indexOf("-") === 0) {
|
2018-04-06 20:43:34 +02:00
|
|
|
negated = true;
|
2018-05-20 16:30:06 +02:00
|
|
|
last_operand = last_operand.slice(1);
|
2018-04-06 20:43:34 +02:00
|
|
|
}
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let choices = ['stream', 'topic', 'pm-with', 'sender', 'near', 'from', 'group-pm-with'];
|
2018-04-06 20:43:34 +02:00
|
|
|
choices = _.filter(choices, function (choice) {
|
2018-06-25 17:14:45 +02:00
|
|
|
return common.phrase_match(last_operand, choice);
|
2018-04-06 20:43:34 +02:00
|
|
|
});
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2018-04-06 20:43:34 +02:00
|
|
|
return _.map(choices, function (choice) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const op = [{operator: choice, operand: '', negated: negated}];
|
2018-06-01 15:59:17 +02:00
|
|
|
return format_as_suggestion(op);
|
2018-04-06 20:43:34 +02:00
|
|
|
});
|
2017-06-15 09:07:12 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 18:36:16 +01:00
|
|
|
function make_attacher(base) {
|
|
|
|
const self = {};
|
|
|
|
self.result = [];
|
|
|
|
const prev = {};
|
|
|
|
|
|
|
|
function prepend_base(suggestion) {
|
2018-07-02 22:30:18 +02:00
|
|
|
if (base && base.description.length > 0) {
|
2017-06-03 04:32:25 +02:00
|
|
|
suggestion.search_string = base.search_string + " " + suggestion.search_string;
|
|
|
|
suggestion.description = base.description + ", " + suggestion.description;
|
|
|
|
}
|
2019-12-25 18:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.push = function (suggestion) {
|
|
|
|
if (!prev[suggestion.search_string]) {
|
|
|
|
prev[suggestion.search_string] = suggestion;
|
|
|
|
self.result.push(suggestion);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.concat = function (suggestions) {
|
|
|
|
_.each(suggestions, self.push);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.attach_many = function (suggestions) {
|
|
|
|
_.each(suggestions, function (suggestion) {
|
|
|
|
prepend_base(suggestion);
|
|
|
|
self.push(suggestion);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 15:36:51 +01:00
|
|
|
exports.get_search_result = function (base_query, query) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let suggestion;
|
|
|
|
let suggestions;
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2018-07-02 22:30:18 +02:00
|
|
|
// base_query_operators correspond to the existing pills. query_operators correspond
|
|
|
|
// to the operators for the query in the input. This query may contain one or more
|
|
|
|
// operators. e.g if `is:starred stream:Ver` was typed without selecting the typeahead
|
|
|
|
// or pressing enter in between i.e search pill for is:starred has not yet been added,
|
|
|
|
// then `base` should be equal to the default suggestion for `is:starred`. Thus the
|
|
|
|
// description of `is:starred` will act as a prefix in every suggestion.
|
2019-11-02 00:06:25 +01:00
|
|
|
const base_query_operators = Filter.parse(base_query);
|
|
|
|
const query_operators = Filter.parse(query);
|
|
|
|
const operators = base_query_operators.concat(query_operators);
|
|
|
|
let last = {operator: '', operand: '', negated: false};
|
2018-07-04 01:48:10 +02:00
|
|
|
if (query_operators.length > 0) {
|
|
|
|
last = query_operators.slice(-1)[0];
|
|
|
|
} else {
|
|
|
|
// If query_operators = [] then last will remain
|
|
|
|
// {operator: '', operand: '', negated: false}; from above.
|
|
|
|
// `last` has not yet been added to operators/query_operators.
|
|
|
|
// The code below adds last to operators/query_operators
|
|
|
|
operators.push(last);
|
|
|
|
query_operators.push(last);
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const person_suggestion_ops = ['sender', 'pm-with', 'from', 'group-pm'];
|
|
|
|
const operators_len = operators.length;
|
|
|
|
const query_operators_len = query_operators.length;
|
2018-05-25 17:03:13 +02:00
|
|
|
|
|
|
|
// Handle spaces in person name in new suggestions only. Checks if the last operator is
|
2018-07-02 22:30:18 +02:00
|
|
|
// 'search' and the second last operator in query_operators is one out of person_suggestion_ops.
|
2018-05-25 17:03:13 +02:00
|
|
|
// e.g for `sender:Ted sm`, initially last = {operator: 'search', operand: 'sm'....}
|
|
|
|
// and second last is {operator: 'sender', operand: 'sm'....}. If the second last operand
|
|
|
|
// is an email of a user, both of these operators remain unchanged. Otherwise search operator
|
|
|
|
// will be deleted and new last will become {operator:'sender', operand: 'Ted sm`....}.
|
2018-07-02 22:30:18 +02:00
|
|
|
if (query_operators_len > 1 &&
|
2018-05-25 17:03:13 +02:00
|
|
|
last.operator === 'search' &&
|
2018-07-02 22:30:18 +02:00
|
|
|
person_suggestion_ops.indexOf(query_operators[query_operators_len - 2].operator) !== -1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person_op = query_operators[query_operators_len - 2];
|
2019-06-27 19:16:55 +02:00
|
|
|
if (!people.reply_to_to_user_ids_string(person_op.operand)) {
|
2018-05-25 17:03:13 +02:00
|
|
|
last = {
|
|
|
|
operator: person_op.operator,
|
|
|
|
operand: person_op.operand + ' ' + last.operand,
|
|
|
|
negated: person_op.negated,
|
|
|
|
};
|
|
|
|
operators[operators_len - 2] = last;
|
|
|
|
operators.splice(-1, 1);
|
2018-07-02 22:30:18 +02:00
|
|
|
query_operators[query_operators_len - 2] = last;
|
|
|
|
query_operators.splice(-1, 1);
|
2018-05-25 17:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 18:36:16 +01:00
|
|
|
const base = get_default_suggestion(query_operators.slice(0, -1));
|
|
|
|
const attacher = make_attacher(base);
|
|
|
|
const attach = attacher.attach_many;
|
|
|
|
|
2017-06-03 04:32:25 +02:00
|
|
|
// Display the default first
|
2018-05-22 14:37:51 +02:00
|
|
|
// `has` and `is` operators work only on predefined categories. Default suggestion
|
2018-05-18 06:33:13 +02:00
|
|
|
// is not displayed in that case. e.g. `messages with one or more abc` as
|
|
|
|
// a suggestion for `has:abc`does not make sense.
|
2018-05-22 14:37:51 +02:00
|
|
|
if (last.operator !== '' && last.operator !== 'has' && last.operator !== 'is') {
|
2018-07-02 22:30:18 +02:00
|
|
|
suggestion = get_default_suggestion(query_operators);
|
|
|
|
if (suggestion) {
|
2019-12-25 18:36:16 +01:00
|
|
|
attacher.push(suggestion);
|
2018-07-02 22:30:18 +02:00
|
|
|
}
|
2017-06-03 04:32:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let base_operators = [];
|
2017-06-03 04:32:25 +02:00
|
|
|
if (operators.length > 1) {
|
|
|
|
base_operators = operators.slice(0, -1);
|
|
|
|
}
|
2018-07-02 22:30:18 +02:00
|
|
|
|
2019-08-13 20:20:36 +02:00
|
|
|
suggestions = get_streams_filter_suggestions(last, base_operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2019-08-13 20:20:36 +02:00
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
suggestions = get_is_filter_suggestions(last, base_operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(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);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2017-01-11 19:20:31 +01:00
|
|
|
|
2017-06-03 07:10:13 +02:00
|
|
|
suggestions = get_stream_suggestions(last, base_operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const persons = people.get_all_persons();
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2018-05-21 07:46:25 +02:00
|
|
|
suggestions = get_person_suggestions(persons, last, base_operators, 'sender');
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2018-05-21 07:46:25 +02:00
|
|
|
suggestions = get_person_suggestions(persons, last, base_operators, 'pm-with');
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(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');
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2017-02-05 22:11:18 +01:00
|
|
|
|
2017-06-04 01:51:53 +02:00
|
|
|
suggestions = get_person_suggestions(persons, last, base_operators, 'group-pm-with');
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2017-06-04 01:51:53 +02:00
|
|
|
|
2017-06-03 09:19:57 +02:00
|
|
|
suggestions = get_group_suggestions(persons, last, base_operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-03 22:54:35 +02:00
|
|
|
suggestions = get_topic_suggestions(last, base_operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2017-06-15 09:07:12 +02:00
|
|
|
suggestions = get_operator_suggestions(last);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2017-06-15 09:07:12 +02:00
|
|
|
|
2018-05-18 10:10:49 +02:00
|
|
|
suggestions = get_has_filter_suggestions(last, base_operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attach(suggestions);
|
2017-06-05 09:54:56 +02:00
|
|
|
|
2019-12-25 18:36:16 +01:00
|
|
|
attacher.concat(suggestions);
|
|
|
|
return attacher.result;
|
2013-07-30 23:02:10 +02:00
|
|
|
};
|
|
|
|
|
2019-12-25 15:36:51 +01:00
|
|
|
exports.get_search_result_legacy = function (query) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let suggestion;
|
2018-07-14 13:01:21 +02:00
|
|
|
|
|
|
|
// Add an entry for narrow by operators.
|
2019-11-02 00:06:25 +01:00
|
|
|
const operators = Filter.parse(query);
|
|
|
|
let last = {operator: '', operand: '', negated: false};
|
2018-07-14 13:01:21 +02:00
|
|
|
if (operators.length > 0) {
|
|
|
|
last = operators.slice(-1)[0];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const person_suggestion_ops = ['sender', 'pm-with', 'from', 'group-pm'];
|
|
|
|
const operators_len = operators.length;
|
2018-07-14 13:01:21 +02:00
|
|
|
|
|
|
|
// Handle spaces in person name in new suggestions only. Checks if the last operator is
|
|
|
|
// 'search' and the second last operator is one out of person_suggestion_ops.
|
|
|
|
// e.g for `sender:Ted sm`, initially last = {operator: 'search', operand: 'sm'....}
|
|
|
|
// and second last is {operator: 'sender', operand: 'sm'....}. If the second last operand
|
|
|
|
// is an email of a user, both of these operators remain unchanged. Otherwise search operator
|
|
|
|
// will be deleted and new last will become {operator:'sender', operand: 'Ted sm`....}.
|
|
|
|
if (operators_len > 1 &&
|
|
|
|
last.operator === 'search' &&
|
|
|
|
person_suggestion_ops.indexOf(operators[operators_len - 2].operator) !== -1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person_op = operators[operators_len - 2];
|
2019-06-27 19:16:55 +02:00
|
|
|
if (!people.reply_to_to_user_ids_string(person_op.operand)) {
|
2018-07-14 13:01:21 +02:00
|
|
|
last = {
|
|
|
|
operator: person_op.operator,
|
|
|
|
operand: person_op.operand + ' ' + last.operand,
|
|
|
|
negated: person_op.negated,
|
|
|
|
};
|
|
|
|
operators[operators_len - 2] = last;
|
|
|
|
operators.splice(-1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 18:36:16 +01:00
|
|
|
let base_operators = [];
|
|
|
|
if (operators.length > 1) {
|
|
|
|
base_operators = operators.slice(0, -1);
|
|
|
|
}
|
|
|
|
const base = get_default_suggestion_legacy(base_operators);
|
|
|
|
const attacher = make_attacher(base);
|
|
|
|
|
2018-07-14 13:01:21 +02:00
|
|
|
// Display the default first
|
|
|
|
// `has` and `is` operators work only on predefined categories. Default suggestion
|
|
|
|
// is not displayed in that case. e.g. `messages with one or more abc` as
|
|
|
|
// a suggestion for `has:abc`does not make sense.
|
|
|
|
if (last.operator !== '' && last.operator !== 'has' && last.operator !== 'is') {
|
2018-07-14 13:08:34 +02:00
|
|
|
suggestion = get_default_suggestion_legacy(operators);
|
2019-12-25 18:36:16 +01:00
|
|
|
attacher.push(suggestion);
|
2018-07-14 13:01:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const persons = people.get_all_persons();
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 16:33:38 +01:00
|
|
|
function get_people(flavor) {
|
|
|
|
return function (last, base_operators) {
|
|
|
|
return get_person_suggestions(persons, last, base_operators, flavor);
|
|
|
|
};
|
|
|
|
}
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 16:33:38 +01:00
|
|
|
function get_groups(last, base_operators) {
|
|
|
|
return get_group_suggestions(persons, last, base_operators);
|
|
|
|
}
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 16:33:38 +01:00
|
|
|
const filterers = [
|
|
|
|
get_streams_filter_suggestions,
|
|
|
|
get_is_filter_suggestions,
|
|
|
|
get_sent_by_me_suggestions,
|
|
|
|
get_stream_suggestions,
|
|
|
|
get_people('sender'),
|
|
|
|
get_people('pm-with'),
|
|
|
|
get_people('from'),
|
|
|
|
get_people('group-pm-with'),
|
|
|
|
get_groups,
|
|
|
|
get_topic_suggestions,
|
|
|
|
get_operator_suggestions,
|
|
|
|
get_has_filter_suggestions,
|
|
|
|
];
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 16:58:11 +01:00
|
|
|
const max_items = exports.max_num_of_search_results;
|
|
|
|
|
2019-12-25 16:33:38 +01:00
|
|
|
_.each(filterers, function (filterer) {
|
2019-12-25 16:58:11 +01:00
|
|
|
if (attacher.result.length < max_items) {
|
|
|
|
const suggestions = filterer(last, base_operators);
|
|
|
|
attacher.attach_many(suggestions);
|
|
|
|
}
|
2019-12-25 16:33:38 +01:00
|
|
|
});
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 16:58:11 +01:00
|
|
|
if (attacher.result.length < max_items) {
|
|
|
|
const subset_suggestions = get_operator_subset_suggestions(operators);
|
|
|
|
attacher.concat(subset_suggestions);
|
|
|
|
}
|
|
|
|
|
|
|
|
return attacher.result.slice(0, max_items);
|
2019-12-25 15:36:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_suggestions_legacy = function (query) {
|
|
|
|
const result = exports.get_search_result_legacy(query);
|
|
|
|
return exports.finalize_search_result(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_suggestions = function (base_query, query) {
|
|
|
|
const result = exports.get_search_result(base_query, query);
|
|
|
|
return exports.finalize_search_result(result);
|
|
|
|
};
|
2018-07-14 13:01:21 +02:00
|
|
|
|
2019-12-25 15:36:51 +01:00
|
|
|
exports.finalize_search_result = function (result) {
|
2018-07-14 13:01:21 +02:00
|
|
|
_.each(result, function (sug) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const first = sug.description.charAt(0).toUpperCase();
|
2018-07-14 13:01:21 +02:00
|
|
|
sug.description = first + sug.description.slice(1);
|
|
|
|
});
|
|
|
|
|
2019-12-25 18:36:16 +01:00
|
|
|
// Typeahead expects us to give it strings, not objects,
|
|
|
|
// so we maintain our own hash back to our objects
|
2019-11-02 00:06:25 +01:00
|
|
|
const lookup_table = {};
|
2018-07-14 13:01:21 +02:00
|
|
|
_.each(result, function (obj) {
|
2019-12-25 18:36:16 +01:00
|
|
|
lookup_table[obj.search_string] = obj;
|
2018-07-14 13:01:21 +02:00
|
|
|
});
|
2019-12-25 18:36:16 +01:00
|
|
|
const strings = _.map(result, function (obj) {
|
2018-07-14 13:01:21 +02:00
|
|
|
return obj.search_string;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
strings: strings,
|
|
|
|
lookup_table: lookup_table,
|
|
|
|
};
|
|
|
|
};
|
2013-07-30 23:02:10 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.search_suggestion = exports;
|