2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const render_muted_topic_ui_row = require("../templates/muted_topic_ui_row.hbs");
|
|
|
|
const render_topic_muted = require("../templates/topic_muted.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
const channel = require("./channel");
|
2021-02-10 16:47:06 +01:00
|
|
|
const feedback_widget = require("./feedback_widget");
|
2021-02-28 00:41:04 +01:00
|
|
|
const muting = require("./muting");
|
2021-02-10 17:06:24 +01:00
|
|
|
const settings_muting = require("./settings_muting");
|
2021-02-28 00:40:18 +01:00
|
|
|
const unread_ui = require("./unread_ui");
|
2021-02-10 16:47:06 +01:00
|
|
|
|
2013-09-11 17:27:43 +02:00
|
|
|
function timestamp_ms() {
|
2020-12-22 11:54:49 +01:00
|
|
|
return Date.now();
|
2013-09-11 17:27:43 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let last_topic_update = 0;
|
2013-09-11 17:27:43 +02:00
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.rerender_on_topic_update = function () {
|
2017-08-29 21:40:38 +02:00
|
|
|
// Note: We tend to optimistically rerender muting preferences before
|
2020-09-19 01:39:07 +02:00
|
|
|
// the backend actually acknowledges the mute. This gives a more
|
|
|
|
// immediate feel to the user, and if the backend fails temporarily,
|
2017-08-29 21:40:38 +02:00
|
|
|
// re-doing a mute or unmute is a pretty recoverable thing.
|
|
|
|
|
2013-09-30 22:03:10 +02:00
|
|
|
stream_list.update_streams_sidebar();
|
2021-01-25 06:23:16 +01:00
|
|
|
if (current_msg_list.excludes_muted_topics) {
|
2018-04-27 17:35:02 +02:00
|
|
|
current_msg_list.update_muting_and_rerender();
|
|
|
|
}
|
2013-09-27 16:26:04 +02:00
|
|
|
if (current_msg_list !== home_msg_list) {
|
2018-04-27 17:35:02 +02:00
|
|
|
home_msg_list.update_muting_and_rerender();
|
2013-09-27 16:26:04 +02:00
|
|
|
}
|
2020-07-12 00:13:47 +02:00
|
|
|
if (overlays.settings_open() && settings_muting.loaded) {
|
|
|
|
exports.set_up_muted_topics_ui();
|
|
|
|
}
|
2013-09-27 16:26:04 +02:00
|
|
|
};
|
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.persist_topic_mute = function (stream_id, topic_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2017-08-29 21:40:38 +02:00
|
|
|
topic: topic_name,
|
2020-07-15 01:29:15 +02:00
|
|
|
op: "add",
|
2013-09-10 20:07:24 +02:00
|
|
|
};
|
2013-09-11 17:27:43 +02:00
|
|
|
last_topic_update = timestamp_ms();
|
2017-08-29 21:40:38 +02:00
|
|
|
channel.patch({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/subscriptions/muted_topics",
|
2017-08-29 21:40:38 +02:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2017-08-29 21:40:38 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.persist_topic_unmute = function (stream_id, topic_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2017-08-29 21:40:38 +02:00
|
|
|
topic: topic_name,
|
2020-07-15 01:29:15 +02:00
|
|
|
op: "remove",
|
2017-08-29 21:40:38 +02:00
|
|
|
};
|
|
|
|
last_topic_update = timestamp_ms();
|
|
|
|
channel.patch({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/subscriptions/muted_topics",
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2013-09-10 20:07:24 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.handle_topic_updates = function (muted_topics) {
|
2013-09-11 17:27:43 +02:00
|
|
|
if (timestamp_ms() < last_topic_update + 1000) {
|
|
|
|
// This topic update is either the one that we just rendered, or,
|
|
|
|
// much less likely, it's coming from another device and would probably
|
|
|
|
// be overwriting this device's preferences with stale data.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-11 09:17:19 +01:00
|
|
|
exports.update_muted_topics(muted_topics);
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.rerender_on_topic_update();
|
2013-09-11 00:36:21 +02:00
|
|
|
};
|
|
|
|
|
2017-02-11 09:17:19 +01:00
|
|
|
exports.update_muted_topics = function (muted_topics) {
|
|
|
|
muting.set_muted_topics(muted_topics);
|
2017-02-11 09:55:18 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2017-02-11 09:17:19 +01:00
|
|
|
};
|
|
|
|
|
2020-04-22 23:22:26 +02:00
|
|
|
exports.set_up_muted_topics_ui = function () {
|
|
|
|
const muted_topics = muting.get_muted_topics();
|
|
|
|
const muted_topics_table = $("#muted_topics_table");
|
2020-02-05 13:14:24 +01:00
|
|
|
const $search_input = $("#muted_topics_search");
|
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-01-29 10:27:56 +01:00
|
|
|
ListWidget.create(muted_topics_table, muted_topics, {
|
2020-02-05 13:14:24 +01:00
|
|
|
name: "muted-topics-list",
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(muted_topics) {
|
|
|
|
return render_muted_topic_ui_row({muted_topics});
|
2020-02-05 13:14:24 +01:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $search_input,
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, value) {
|
2020-10-07 10:48:54 +02:00
|
|
|
return item.topic.toLocaleLowerCase().includes(value);
|
2020-02-05 13:14:24 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
onupdate() {
|
2020-02-05 13:14:24 +01:00
|
|
|
ui.reset_scrollbar(muted_topics_table.closest(".progressive-table-wrapper"));
|
|
|
|
},
|
|
|
|
},
|
2020-07-15 01:29:15 +02:00
|
|
|
parent_container: $("#muted-topic-settings"),
|
|
|
|
simplebar_container: $("#muted-topic-settings .progressive-table-wrapper"),
|
2020-02-05 13:14:24 +01:00
|
|
|
});
|
2016-11-26 04:39:53 +01:00
|
|
|
};
|
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.mute_topic = function (stream_id, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = stream_data.maybe_get_stream_name(stream_id);
|
2018-12-20 18:04:46 +01:00
|
|
|
|
2017-03-23 06:57:36 +01:00
|
|
|
stream_popover.hide_topic_popover();
|
2018-12-13 22:26:10 +01:00
|
|
|
muting.add_muted_topic(stream_id, topic);
|
2017-10-18 01:40:19 +02:00
|
|
|
unread_ui.update_unread_counts();
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.rerender_on_topic_update();
|
|
|
|
exports.persist_topic_mute(stream_id, topic);
|
2018-12-21 15:58:28 +01:00
|
|
|
feedback_widget.show({
|
2020-07-20 22:18:43 +02:00
|
|
|
populate(container) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered_html = render_topic_muted();
|
2018-12-21 00:36:13 +01:00
|
|
|
container.html(rendered_html);
|
2018-12-20 18:04:46 +01:00
|
|
|
container.find(".stream").text(stream_name);
|
|
|
|
container.find(".topic").text(topic);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on_undo() {
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.unmute_topic(stream_id, topic);
|
2018-12-20 18:04:46 +01:00
|
|
|
},
|
2018-12-20 19:48:37 +01:00
|
|
|
title_text: i18n.t("Topic muted"),
|
|
|
|
undo_button_text: i18n.t("Unmute"),
|
2018-12-20 18:04:46 +01:00
|
|
|
});
|
2020-05-29 17:22:53 +02:00
|
|
|
recent_topics.update_topic_is_muted(stream_id, topic);
|
2017-03-23 06:57:36 +01:00
|
|
|
};
|
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.unmute_topic = function (stream_id, topic) {
|
2017-03-23 06:57:36 +01:00
|
|
|
// we don't run a unmute_notify function because it isn't an issue as much
|
|
|
|
// if someone accidentally unmutes a stream rather than if they mute it
|
|
|
|
// and miss out on info.
|
|
|
|
stream_popover.hide_topic_popover();
|
2018-12-13 22:26:10 +01:00
|
|
|
muting.remove_muted_topic(stream_id, topic);
|
2017-10-18 01:40:19 +02:00
|
|
|
unread_ui.update_unread_counts();
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.rerender_on_topic_update();
|
|
|
|
exports.persist_topic_unmute(stream_id, topic);
|
2018-12-21 15:58:28 +01:00
|
|
|
feedback_widget.dismiss();
|
2020-05-29 17:22:53 +02:00
|
|
|
recent_topics.update_topic_is_muted(stream_id, topic);
|
2017-03-23 06:57:36 +01:00
|
|
|
};
|
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.toggle_topic_mute = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = message.stream_id;
|
2020-02-19 00:04:12 +01:00
|
|
|
const topic = message.topic;
|
2018-12-22 19:18:59 +01:00
|
|
|
|
|
|
|
if (muting.is_topic_muted(stream_id, topic)) {
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.unmute_topic(stream_id, topic);
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (message.type === "stream") {
|
2021-01-03 12:28:09 +01:00
|
|
|
exports.mute_topic(stream_id, topic);
|
2017-03-24 23:25:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.muting_ui = exports;
|