zulip/static/js/user_topics.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

import * as blueslip from "./blueslip";
import {FoldDict} from "./fold_dict";
import {page_params} from "./page_params";
import * as stream_data from "./stream_data";
import * as timerender from "./timerender";
import {get_time_from_date_muted} from "./util";
const muted_topics = new Map();
export const visibility_policy = {
VISIBILITY_POLICY_INHERIT: 0,
MUTED: 1,
UNMUTED: 2,
FOLLOWED: 3,
};
export function add_muted_topic(stream_id, topic, date_muted) {
let sub_dict = muted_topics.get(stream_id);
if (!sub_dict) {
sub_dict = new FoldDict();
muted_topics.set(stream_id, sub_dict);
}
const time = get_time_from_date_muted(date_muted);
sub_dict.set(topic, time);
}
export function remove_muted_topic(stream_id, topic) {
const sub_dict = muted_topics.get(stream_id);
if (sub_dict) {
sub_dict.delete(topic);
}
}
export function is_topic_muted(stream_id, topic) {
if (stream_id === undefined) {
return false;
}
const sub_dict = muted_topics.get(stream_id);
return (sub_dict && sub_dict.get(topic)) || false;
}
export function get_muted_topics() {
const topics = [];
for (const [stream_id, sub_dict] of muted_topics) {
const stream = stream_data.maybe_get_stream_name(stream_id);
for (const topic of sub_dict.keys()) {
const date_muted = sub_dict.get(topic);
const date_muted_str = timerender.render_now(new Date(date_muted)).time_str;
topics.push({
stream_id,
stream,
topic,
date_muted,
date_muted_str,
});
}
}
return topics;
}
export function set_user_topic(user_topic) {
const stream_id = user_topic.stream_id;
const topic = user_topic.topic_name;
const date_muted = user_topic.last_updated;
const stream_name = stream_data.maybe_get_stream_name(stream_id);
if (!stream_name) {
blueslip.warn("Unknown stream ID in set_user_topic: " + stream_id);
return;
}
switch (user_topic.visibility_policy) {
case visibility_policy.MUTED:
add_muted_topic(stream_id, topic, date_muted);
break;
case visibility_policy.VISIBILITY_POLICY_INHERIT:
remove_muted_topic(stream_id, topic);
break;
}
}
export function set_user_topics(user_topics) {
muted_topics.clear();
for (const user_topic of user_topics) {
set_user_topic(user_topic);
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
}
}
export function initialize() {
set_user_topics(page_params.user_topics);
}