zulip/static/js/narrow_state.js

360 lines
9.3 KiB
JavaScript
Raw Normal View History

let 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;
};
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) {
return;
}
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) {
const seen = new Map();
const result = new Map();
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const elem of operators) {
const key = elem.operator;
if (seen.has(key)) {
result.delete(key);
} else {
result.set(key, elem.operand);
seen.set(key, true);
}
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
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.
exports.set_compose_defaults = function () {
const opts = {};
const single = collect_single(exports.operators());
// Set the stream, topic, and/or PM recipient if they are
// uniquely specified in the narrow view.
if (single.has("stream")) {
opts.stream = stream_data.get_name(single.get("stream"));
}
if (single.has("topic")) {
opts.topic = single.get("topic");
}
if (single.has("pm-with")) {
const private_message_recipient = single.get("pm-with");
if (people.is_valid_bulk_emails_for_compose(private_message_recipient.split(","))) {
opts.private_message_recipient = private_message_recipient;
}
}
return opts;
};
exports.stream = function () {
if (current_filter === undefined) {
return;
}
const stream_operands = current_filter.operands("stream");
if (stream_operands.length === 1) {
const name = stream_operands[0];
// Use get_name() to get the most current stream
// name (considering renames and capitalization).
return stream_data.get_name(name);
}
return;
};
exports.stream_sub = function () {
if (current_filter === undefined) {
return;
}
const stream_operands = current_filter.operands("stream");
if (stream_operands.length !== 1) {
return;
}
const name = stream_operands[0];
const sub = stream_data.get_sub_by_name(name);
return sub;
};
exports.stream_id = function () {
const sub = exports.stream_sub();
if (!sub) {
return;
}
return sub.stream_id;
};
exports.topic = function () {
if (current_filter === undefined) {
return;
}
const operands = current_filter.operands("topic");
if (operands.length === 1) {
return operands[0];
}
return;
};
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;
}
const operands = current_filter.operands("pm-with");
2018-02-09 22:26:45 +01:00
if (operands.length !== 1) {
return;
}
const emails_string = operands[0];
2018-02-09 22:26:45 +01:00
if (!emails_string) {
return;
}
const user_ids_string = people.reply_to_to_user_ids_string(emails_string);
2018-02-09 22:26:45 +01:00
return user_ids_string;
};
exports.get_first_unread_info = function () {
if (current_filter === undefined) {
// we don't yet support the all-messages view
blueslip.error("unexpected call to get_first_unread_info");
return {
flavor: "cannot_compute",
};
}
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",
};
}
const unread_ids = exports._possible_unread_message_ids();
if (unread_ids === undefined) {
// _possible_unread_message_ids() only works for certain narrows
return {
flavor: "cannot_compute",
};
}
const msg_id = current_filter.first_valid_id_from(unread_ids);
if (msg_id === undefined) {
return {
flavor: "not_found",
};
}
return {
flavor: "found",
msg_id: unread_ids[0],
};
};
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.
//
// 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;
}
let stream_id;
let topic_name;
let pm_string;
2018-05-02 15:31:36 +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 [];
}
topic_name = exports.topic();
return unread.get_msg_ids_for_topic(stream_id, topic_name);
2018-05-02 15:31:36 +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 [];
}
return unread.get_msg_ids_for_stream(stream_id);
2018-05-02 15:31:36 +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);
}
if (current_filter.can_bucket_by("is-private")) {
2018-05-02 15:31:36 +02:00
return unread.get_msg_ids_for_private();
}
if (current_filter.can_bucket_by("is-mentioned")) {
2018-05-02 15:31:36 +02:00
return unread.get_msg_ids_for_mentions();
}
if (current_filter.can_bucket_by("is-starred")) {
return unread.get_msg_ids_for_starred();
}
if (current_filter.can_bucket_by("sender")) {
// TODO: see #9352 to make this more efficient
return unread.get_all_msg_ids();
}
if (current_filter.can_apply_locally()) {
return unread.get_all_msg_ids();
}
2018-05-02 15:31:36 +02:00
return;
};
// Are we narrowed to PMs: all PMs or PMs with particular people.
exports.narrowed_to_pms = function () {
if (current_filter === undefined) {
return false;
}
return current_filter.has_operator("pm-with") ||
current_filter.has_operand("is", "private");
};
exports.narrowed_by_pm_reply = function () {
if (current_filter === undefined) {
return false;
}
const operators = current_filter.operators();
return operators.length === 1 &&
current_filter.has_operator("pm-with");
};
exports.narrowed_by_topic_reply = function () {
if (current_filter === undefined) {
return false;
}
const operators = current_filter.operators();
return operators.length === 2 &&
current_filter.operands("stream").length === 1 &&
current_filter.operands("topic").length === 1;
};
// We auto-reply under certain conditions, namely when you're narrowed
// to a PM (or huddle), and when you're narrowed to some stream/topic pair
exports.narrowed_by_reply = function () {
return exports.narrowed_by_pm_reply() ||
exports.narrowed_by_topic_reply();
};
exports.narrowed_by_stream_reply = function () {
if (current_filter === undefined) {
return false;
}
const operators = current_filter.operators();
return operators.length === 1 &&
current_filter.operands("stream").length === 1;
};
exports.narrowed_to_topic = function () {
if (current_filter === undefined) {
return false;
}
return current_filter.has_operator("stream") &&
current_filter.has_operator("topic");
};
exports.narrowed_to_search = function () {
return current_filter !== undefined && current_filter.is_search();
};
exports.narrowed_to_starred = function () {
if (current_filter === undefined) {
return false;
}
return current_filter.has_operand("is", "starred");
};
exports.muting_enabled = function () {
return !exports.narrowed_to_topic() && !exports.narrowed_to_search() &&
!exports.narrowed_to_pms() && !exports.narrowed_to_starred();
};
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.
const narrow_stream_id = exports.stream_id();
if (narrow_stream_id === undefined) {
return false;
}
return stream_id === narrow_stream_id;
};
window.narrow_state = exports;