2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:41:04 +01:00
|
|
|
import {FoldDict} from "./fold_dict";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 01:14:36 +01:00
|
|
|
import * as timerender from "./timerender";
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-02-03 09:42:50 +01:00
|
|
|
const muted_topics = new Map();
|
2021-03-27 12:42:16 +01:00
|
|
|
const muted_users = new Map();
|
2013-08-20 22:05:56 +02:00
|
|
|
|
2021-03-28 08:28:05 +02:00
|
|
|
function get_time_from_date_muted(date_muted) {
|
|
|
|
if (date_muted === undefined) {
|
|
|
|
return Date.now();
|
|
|
|
}
|
|
|
|
return date_muted * 1000;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
export function add_muted_topic(stream_id, topic, date_muted) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let sub_dict = muted_topics.get(stream_id);
|
2013-08-20 22:05:56 +02:00
|
|
|
if (!sub_dict) {
|
2019-12-26 15:34:17 +01:00
|
|
|
sub_dict = new FoldDict();
|
2018-12-13 22:26:10 +01:00
|
|
|
muted_topics.set(stream_id, sub_dict);
|
2013-08-20 22:05:56 +02:00
|
|
|
}
|
2021-03-28 08:28:05 +02:00
|
|
|
const time = get_time_from_date_muted(date_muted);
|
2020-02-05 13:14:24 +01:00
|
|
|
sub_dict.set(topic, time);
|
2021-02-28 00:41:04 +01:00
|
|
|
}
|
2013-08-20 22:05:56 +02:00
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
export function remove_muted_topic(stream_id, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_dict = muted_topics.get(stream_id);
|
2013-09-09 20:33:25 +02:00
|
|
|
if (sub_dict) {
|
2020-02-03 07:41:38 +01:00
|
|
|
sub_dict.delete(topic);
|
2013-09-09 20:33:25 +02:00
|
|
|
}
|
2021-02-28 00:41:04 +01:00
|
|
|
}
|
2013-09-09 20:33:25 +02:00
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
export function is_topic_muted(stream_id, topic) {
|
2018-12-13 22:26:10 +01:00
|
|
|
if (stream_id === undefined) {
|
2013-09-10 22:42:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_dict = muted_topics.get(stream_id);
|
2021-04-30 07:44:43 +02:00
|
|
|
return (sub_dict && sub_dict.get(topic)) || false;
|
2021-02-28 00:41:04 +01:00
|
|
|
}
|
2013-08-20 22:05:56 +02:00
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
export function get_muted_topics() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const topics = [];
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [stream_id, sub_dict] of muted_topics) {
|
2020-02-05 13:14:24 +01:00
|
|
|
const stream = stream_data.maybe_get_stream_name(stream_id);
|
2020-02-03 08:42:48 +01:00
|
|
|
for (const topic of sub_dict.keys()) {
|
2020-02-05 13:14:24 +01:00
|
|
|
const date_muted = sub_dict.get(topic);
|
2021-02-05 21:20:14 +01:00
|
|
|
const date_muted_str = timerender.render_now(new Date(date_muted)).time_str;
|
2020-02-05 13:14:24 +01:00
|
|
|
topics.push({
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
|
|
|
stream,
|
|
|
|
topic,
|
|
|
|
date_muted,
|
|
|
|
date_muted_str,
|
2020-02-05 13:14:24 +01:00
|
|
|
});
|
2020-02-03 08:42:48 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2013-09-10 17:01:38 +02:00
|
|
|
return topics;
|
2021-02-28 00:41:04 +01:00
|
|
|
}
|
2013-09-10 17:01:38 +02:00
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
export function set_muted_topics(tuples) {
|
2020-02-01 04:10:14 +01:00
|
|
|
muted_topics.clear();
|
2013-09-10 20:34:41 +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 tuple of tuples) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = tuple[0];
|
|
|
|
const topic = tuple[1];
|
2020-02-05 13:14:24 +01:00
|
|
|
const date_muted = tuple[2];
|
2013-09-10 20:34:41 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = stream_data.get_stream_id(stream_name);
|
2018-12-13 22:26:10 +01:00
|
|
|
|
|
|
|
if (!stream_id) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("Unknown stream in set_muted_topics: " + stream_name);
|
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;
|
2018-12-13 22:26:10 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
add_muted_topic(stream_id, topic, date_muted);
|
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
|
|
|
}
|
2021-02-28 00:41:04 +01:00
|
|
|
}
|
2018-12-15 19:53:20 +01:00
|
|
|
|
2021-03-27 12:42:16 +01:00
|
|
|
export function add_muted_user(user_id, date_muted) {
|
|
|
|
const time = get_time_from_date_muted(date_muted);
|
|
|
|
if (user_id) {
|
|
|
|
muted_users.set(user_id, time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove_muted_user(user_id) {
|
|
|
|
if (user_id) {
|
|
|
|
muted_users.delete(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function is_user_muted(user_id) {
|
|
|
|
if (user_id === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return muted_users.has(user_id);
|
|
|
|
}
|
|
|
|
|
2021-05-05 05:27:06 +02:00
|
|
|
export function filter_muted_user_ids(user_ids) {
|
|
|
|
// Returns a copy of the user ID list, after removing muted user IDs.
|
|
|
|
const base_user_ids = [...user_ids];
|
|
|
|
return base_user_ids.filter((user_id) => !is_user_muted(user_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function filter_muted_users(persons) {
|
|
|
|
// Returns a copy of the people list, after removing muted users.
|
|
|
|
const base_users = [...persons];
|
|
|
|
return base_users.filter((person) => !is_user_muted(person.user_id));
|
|
|
|
}
|
|
|
|
|
2021-03-27 12:42:16 +01:00
|
|
|
export function get_muted_users() {
|
|
|
|
const users = [];
|
|
|
|
for (const [id, date_muted] of muted_users) {
|
|
|
|
const date_muted_str = timerender.render_now(new Date(date_muted)).time_str;
|
|
|
|
users.push({
|
|
|
|
id,
|
|
|
|
date_muted,
|
|
|
|
date_muted_str,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function set_muted_users(list) {
|
|
|
|
muted_users.clear();
|
|
|
|
|
|
|
|
for (const user of list) {
|
|
|
|
if (user !== undefined && user.id !== undefined) {
|
|
|
|
add_muted_user(user.id, user.timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:41:04 +01:00
|
|
|
export function initialize() {
|
|
|
|
set_muted_topics(page_params.muted_topics);
|
2021-03-27 12:42:16 +01:00
|
|
|
set_muted_users(page_params.muted_users);
|
2021-02-28 00:41:04 +01:00
|
|
|
}
|