2019-11-02 00:06:25 +01: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
|
|
|
|
2013-09-11 17:27:43 +02:00
|
|
|
function timestamp_ms() {
|
2019-11-09 02:48:01 +01:00
|
|
|
return new Date().getTime();
|
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
|
|
|
|
2013-09-27 16:26:04 +02:00
|
|
|
exports.rerender = function () {
|
2017-08-29 21:40:38 +02:00
|
|
|
// Note: We tend to optimistically rerender muting preferences before
|
|
|
|
// the back end actually acknowledges the mute. This gives a more
|
|
|
|
// immediate feel to the user, and if the back end fails temporarily,
|
|
|
|
// re-doing a mute or unmute is a pretty recoverable thing.
|
|
|
|
|
2013-09-30 22:03:10 +02:00
|
|
|
stream_list.update_streams_sidebar();
|
2018-04-27 17:35:02 +02:00
|
|
|
if (current_msg_list.muting_enabled) {
|
|
|
|
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-04-22 23:22:26 +02:00
|
|
|
exports.set_up_muted_topics_ui();
|
2013-09-27 16:26:04 +02:00
|
|
|
};
|
|
|
|
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_mute = function (stream_id, topic_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2018-12-24 18:30:36 +01:00
|
|
|
stream_id: stream_id,
|
2017-08-29 21:40:38 +02:00
|
|
|
topic: topic_name,
|
|
|
|
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({
|
|
|
|
url: '/json/users/me/subscriptions/muted_topics',
|
|
|
|
idempotent: true,
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_unmute = function (stream_id, topic_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2018-12-24 18:30:36 +01:00
|
|
|
stream_id: stream_id,
|
2017-08-29 21:40:38 +02:00
|
|
|
topic: topic_name,
|
|
|
|
op: 'remove',
|
|
|
|
};
|
|
|
|
last_topic_update = timestamp_ms();
|
|
|
|
channel.patch({
|
2017-01-10 01:23:53 +01:00
|
|
|
url: '/json/users/me/subscriptions/muted_topics',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
data: data,
|
2013-09-10 20:07:24 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-11 00:36:21 +02:00
|
|
|
exports.handle_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);
|
2013-09-27 16:26:04 +02:00
|
|
|
exports.rerender();
|
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
|
|
|
|
2020-02-05 13:14:24 +01:00
|
|
|
list_render.create(muted_topics_table, muted_topics, {
|
|
|
|
name: "muted-topics-list",
|
|
|
|
modifier: function (muted_topics) {
|
|
|
|
return render_muted_topic_ui_row({ muted_topics: muted_topics });
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $search_input,
|
|
|
|
predicate: function (item, value) {
|
|
|
|
return item.topic.toLocaleLowerCase().indexOf(value) >= 0;
|
|
|
|
},
|
|
|
|
onupdate: function () {
|
|
|
|
ui.reset_scrollbar(muted_topics_table.closest(".progressive-table-wrapper"));
|
|
|
|
},
|
|
|
|
},
|
2020-04-22 23:22:26 +02:00
|
|
|
parent_container: $('#muted-topic-settings'),
|
2020-02-05 13:14:24 +01:00
|
|
|
});
|
2016-11-26 04:39:53 +01:00
|
|
|
};
|
|
|
|
|
2018-12-14 17:20:35 +01:00
|
|
|
exports.mute = 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();
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.rerender();
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_mute(stream_id, topic);
|
2018-12-21 15:58:28 +01:00
|
|
|
feedback_widget.show({
|
2018-12-20 18:04:46 +01:00
|
|
|
populate: function (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);
|
|
|
|
},
|
|
|
|
on_undo: function () {
|
|
|
|
exports.unmute(stream_id, topic);
|
|
|
|
},
|
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-15 14:27:36 +02:00
|
|
|
recent_topics.update_topic_is_muted(stream_id, topic, true);
|
2017-03-23 06:57:36 +01:00
|
|
|
};
|
|
|
|
|
2018-12-14 17:20:35 +01:00
|
|
|
exports.unmute = 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();
|
2017-08-29 21:40:38 +02:00
|
|
|
exports.rerender();
|
2018-12-13 23:05:26 +01:00
|
|
|
exports.persist_unmute(stream_id, topic);
|
2018-12-21 15:58:28 +01:00
|
|
|
feedback_widget.dismiss();
|
2020-05-15 14:27:36 +02:00
|
|
|
recent_topics.update_topic_is_muted(stream_id, topic, false);
|
2017-03-23 06:57:36 +01:00
|
|
|
};
|
|
|
|
|
2018-12-22 19:18:59 +01:00
|
|
|
exports.toggle_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)) {
|
|
|
|
exports.unmute(stream_id, topic);
|
|
|
|
} else if (message.type === 'stream') {
|
|
|
|
exports.mute(stream_id, topic);
|
2017-03-24 23:25:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.muting_ui = exports;
|