2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:47:56 +01:00
|
|
|
import {Filter} from "./filter";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 00:47:56 +01:00
|
|
|
import * as people from "./people";
|
2023-02-14 06:42:58 +01:00
|
|
|
import * as recent_topics_util from "./recent_topics_util";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let current_filter;
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function reset_current_filter() {
|
2017-04-25 15:25:31 +02:00
|
|
|
current_filter = undefined;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function set_current_filter(filter) {
|
2017-04-25 15:25:31 +02:00
|
|
|
current_filter = filter;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function active() {
|
2017-04-25 15:25:31 +02:00
|
|
|
return current_filter !== undefined;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function filter() {
|
2020-07-05 12:19:09 +02:00
|
|
|
// Both, `All messages` and
|
|
|
|
// `Recent topics` have `current_filter=undefined`
|
2017-04-25 15:25:31 +02:00
|
|
|
return current_filter;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function operators() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return new Filter(page_params.narrow).operators();
|
|
|
|
}
|
|
|
|
return current_filter.operators();
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2023-02-14 06:42:58 +01:00
|
|
|
export function is_message_feed_visible() {
|
|
|
|
return !recent_topics_util.is_visible();
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function update_email(user_id, new_email) {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter !== undefined) {
|
|
|
|
current_filter.update_email(user_id, new_email);
|
|
|
|
}
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
|
|
|
/* Operators we should send to the server. */
|
2021-02-28 00:47:56 +01:00
|
|
|
export function public_operators() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
return current_filter.public_operators();
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function search_string() {
|
|
|
|
return Filter.unparse(operators());
|
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
|
|
|
// Collect operators which appear only once into an object,
|
|
|
|
// and discard those which appear more than once.
|
|
|
|
function collect_single(operators) {
|
2020-07-16 23:29:01 +02:00
|
|
|
const seen = new Map();
|
2020-02-03 09:39:58 +01:00
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = elem.operator;
|
2017-04-25 15:25:31 +02:00
|
|
|
if (seen.has(key)) {
|
2020-02-03 07:41:38 +01:00
|
|
|
result.delete(key);
|
2017-04-25 15:25:31 +02:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2017-04-25 15:25:31 +02: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.
|
2021-02-28 00:47:56 +01:00
|
|
|
export function set_compose_defaults() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {};
|
2021-02-28 00:47:56 +01:00
|
|
|
const single = collect_single(operators());
|
2017-04-25 15:25:31 +02:00
|
|
|
|
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.
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (single.has("stream")) {
|
|
|
|
opts.stream = stream_data.get_name(single.get("stream"));
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (single.has("topic")) {
|
|
|
|
opts.topic = single.get("topic");
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
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(","))) {
|
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;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function stream() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_operands = current_filter.operands("stream");
|
2017-04-25 15:25:31 +02:00
|
|
|
if (stream_operands.length === 1) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const name = stream_operands[0];
|
2017-05-12 01:09:11 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function stream_sub() {
|
2018-05-02 13:51:02 +02:00
|
|
|
if (current_filter === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-05-02 13:51:02 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_operands = current_filter.operands("stream");
|
2018-05-02 13:51:02 +02:00
|
|
|
if (stream_operands.length !== 1) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-05-02 13:51:02 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const name = stream_operands[0];
|
|
|
|
const sub = stream_data.get_sub_by_name(name);
|
2018-05-02 13:51:02 +02:00
|
|
|
|
|
|
|
return sub;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2018-05-02 13:51:02 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function topic() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-25 15:25:31 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const operands = current_filter.operands("topic");
|
2017-04-25 15:25:31 +02:00
|
|
|
if (operands.length === 1) {
|
|
|
|
return operands[0];
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
export function pm_ids_string() {
|
2018-02-09 22:26:45 +01:00
|
|
|
// If you are narrowed to a PM conversation
|
|
|
|
// with users 4, 5, and 99, this will return "4,5,99"
|
2022-05-26 04:20:07 +02:00
|
|
|
const emails_string = pm_emails_string();
|
2018-02-09 22:26:45 +01:00
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
if (!emails_string) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-09 22:26:45 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
const user_ids_string = people.reply_to_to_user_ids_string(emails_string);
|
2018-02-09 22:26:45 +01:00
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
return user_ids_string;
|
|
|
|
}
|
2018-02-09 22:26:45 +01:00
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
export function pm_emails_string() {
|
|
|
|
if (current_filter === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-02-09 22:26:45 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
const operands = current_filter.operands("pm-with");
|
|
|
|
if (operands.length !== 1) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-02-09 22:26:45 +01:00
|
|
|
|
2022-05-26 04:20:07 +02:00
|
|
|
return operands[0];
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2018-02-09 22:26:45 +01:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function get_first_unread_info() {
|
2018-05-31 12:55:19 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
// we don't yet support the all-messages view
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("unexpected call to get_first_unread_info");
|
2018-05-09 00:08:24 +02:00
|
|
|
return {
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "cannot_compute",
|
2018-05-09 00:08:24 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:55:19 +02:00
|
|
|
if (!current_filter.can_apply_locally()) {
|
|
|
|
// For things like search queries, where the server has info
|
2022-02-08 00:13:33 +01:00
|
|
|
// that the client isn't privy to, we need to wait for the
|
2018-05-31 12:55:19 +02:00
|
|
|
// server to give us a definitive list of messages before
|
|
|
|
// deciding where we'll move the selection.
|
|
|
|
return {
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "cannot_compute",
|
2018-05-31 12:55:19 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
const unread_ids = _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 {
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "cannot_compute",
|
2018-05-03 18:58:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_id = current_filter.first_valid_id_from(unread_ids);
|
2018-05-09 00:08:24 +02:00
|
|
|
|
|
|
|
if (msg_id === undefined) {
|
2018-05-03 18:58:00 +02:00
|
|
|
return {
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "not_found",
|
2018-05-03 18:58:00 +02:00
|
|
|
};
|
2018-05-02 21:24:38 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 18:58:00 +02:00
|
|
|
return {
|
2020-07-15 01:29:15 +02:00
|
|
|
flavor: "found",
|
2018-05-03 18:58:00 +02:00
|
|
|
msg_id: unread_ids[0],
|
|
|
|
};
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2018-05-02 21:24:38 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function _possible_unread_message_ids() {
|
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) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-05-02 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 02:22:47 +02:00
|
|
|
let sub;
|
2019-11-02 00:06:25 +01:00
|
|
|
let topic_name;
|
2020-09-23 02:29:31 +02:00
|
|
|
let current_filter_pm_string;
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (current_filter.can_bucket_by("stream", "topic")) {
|
2021-02-28 00:47:56 +01:00
|
|
|
sub = stream_sub();
|
2020-09-23 02:22:47 +02:00
|
|
|
if (sub === undefined) {
|
2018-05-02 15:31:36 +02:00
|
|
|
return [];
|
|
|
|
}
|
2021-02-28 00:47:56 +01:00
|
|
|
topic_name = topic();
|
2020-09-23 02:22:47 +02:00
|
|
|
return unread.get_msg_ids_for_topic(sub.stream_id, topic_name);
|
2018-05-02 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (current_filter.can_bucket_by("stream")) {
|
2021-02-28 00:47:56 +01:00
|
|
|
sub = stream_sub();
|
2020-09-23 02:22:47 +02:00
|
|
|
if (sub === undefined) {
|
2018-05-02 15:31:36 +02:00
|
|
|
return [];
|
|
|
|
}
|
2020-09-23 02:22:47 +02:00
|
|
|
return unread.get_msg_ids_for_stream(sub.stream_id);
|
2018-05-02 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (current_filter.can_bucket_by("pm-with")) {
|
2022-05-26 04:20:07 +02:00
|
|
|
current_filter_pm_string = pm_ids_string();
|
2020-09-23 02:29:31 +02:00
|
|
|
if (current_filter_pm_string === undefined) {
|
2018-05-02 15:31:36 +02:00
|
|
|
return [];
|
|
|
|
}
|
2022-10-22 07:13:57 +02:00
|
|
|
return unread.get_msg_ids_for_user_ids_string(current_filter_pm_string);
|
2018-05-02 15:31:36 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +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();
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +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();
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +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();
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (current_filter.can_bucket_by("sender")) {
|
2018-05-10 13:55:56 +02:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
// Are we narrowed to PMs: all PMs or PMs with particular people.
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_to_pms() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
return current_filter.has_operator("pm-with") || current_filter.has_operand("is", "private");
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_by_pm_reply() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const operators = current_filter.operators();
|
2020-07-15 00:34:28 +02:00
|
|
|
return operators.length === 1 && current_filter.has_operator("pm-with");
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_by_topic_reply() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const operators = current_filter.operators();
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
operators.length === 2 &&
|
|
|
|
current_filter.operands("stream").length === 1 &&
|
|
|
|
current_filter.operands("topic").length === 1
|
|
|
|
);
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
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
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_by_reply() {
|
|
|
|
return narrowed_by_pm_reply() || narrowed_by_topic_reply();
|
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_by_stream_reply() {
|
2017-04-21 22:51:25 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const operators = current_filter.operators();
|
2020-07-15 00:34:28 +02:00
|
|
|
return operators.length === 1 && current_filter.operands("stream").length === 1;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-21 22:51:25 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_to_topic() {
|
2017-04-25 15:25:31 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
return current_filter.has_operator("stream") && current_filter.has_operator("topic");
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_to_search() {
|
2018-06-06 18:50:09 +02:00
|
|
|
return current_filter !== undefined && current_filter.is_search();
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function narrowed_to_starred() {
|
2020-02-25 20:46:21 +01:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return current_filter.has_operand("is", "starred");
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2020-02-25 20:46:21 +01:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function excludes_muted_topics() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
2021-02-28 00:47:56 +01:00
|
|
|
!narrowed_to_topic() &&
|
|
|
|
!narrowed_to_search() &&
|
|
|
|
!narrowed_to_pms() &&
|
|
|
|
!narrowed_to_starred()
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2021-02-28 00:47:56 +01:00
|
|
|
export function is_for_stream_id(stream_id) {
|
2017-04-25 15:25:31 +02:00
|
|
|
// This is not perfect, since we still track narrows by
|
|
|
|
// name, not id, but at least the interface is good going
|
|
|
|
// forward.
|
2021-02-28 00:47:56 +01:00
|
|
|
const narrow_sub = stream_sub();
|
2017-04-25 15:25:31 +02:00
|
|
|
|
2020-09-23 02:22:47 +02:00
|
|
|
if (narrow_sub === undefined) {
|
2017-04-25 15:25:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:22:47 +02:00
|
|
|
return stream_id === narrow_sub.stream_id;
|
2021-02-28 00:47:56 +01:00
|
|
|
}
|