2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-08-03 23:57:48 +02:00
|
|
|
const {FoldDict} = require("./fold_dict");
|
2021-02-28 01:10:03 +01:00
|
|
|
const message_store = require("./message_store");
|
2021-02-28 00:41:04 +01:00
|
|
|
const muting = require("./muting");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
2021-02-28 00:53:59 +01:00
|
|
|
const stream_data = require("./stream_data");
|
2020-07-24 06:02:07 +02:00
|
|
|
const util = require("./util");
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-02-04 21:39:58 +01:00
|
|
|
// The unread module tracks the message IDs and locations of the
|
|
|
|
// user's unread messages. The tracking is initialized with
|
|
|
|
// server-provided data of the total set of unread messages in the
|
|
|
|
// user's history via page_params.unread_msgs (well, it cuts off at
|
|
|
|
// MAX_UNREAD_MESSAGES unreads for performance reasons). As a result,
|
|
|
|
// it can contain many thousands of messages that we don't have full
|
|
|
|
// data for in `message_store`, so we cannot in general look these
|
|
|
|
// messages up there.
|
|
|
|
|
|
|
|
// See https://zulip.readthedocs.io/en/latest/subsystems/pointer.html
|
|
|
|
// for more details on how this system is designed.
|
2016-08-17 01:19:14 +02:00
|
|
|
|
2016-04-03 16:45:07 +02:00
|
|
|
exports.messages_read_in_narrow = false;
|
2018-08-04 08:42:57 +02:00
|
|
|
exports.set_messages_read_in_narrow = function (value) {
|
|
|
|
exports.messages_read_in_narrow = value;
|
|
|
|
};
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2020-02-01 03:17:09 +01:00
|
|
|
const unread_messages = new Set();
|
2017-08-15 19:54:38 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
class Bucketer {
|
|
|
|
reverse_lookup = new Map();
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
constructor(options) {
|
|
|
|
this.key_to_bucket = new options.KeyDict();
|
|
|
|
this.make_bucket = options.make_bucket;
|
|
|
|
}
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
clear() {
|
|
|
|
this.key_to_bucket.clear();
|
|
|
|
this.reverse_lookup.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
add(opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucket_key = opts.bucket_key;
|
|
|
|
const item_id = opts.item_id;
|
|
|
|
const add_callback = opts.add_callback;
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
let bucket = this.key_to_bucket.get(bucket_key);
|
2017-08-03 04:32:21 +02:00
|
|
|
if (!bucket) {
|
2020-07-23 03:55:35 +02:00
|
|
|
bucket = this.make_bucket();
|
|
|
|
this.key_to_bucket.set(bucket_key, bucket);
|
2017-08-03 04:32:21 +02:00
|
|
|
}
|
|
|
|
if (add_callback) {
|
|
|
|
add_callback(bucket, item_id);
|
|
|
|
} else {
|
|
|
|
bucket.add(item_id);
|
|
|
|
}
|
2020-07-23 03:55:35 +02:00
|
|
|
this.reverse_lookup.set(item_id, bucket);
|
|
|
|
}
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
delete(item_id) {
|
|
|
|
const bucket = this.reverse_lookup.get(item_id);
|
2017-08-03 04:32:21 +02:00
|
|
|
if (bucket) {
|
2020-02-01 03:28:35 +01:00
|
|
|
bucket.delete(item_id);
|
2020-07-23 03:55:35 +02:00
|
|
|
this.reverse_lookup.delete(item_id);
|
2017-08-03 04:32:21 +02:00
|
|
|
}
|
2020-07-23 03:55:35 +02:00
|
|
|
}
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
get_bucket(bucket_key) {
|
|
|
|
return this.key_to_bucket.get(bucket_key);
|
|
|
|
}
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
keys() {
|
|
|
|
return this.key_to_bucket.keys();
|
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
values() {
|
|
|
|
return this.key_to_bucket.values();
|
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
|
2020-07-23 03:55:35 +02:00
|
|
|
[Symbol.iterator]() {
|
|
|
|
return this.key_to_bucket[Symbol.iterator]();
|
|
|
|
}
|
2017-08-03 04:32:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
class UnreadPMCounter {
|
|
|
|
bucketer = new Bucketer({
|
2020-02-03 09:39:58 +01:00
|
|
|
KeyDict: Map,
|
2020-02-01 03:28:35 +01:00
|
|
|
make_bucket: () => new Set(),
|
2017-08-03 04:32:21 +02:00
|
|
|
});
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
clear() {
|
|
|
|
this.bucketer.clear();
|
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
set_pms(pms) {
|
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 obj of pms) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = obj.sender_id.toString();
|
2020-07-23 03:58:17 +02:00
|
|
|
this.set_message_ids(user_ids_string, obj.unread_message_ids);
|
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
|
|
|
}
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
set_huddles(huddles) {
|
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 obj of huddles) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = people.pm_lookup_key(obj.user_ids_string);
|
2020-07-23 03:58:17 +02:00
|
|
|
this.set_message_ids(user_ids_string, obj.unread_message_ids);
|
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
|
|
|
}
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
set_message_ids(user_ids_string, unread_message_ids) {
|
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 msg_id of unread_message_ids) {
|
2020-07-23 03:58:17 +02:00
|
|
|
this.bucketer.add({
|
2017-08-01 14:50:40 +02:00
|
|
|
bucket_key: user_ids_string,
|
|
|
|
item_id: msg_id,
|
|
|
|
});
|
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
|
|
|
}
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
add(message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = people.pm_reply_user_string(message);
|
2017-07-31 14:59:18 +02:00
|
|
|
if (user_ids_string) {
|
2020-07-23 03:58:17 +02:00
|
|
|
this.bucketer.add({
|
2017-08-03 04:32:21 +02:00
|
|
|
bucket_key: user_ids_string,
|
|
|
|
item_id: message.id,
|
|
|
|
});
|
2017-07-31 14:59:18 +02:00
|
|
|
}
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
delete(message_id) {
|
|
|
|
this.bucketer.delete(message_id);
|
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
get_counts() {
|
2020-02-03 09:39:58 +01:00
|
|
|
const pm_dict = new Map(); // Hash by user_ids_string -> count
|
2019-11-02 00:06:25 +01:00
|
|
|
let total_count = 0;
|
2020-07-23 03:58:17 +02:00
|
|
|
for (const [user_ids_string, id_set] of this.bucketer) {
|
2020-02-01 03:28:35 +01:00
|
|
|
const count = id_set.size;
|
2017-07-31 14:59:18 +02:00
|
|
|
pm_dict.set(user_ids_string, count);
|
|
|
|
total_count += count;
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
total_count,
|
|
|
|
pm_dict,
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
num_unread(user_ids_string) {
|
2017-07-31 14:59:18 +02:00
|
|
|
if (!user_ids_string) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
const bucket = this.bucketer.get_bucket(user_ids_string);
|
2017-08-03 04:32:21 +02:00
|
|
|
|
|
|
|
if (!bucket) {
|
2017-07-31 14:59:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-02-01 03:28:35 +01:00
|
|
|
return bucket.size;
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
get_msg_ids() {
|
2020-02-12 01:35:16 +01:00
|
|
|
const ids = [];
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
for (const id_set of this.bucketer.values()) {
|
2020-02-26 09:08:35 +01:00
|
|
|
for (const id of id_set) {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2018-05-02 19:50:25 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
get_msg_ids_for_person(user_ids_string) {
|
2018-05-02 13:02:32 +02:00
|
|
|
if (!user_ids_string) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-07-23 03:58:17 +02:00
|
|
|
const bucket = this.bucketer.get_bucket(user_ids_string);
|
2018-05-02 13:02:32 +02:00
|
|
|
|
|
|
|
if (!bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(bucket);
|
2018-05-02 13:02:32 +02:00
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 03:58:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.unread_pm_counter = new UnreadPMCounter();
|
2017-07-31 14:59:18 +02:00
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
function make_per_stream_bucketer() {
|
2020-07-23 03:55:35 +02:00
|
|
|
return new Bucketer({
|
2020-02-04 02:48:42 +01:00
|
|
|
KeyDict: FoldDict, // bucket keys are topics
|
2020-02-01 03:28:35 +01:00
|
|
|
make_bucket: () => new Set(),
|
2017-08-03 05:01:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
class UnreadTopicCounter {
|
|
|
|
bucketer = new Bucketer({
|
2020-02-03 09:42:50 +01:00
|
|
|
KeyDict: Map, // bucket keys are stream_ids
|
2017-08-03 05:01:47 +02:00
|
|
|
make_bucket: make_per_stream_bucketer,
|
|
|
|
});
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
clear() {
|
|
|
|
this.bucketer.clear();
|
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
set_streams(objs) {
|
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 obj of objs) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = obj.stream_id;
|
|
|
|
const topic = obj.topic;
|
|
|
|
const unread_message_ids = obj.unread_message_ids;
|
2017-08-01 14:50:40 +02:00
|
|
|
|
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 msg_id of unread_message_ids) {
|
2020-07-23 04:01:19 +02:00
|
|
|
this.add(stream_id, topic, msg_id);
|
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
|
|
|
}
|
|
|
|
}
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
add(stream_id, topic, msg_id) {
|
|
|
|
this.bucketer.add({
|
2017-08-03 05:01:47 +02:00
|
|
|
bucket_key: stream_id,
|
|
|
|
item_id: msg_id,
|
2020-07-20 22:18:43 +02:00
|
|
|
add_callback(per_stream_bucketer) {
|
2017-08-03 05:01:47 +02:00
|
|
|
per_stream_bucketer.add({
|
|
|
|
bucket_key: topic,
|
|
|
|
item_id: msg_id,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
delete(msg_id) {
|
|
|
|
this.bucketer.delete(msg_id);
|
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
get_counts() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = {};
|
2016-12-15 01:53:33 +01:00
|
|
|
res.stream_unread_messages = 0;
|
2020-07-16 23:29:01 +02:00
|
|
|
res.stream_count = new Map(); // hash by stream_id -> count
|
2020-07-23 04:01:19 +02:00
|
|
|
for (const [stream_id, per_stream_bucketer] of this.bucketer) {
|
2016-12-15 01:59:08 +01:00
|
|
|
// We track unread counts for streams that may be currently
|
|
|
|
// unsubscribed. Since users may re-subscribe, we don't
|
|
|
|
// completely throw away the data. But we do ignore it here,
|
|
|
|
// so that callers have a view of the **current** world.
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2017-05-13 19:26:54 +02:00
|
|
|
if (!sub || !stream_data.is_subscribed(sub.name)) {
|
2020-02-03 09:26:53 +01:00
|
|
|
continue;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream_count = 0;
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
2020-02-01 03:28:35 +01:00
|
|
|
const topic_count = msgs.size;
|
2018-12-13 22:26:10 +01:00
|
|
|
if (!muting.is_topic_muted(stream_id, topic)) {
|
2017-08-03 05:01:47 +02:00
|
|
|
stream_count += topic_count;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
res.stream_count.set(stream_id, stream_count);
|
2019-05-21 09:33:21 +02:00
|
|
|
if (!stream_data.is_muted(stream_id)) {
|
2017-08-03 05:01:47 +02:00
|
|
|
res.stream_unread_messages += stream_count;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2016-12-15 01:53:33 +01:00
|
|
|
|
|
|
|
return res;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
get_missing_topics(opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = opts.stream_id;
|
|
|
|
const topic_dict = opts.topic_dict;
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
|
2018-05-13 11:38:40 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
let topic_names = Array.from(per_stream_bucketer.keys());
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
topic_names = topic_names.filter((topic_name) => !topic_dict.has(topic_name));
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const result = topic_names.map((topic_name) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const msgs = per_stream_bucketer.get_bucket(topic_name);
|
2018-05-13 11:38:40 +02:00
|
|
|
|
|
|
|
return {
|
2020-02-04 02:48:42 +01:00
|
|
|
pretty_name: topic_name,
|
2020-02-04 23:48:49 +01:00
|
|
|
message_id: Math.max(...Array.from(msgs)),
|
2018-05-13 11:38:40 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
get_stream_count(stream_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream_count = 0;
|
2017-01-15 16:44:33 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
2017-01-15 16:44:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
2018-12-13 22:26:10 +01:00
|
|
|
if (sub && !muting.is_topic_muted(stream_id, topic)) {
|
2020-02-01 03:28:35 +01:00
|
|
|
stream_count += msgs.size;
|
2017-01-15 16:44:33 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2017-01-15 16:44:33 +01:00
|
|
|
|
|
|
|
return stream_count;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2017-01-15 16:44:33 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
get(stream_id, topic) {
|
|
|
|
const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return 0;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_bucket = per_stream_bucketer.get_bucket(topic);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!topic_bucket) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
return topic_bucket.size;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
get_msg_ids_for_stream(stream_id) {
|
|
|
|
const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
const ids = [];
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2020-02-26 09:08:35 +01:00
|
|
|
for (const [topic, id_set] of per_stream_bucketer) {
|
2018-12-13 22:26:10 +01:00
|
|
|
if (sub && !muting.is_topic_muted(stream_id, topic)) {
|
2020-02-26 09:08:35 +01:00
|
|
|
for (const id of id_set) {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
get_msg_ids_for_topic(stream_id, topic) {
|
|
|
|
const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
|
2018-04-25 22:48:51 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_bucket = per_stream_bucketer.get_bucket(topic);
|
2018-04-25 22:48:51 +02:00
|
|
|
if (!topic_bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(topic_bucket);
|
2018-04-25 22:48:51 +02:00
|
|
|
return util.sorted_ids(ids);
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
2018-04-25 22:48:51 +02:00
|
|
|
|
2020-07-23 04:01:19 +02:00
|
|
|
topic_has_any_unread(stream_id, topic) {
|
|
|
|
const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
|
2017-04-21 18:00:19 +02:00
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!per_stream_bucketer) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const id_set = per_stream_bucketer.get_bucket(topic);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!id_set) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
return id_set.size !== 0;
|
2020-07-23 04:01:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.unread_topic_counter = new UnreadTopicCounter();
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2020-02-01 03:13:57 +01:00
|
|
|
exports.unread_mentions_counter = new Set();
|
2017-08-01 14:04:48 +02:00
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
exports.message_unread = function (message) {
|
|
|
|
if (message === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-16 21:42:41 +01:00
|
|
|
return message.unread;
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
exports.get_unread_message_ids = function (message_ids) {
|
2020-07-02 01:39:34 +02:00
|
|
|
return message_ids.filter((message_id) => unread_messages.has(message_id));
|
2017-12-16 16:53:27 +01:00
|
|
|
};
|
|
|
|
|
2018-03-28 21:00:48 +02:00
|
|
|
exports.get_unread_messages = function (messages) {
|
2020-07-02 01:39:34 +02:00
|
|
|
return messages.filter((message) => unread_messages.has(message.id));
|
2017-08-03 22:01:21 +02:00
|
|
|
};
|
|
|
|
|
2016-08-27 03:29:32 +02:00
|
|
|
exports.update_unread_topics = function (msg, event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_topic = util.get_edit_event_topic(event);
|
2020-04-07 22:29:22 +02:00
|
|
|
const {new_stream_id} = event;
|
2018-12-22 17:45:18 +01:00
|
|
|
|
2020-04-07 22:29:22 +02:00
|
|
|
if (new_topic === undefined && new_stream_id === undefined) {
|
2017-08-10 20:27:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unread_messages.has(msg.id)) {
|
|
|
|
return;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2017-08-10 20:27:23 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.unread_topic_counter.delete(msg.id);
|
2017-08-10 20:27:23 +02:00
|
|
|
|
|
|
|
exports.unread_topic_counter.add(
|
2020-04-07 22:29:22 +02:00
|
|
|
new_stream_id || msg.stream_id,
|
|
|
|
new_topic || msg.topic,
|
2020-07-02 02:16:03 +02:00
|
|
|
msg.id,
|
2017-08-10 20:27:23 +02:00
|
|
|
);
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.process_loaded_messages = function (messages) {
|
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 message of messages) {
|
2017-12-16 21:42:41 +01:00
|
|
|
if (!message.unread) {
|
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
|
|
|
continue;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 19:54:38 +02:00
|
|
|
unread_messages.add(message.id);
|
2017-08-03 22:01:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "private") {
|
2017-07-31 14:59:18 +02:00
|
|
|
exports.unread_pm_counter.add(message);
|
2013-09-27 23:20:52 +02:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "stream") {
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.unread_topic_counter.add(message.stream_id, message.topic, message.id);
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-05-30 22:15:59 +02:00
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
exports.update_message_for_mention(message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_message_for_mention = function (message) {
|
|
|
|
if (!message.unread) {
|
|
|
|
exports.unread_mentions_counter.delete(message.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const is_unmuted_mention =
|
2020-07-15 01:29:15 +02:00
|
|
|
message.type === "stream" &&
|
2020-04-11 02:51:45 +02:00
|
|
|
message.mentioned &&
|
|
|
|
!muting.is_topic_muted(message.stream_id, message.topic);
|
|
|
|
|
|
|
|
if (is_unmuted_mention || message.mentioned_me_directly) {
|
|
|
|
exports.unread_mentions_counter.add(message.id);
|
|
|
|
} else {
|
|
|
|
exports.unread_mentions_counter.delete(message.id);
|
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
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
exports.mark_as_read = function (message_id) {
|
|
|
|
// We don't need to check anything about the message, since all
|
|
|
|
// the following methods are cheap and work fine even if message_id
|
|
|
|
// was never set to unread.
|
2020-02-03 07:41:38 +01:00
|
|
|
exports.unread_pm_counter.delete(message_id);
|
|
|
|
exports.unread_topic_counter.delete(message_id);
|
2020-02-01 03:13:57 +01:00
|
|
|
exports.unread_mentions_counter.delete(message_id);
|
2020-02-01 03:17:09 +01:00
|
|
|
unread_messages.delete(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
if (message) {
|
|
|
|
message.unread = false;
|
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.declare_bankruptcy = function () {
|
2017-07-31 14:59:18 +02:00
|
|
|
exports.unread_pm_counter.clear();
|
2016-11-30 03:20:29 +01:00
|
|
|
exports.unread_topic_counter.clear();
|
2017-08-01 14:04:48 +02:00
|
|
|
exports.unread_mentions_counter.clear();
|
2017-08-03 22:01:21 +02:00
|
|
|
unread_messages.clear();
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_counts = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = {};
|
2013-05-22 18:06:40 +02:00
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
// Return a data structure with various counts. This function should be
|
|
|
|
// pretty cheap, even if you don't care about all the counts, and you
|
|
|
|
// should strive to keep it free of side effects on globals or DOM.
|
|
|
|
res.private_message_count = 0;
|
2020-02-01 03:13:57 +01:00
|
|
|
res.mentioned_message_count = exports.unread_mentions_counter.size;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2017-07-31 14:04:20 +02:00
|
|
|
// This sets stream_count, topic_count, and home_unread_messages
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_res = exports.unread_topic_counter.get_counts();
|
2016-12-15 01:53:33 +01:00
|
|
|
res.home_unread_messages = topic_res.stream_unread_messages;
|
|
|
|
res.stream_count = topic_res.stream_count;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_res = exports.unread_pm_counter.get_counts();
|
2017-07-31 14:59:18 +02:00
|
|
|
res.pm_count = pm_res.pm_dict;
|
|
|
|
res.private_message_count = pm_res.total_count;
|
|
|
|
res.home_unread_messages += pm_res.total_count;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2019-07-20 14:45:56 +02:00
|
|
|
// Saves us from calling to get_counts() when we can avoid it.
|
|
|
|
exports.calculate_notifiable_count = function (res) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let new_message_count = 0;
|
2019-07-20 14:45:56 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const only_show_notifiable =
|
|
|
|
page_params.desktop_icon_count_display ===
|
2019-07-20 14:45:56 +02:00
|
|
|
settings_notifications.desktop_icon_count_display_values.notifiable.code;
|
2020-07-15 00:34:28 +02:00
|
|
|
const no_notifications =
|
|
|
|
page_params.desktop_icon_count_display ===
|
2019-08-19 23:33:11 +02:00
|
|
|
settings_notifications.desktop_icon_count_display_values.none.code;
|
2019-07-20 14:45:56 +02:00
|
|
|
if (only_show_notifiable) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_NOTIFIABLE
|
|
|
|
new_message_count = res.mentioned_message_count + res.private_message_count;
|
2019-08-19 23:33:11 +02:00
|
|
|
} else if (no_notifications) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_NONE
|
|
|
|
new_message_count = 0;
|
2019-07-20 14:45:56 +02:00
|
|
|
} else {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_MESSAGES
|
|
|
|
new_message_count = res.home_unread_messages;
|
|
|
|
}
|
|
|
|
return new_message_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_notifiable_count = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = exports.get_counts();
|
2019-07-20 14:45:56 +02:00
|
|
|
return exports.calculate_notifiable_count(res);
|
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
exports.num_unread_for_stream = function (stream_id) {
|
|
|
|
return exports.unread_topic_counter.get_stream_count(stream_id);
|
2017-01-15 16:44:33 +01:00
|
|
|
};
|
|
|
|
|
2018-12-22 15:20:54 +01:00
|
|
|
exports.num_unread_for_topic = function (stream_id, topic_name) {
|
|
|
|
return exports.unread_topic_counter.get(stream_id, topic_name);
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
exports.topic_has_any_unread = function (stream_id, topic) {
|
|
|
|
return exports.unread_topic_counter.topic_has_any_unread(stream_id, topic);
|
2017-04-21 18:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-11-18 17:02:06 +01:00
|
|
|
exports.num_unread_for_person = function (user_ids_string) {
|
2017-07-31 14:59:18 +02:00
|
|
|
return exports.unread_pm_counter.num_unread(user_ids_string);
|
2013-08-22 19:06:04 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 21:22:49 +02:00
|
|
|
exports.get_msg_ids_for_stream = function (stream_id) {
|
|
|
|
return exports.unread_topic_counter.get_msg_ids_for_stream(stream_id);
|
|
|
|
};
|
|
|
|
|
2018-12-22 15:20:54 +01:00
|
|
|
exports.get_msg_ids_for_topic = function (stream_id, topic_name) {
|
|
|
|
return exports.unread_topic_counter.get_msg_ids_for_topic(stream_id, topic_name);
|
2018-04-25 22:48:51 +02:00
|
|
|
};
|
|
|
|
|
2018-05-02 13:02:32 +02:00
|
|
|
exports.get_msg_ids_for_person = function (user_ids_string) {
|
2018-05-02 19:50:25 +02:00
|
|
|
return exports.unread_pm_counter.get_msg_ids_for_person(user_ids_string);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_msg_ids_for_private = function () {
|
|
|
|
return exports.unread_pm_counter.get_msg_ids();
|
2018-05-02 13:02:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-02 19:54:36 +02:00
|
|
|
exports.get_msg_ids_for_mentions = function () {
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(exports.unread_mentions_counter);
|
2018-05-02 19:54:36 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-05-10 13:11:53 +02:00
|
|
|
exports.get_all_msg_ids = function () {
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(unread_messages);
|
2018-05-10 13:11:53 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
exports.get_missing_topics = function (opts) {
|
|
|
|
return exports.unread_topic_counter.get_missing_topics(opts);
|
|
|
|
};
|
|
|
|
|
2018-05-03 22:57:07 +02:00
|
|
|
exports.get_msg_ids_for_starred = function () {
|
|
|
|
// This is here for API consistency sake--we never
|
|
|
|
// have unread starred messages. (Some day we may ironically
|
|
|
|
// want to make starring the same as mark-as-unread, but
|
|
|
|
// for now starring === reading.)
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2019-07-18 22:29:07 +02:00
|
|
|
exports.initialize = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const unread_msgs = page_params.unread_msgs;
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
exports.unread_pm_counter.set_huddles(unread_msgs.huddles);
|
|
|
|
exports.unread_pm_counter.set_pms(unread_msgs.pms);
|
|
|
|
exports.unread_topic_counter.set_streams(unread_msgs.streams);
|
|
|
|
|
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 message_id of unread_msgs.mentions) {
|
|
|
|
exports.unread_mentions_counter.add(message_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const obj of unread_msgs.huddles) {
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of obj.unread_message_ids) {
|
|
|
|
unread_messages.add(message_id);
|
|
|
|
}
|
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 obj of unread_msgs.pms) {
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of obj.unread_message_ids) {
|
|
|
|
unread_messages.add(message_id);
|
|
|
|
}
|
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 obj of unread_msgs.streams) {
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of obj.unread_message_ids) {
|
|
|
|
unread_messages.add(message_id);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const message_id of unread_msgs.mentions) {
|
|
|
|
unread_messages.add(message_id);
|
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.unread = exports;
|