2019-03-24 08:37:53 +01:00
|
|
|
var Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
var current_filter;
|
|
|
|
|
|
|
|
exports.reset_current_filter = function () {
|
|
|
|
current_filter = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_current_filter = function (filter) {
|
|
|
|
current_filter = filter;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.active = function () {
|
|
|
|
return current_filter !== undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.filter = function () {
|
|
|
|
return current_filter;
|
|
|
|
};
|
|
|
|
|
2018-07-07 15:47:45 +02:00
|
|
|
exports.is_reading_mode = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current_filter.is_reading_mode();
|
|
|
|
};
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
exports.operators = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return new Filter(page_params.narrow).operators();
|
|
|
|
}
|
|
|
|
return current_filter.operators();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_email = function (user_id, new_email) {
|
|
|
|
if (current_filter !== undefined) {
|
|
|
|
current_filter.update_email(user_id, new_email);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Operators we should send to the server. */
|
|
|
|
exports.public_operators = function () {
|
|
|
|
if (current_filter === undefined) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
return current_filter.public_operators();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.search_string = function () {
|
|
|
|
return Filter.unparse(exports.operators());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Collect operators which appear only once into an object,
|
|
|
|
// and discard those which appear more than once.
|
|
|
|
function collect_single(operators) {
|
|
|
|
var seen = new Dict();
|
|
|
|
var result = new Dict();
|
|
|
|
_.each(operators, function (elem) {
|
|
|
|
var key = elem.operator;
|
|
|
|
if (seen.has(key)) {
|
|
|
|
result.del(key);
|
|
|
|
} else {
|
|
|
|
result.set(key, elem.operand);
|
|
|
|
seen.set(key, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify default compose parameters (stream etc.) based on
|
|
|
|
// the current narrowed view.
|
|
|
|
//
|
|
|
|
// This logic is here and not in the 'compose' module because
|
|
|
|
// it will get more complicated as we add things to the narrow
|
|
|
|
// operator language.
|
2017-12-02 03:13:43 +01:00
|
|
|
exports.set_compose_defaults = function () {
|
|
|
|
var opts = {};
|
2017-04-25 15:25:31 +02:00
|
|
|
var single = collect_single(exports.operators());
|
|
|
|
|
2018-12-22 15:22:07 +01:00
|
|
|
// Set the stream, topic, and/or PM recipient if they are
|
2017-04-25 15:25:31 +02:00
|
|
|
// uniquely specified in the narrow view.
|
|
|
|
|
|
|
|
if (single.has('stream')) {
|
|
|
|
opts.stream = stream_data.get_name(single.get('stream'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (single.has('topic')) {
|
2018-11-15 19:14:16 +01:00
|
|
|
opts.topic = single.get('topic');
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (single.has('pm-with')) {
|
2018-05-28 11:44:28 +02:00
|
|
|
var private_message_recipient = single.get('pm-with');
|
2018-05-28 14:10:33 +02:00
|
|
|
if (people.is_valid_bulk_emails_for_compose(private_message_recipient.split(','))) {
|
2018-05-28 11:44:28 +02:00
|
|
|
opts.private_message_recipient = private_message_recipient;
|
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
2017-12-02 03:13:43 +01:00
|
|
|
return opts;
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.stream = function () {
|
|
|
|
if (current_filter === undefined) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
var stream_operands = current_filter.operands("stream");
|
|
|
|
if (stream_operands.length === 1) {
|
2017-05-12 01:09:11 +02:00
|
|
|
var name = stream_operands[0];
|
|
|
|
|
|
|
|
// Use get_name() to get the most current stream
|
|
|
|
// name (considering renames and capitalization).
|
|
|
|
return stream_data.get_name(name);
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
2018-05-02 13:51:02 +02:00
|
|
|
exports.stream_sub = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stream_operands = current_filter.operands("stream");
|
|
|
|
if (stream_operands.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var name = stream_operands[0];
|
|
|
|
var sub = stream_data.get_sub_by_name(name);
|
|
|
|
|
|
|
|
return sub;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.stream_id = function () {
|
|
|
|
var sub = exports.stream_sub();
|
|
|
|
|
|
|
|
if (!sub) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sub.stream_id;
|
|
|
|
};
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
exports.topic = function () {
|
|
|
|
if (current_filter === undefined) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
var operands = current_filter.operands("topic");
|
|
|
|
if (operands.length === 1) {
|
|
|
|
return operands[0];
|
|
|
|
}
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
2018-02-09 22:26:45 +01:00
|
|
|
exports.pm_string = function () {
|
|
|
|
// If you are narrowed to a PM conversation
|
|
|
|
// with users 4, 5, and 99, this will return "4,5,99"
|
|
|
|
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var operands = current_filter.operands("pm-with");
|
|
|
|
if (operands.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var emails_string = operands[0];
|
|
|
|
|
|
|
|
if (!emails_string) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:05:27 +02:00
|
|
|
var user_ids_string = people.reply_to_to_user_ids_string(emails_string);
|
2018-02-09 22:26:45 +01:00
|
|
|
|
|
|
|
return user_ids_string;
|
|
|
|
};
|
|
|
|
|
2018-05-03 18:58:00 +02:00
|
|
|
exports.get_first_unread_info = function () {
|
2018-05-31 12:55:19 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
// we don't yet support the all-messages view
|
2018-05-09 00:08:24 +02:00
|
|
|
blueslip.error('unexpected call to get_first_unread_info');
|
|
|
|
return {
|
|
|
|
flavor: 'cannot_compute',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:55:19 +02:00
|
|
|
if (!current_filter.can_apply_locally()) {
|
|
|
|
// For things like search queries, where the server has info
|
|
|
|
// that the client isn't privvy to, we need to wait for the
|
|
|
|
// server to give us a definitive list of messages before
|
|
|
|
// deciding where we'll move the selection.
|
|
|
|
return {
|
|
|
|
flavor: 'cannot_compute',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
var unread_ids = exports._possible_unread_message_ids();
|
2018-05-02 21:24:38 +02:00
|
|
|
|
|
|
|
if (unread_ids === undefined) {
|
2018-05-08 23:06:00 +02:00
|
|
|
// _possible_unread_message_ids() only works for certain narrows
|
2018-05-03 18:58:00 +02:00
|
|
|
return {
|
|
|
|
flavor: 'cannot_compute',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
var msg_id = current_filter.first_valid_id_from(unread_ids);
|
|
|
|
|
|
|
|
if (msg_id === undefined) {
|
2018-05-03 18:58:00 +02:00
|
|
|
return {
|
|
|
|
flavor: 'not_found',
|
|
|
|
};
|
2018-05-02 21:24:38 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
|
2018-05-03 18:58:00 +02:00
|
|
|
return {
|
|
|
|
flavor: 'found',
|
|
|
|
msg_id: unread_ids[0],
|
|
|
|
};
|
2018-05-02 21:24:38 +02:00
|
|
|
};
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
exports._possible_unread_message_ids = function () {
|
2018-05-02 15:31:36 +02:00
|
|
|
// This function currently only returns valid results for
|
|
|
|
// certain types of narrows, mostly left sidebar narrows.
|
|
|
|
// For more complicated narrows we may return undefined.
|
2018-05-08 23:06:00 +02:00
|
|
|
//
|
|
|
|
// If we do return a result, it will be a subset of unread
|
|
|
|
// message ids but possibly a superset of unread message ids
|
|
|
|
// that match our filter.
|
2018-05-02 15:31:36 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var stream_id;
|
|
|
|
var topic_name;
|
|
|
|
var pm_string;
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
if (current_filter.can_bucket_by('stream', 'topic')) {
|
2018-05-02 15:31:36 +02:00
|
|
|
stream_id = exports.stream_id();
|
|
|
|
if (stream_id === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-05-08 20:36:47 +02:00
|
|
|
topic_name = exports.topic();
|
|
|
|
return unread.get_msg_ids_for_topic(stream_id, topic_name);
|
2018-05-02 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
if (current_filter.can_bucket_by('stream')) {
|
2018-05-02 15:31:36 +02:00
|
|
|
stream_id = exports.stream_id();
|
|
|
|
if (stream_id === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-05-08 20:36:47 +02:00
|
|
|
return unread.get_msg_ids_for_stream(stream_id);
|
2018-05-02 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
if (current_filter.can_bucket_by('pm-with')) {
|
2018-05-02 15:31:36 +02:00
|
|
|
pm_string = exports.pm_string();
|
|
|
|
if (pm_string === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return unread.get_msg_ids_for_person(pm_string);
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
if (current_filter.can_bucket_by('is-private')) {
|
2018-05-02 15:31:36 +02:00
|
|
|
return unread.get_msg_ids_for_private();
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
if (current_filter.can_bucket_by('is-mentioned')) {
|
2018-05-02 15:31:36 +02:00
|
|
|
return unread.get_msg_ids_for_mentions();
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
if (current_filter.can_bucket_by('is-starred')) {
|
2018-05-03 22:57:07 +02:00
|
|
|
return unread.get_msg_ids_for_starred();
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:55:56 +02:00
|
|
|
if (current_filter.can_bucket_by('sender')) {
|
|
|
|
// TODO: see #9352 to make this more efficient
|
|
|
|
return unread.get_all_msg_ids();
|
|
|
|
}
|
|
|
|
|
2018-05-31 16:46:39 +02:00
|
|
|
if (current_filter.can_apply_locally()) {
|
|
|
|
return unread.get_all_msg_ids();
|
|
|
|
}
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
// Are we narrowed to PMs: all PMs or PMs with particular people.
|
|
|
|
exports.narrowed_to_pms = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-06 18:19:09 +02:00
|
|
|
return current_filter.has_operator("pm-with") ||
|
|
|
|
current_filter.has_operand("is", "private");
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.narrowed_by_pm_reply = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var operators = current_filter.operators();
|
2018-06-06 18:19:09 +02:00
|
|
|
return operators.length === 1 &&
|
|
|
|
current_filter.has_operator('pm-with');
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.narrowed_by_topic_reply = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var operators = current_filter.operators();
|
2018-06-06 18:19:09 +02:00
|
|
|
return operators.length === 2 &&
|
2017-04-25 15:25:31 +02:00
|
|
|
current_filter.operands("stream").length === 1 &&
|
2018-06-06 18:19:09 +02:00
|
|
|
current_filter.operands("topic").length === 1;
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// We auto-reply under certain conditions, namely when you're narrowed
|
2018-11-13 16:27:16 +01:00
|
|
|
// to a PM (or huddle), and when you're narrowed to some stream/topic pair
|
2017-04-25 15:25:31 +02:00
|
|
|
exports.narrowed_by_reply = function () {
|
2018-06-06 18:19:09 +02:00
|
|
|
return exports.narrowed_by_pm_reply() ||
|
|
|
|
exports.narrowed_by_topic_reply();
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
2017-04-21 22:51:25 +02:00
|
|
|
exports.narrowed_by_stream_reply = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var operators = current_filter.operators();
|
2018-06-06 18:19:09 +02:00
|
|
|
return operators.length === 1 &&
|
|
|
|
current_filter.operands("stream").length === 1;
|
2017-04-21 22:51:25 +02:00
|
|
|
};
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
exports.narrowed_to_topic = function () {
|
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-06 18:19:09 +02:00
|
|
|
return current_filter.has_operator("stream") &&
|
|
|
|
current_filter.has_operator("topic");
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.narrowed_to_search = function () {
|
2018-06-06 18:50:09 +02:00
|
|
|
return current_filter !== undefined && current_filter.is_search();
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.muting_enabled = function () {
|
2018-06-06 18:19:09 +02:00
|
|
|
return !exports.narrowed_to_topic() && !exports.narrowed_to_search() &&
|
|
|
|
!exports.narrowed_to_pms();
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_for_stream_id = function (stream_id) {
|
|
|
|
// This is not perfect, since we still track narrows by
|
|
|
|
// name, not id, but at least the interface is good going
|
|
|
|
// forward.
|
2018-05-02 14:41:40 +02:00
|
|
|
var narrow_stream_id = exports.stream_id();
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2018-05-02 14:41:40 +02:00
|
|
|
if (narrow_stream_id === undefined) {
|
2017-04-25 15:25:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 18:19:09 +02:00
|
|
|
return stream_id === narrow_stream_id;
|
2017-04-25 15:25:31 +02:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.narrow_state = exports;
|