2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
import render_muted_topic_ui_row from "../templates/muted_topic_ui_row.hbs";
|
|
|
|
import render_topic_muted from "../templates/topic_muted.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as channel from "./channel";
|
|
|
|
import * as feedback_widget from "./feedback_widget";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-02-28 00:57:45 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as muting from "./muting";
|
2021-02-28 01:03:46 +01:00
|
|
|
import * as overlays from "./overlays";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as recent_topics from "./recent_topics";
|
2021-03-27 11:39:36 +01:00
|
|
|
import * as settings_muted_topics from "./settings_muted_topics";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-02-28 01:02:37 +01:00
|
|
|
import * as stream_popover from "./stream_popover";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as unread_ui from "./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-02-28 00:41:32 +01:00
|
|
|
export function rerender_on_topic_update() {
|
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-03-28 09:32:21 +02:00
|
|
|
recent_topics.complete_rerender();
|
2021-03-27 11:37:31 +01:00
|
|
|
message_lists.current.update_topic_muting_and_rerender();
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current !== message_lists.home) {
|
2021-03-27 11:37:31 +01:00
|
|
|
message_lists.home.update_topic_muting_and_rerender();
|
2013-09-27 16:26:04 +02:00
|
|
|
}
|
2021-03-27 11:39:36 +01:00
|
|
|
if (overlays.settings_open() && settings_muted_topics.loaded) {
|
2021-02-28 00:41:32 +01:00
|
|
|
set_up_muted_topics_ui();
|
2020-07-12 00:13:47 +02:00
|
|
|
}
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2013-09-27 16:26:04 +02:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function persist_topic_mute(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-02-28 00:41:32 +01:00
|
|
|
}
|
2017-08-29 21:40:38 +02:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function persist_topic_unmute(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-02-28 00:41:32 +01:00
|
|
|
}
|
2013-09-10 20:07:24 +02:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function handle_topic_updates(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;
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
update_muted_topics(muted_topics);
|
|
|
|
rerender_on_topic_update();
|
|
|
|
}
|
2013-09-11 00:36:21 +02:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function update_muted_topics(muted_topics) {
|
2017-02-11 09:17:19 +01:00
|
|
|
muting.set_muted_topics(muted_topics);
|
2017-02-11 09:55:18 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2017-02-11 09:17:19 +01:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function set_up_muted_topics_ui() {
|
2020-04-22 23:22:26 +02:00
|
|
|
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
|
|
|
});
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2016-11-26 04:39:53 +01:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function mute_topic(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-02-28 00:41:32 +01:00
|
|
|
rerender_on_topic_update();
|
|
|
|
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-02-28 00:41:32 +01:00
|
|
|
unmute_topic(stream_id, topic);
|
2018-12-20 18:04:46 +01:00
|
|
|
},
|
2021-04-13 06:51:54 +02:00
|
|
|
title_text: $t({defaultMessage: "Topic muted"}),
|
|
|
|
undo_button_text: $t({defaultMessage: "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);
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2017-03-23 06:57:36 +01:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function unmute_topic(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-02-28 00:41:32 +01:00
|
|
|
rerender_on_topic_update();
|
|
|
|
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);
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2017-03-23 06:57:36 +01:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function toggle_topic_mute(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-02-28 00:41:32 +01:00
|
|
|
unmute_topic(stream_id, topic);
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (message.type === "stream") {
|
2021-02-28 00:41:32 +01:00
|
|
|
mute_topic(stream_id, topic);
|
2017-03-24 23:25:04 +01:00
|
|
|
}
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2021-03-27 12:42:16 +01:00
|
|
|
|
|
|
|
export function handle_user_updates(muted_user_ids) {
|
|
|
|
muting.set_muted_users(muted_user_ids);
|
|
|
|
}
|