Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

js: Convert static/js/muting.js to ES6 module.

Anders Kaseorg94a5da325cac
diff --git a/.eslintrc.json b/.eslintrc.json
index d7faa8913d3..fd207b9e3ec 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -181,7 +181,6 @@
"message_store": false,
"message_util": false,
"message_viewport": false,
- "muting": false,
"muting_ui": false,
"narrow": false,
"narrow_state": false,
diff --git a/frontend_tests/node_tests/message_events.js b/frontend_tests/node_tests/message_events.js
index 830c8efbb4a..262bd787fa8 100644
--- a/frontend_tests/node_tests/message_events.js
+++ b/frontend_tests/node_tests/message_events.js
@@ -22,7 +22,6 @@ rewiremock.enable();
const message_events = zrequire("message_events");
const message_store = zrequire("message_store");
-zrequire("muting");
const people = zrequire("people");
zrequire("recent_senders");
const stream_data = zrequire("stream_data");
diff --git a/frontend_tests/node_tests/narrow_activate.js b/frontend_tests/node_tests/narrow_activate.js
index 436ddb18f30..40cba4645be 100644
--- a/frontend_tests/node_tests/narrow_activate.js
+++ b/frontend_tests/node_tests/narrow_activate.js
@@ -58,7 +58,7 @@ set_global("setTimeout", (f, t) => {
f();
});
-set_global("muting", {
+rewiremock("../../static/js/muting").with({
is_topic_muted: () => false,
});
diff --git a/frontend_tests/node_tests/narrow_local.js b/frontend_tests/node_tests/narrow_local.js
index 3330f246193..d1e64c730cf 100644
--- a/frontend_tests/node_tests/narrow_local.js
+++ b/frontend_tests/node_tests/narrow_local.js
@@ -2,14 +2,18 @@
const {strict: assert} = require("assert");
+const rewiremock = require("rewiremock/node");
+
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const message_list = set_global("message_list", {});
-set_global("muting", {
+rewiremock("../../static/js/muting").with({
is_topic_muted: () => false,
});
+rewiremock.enable();
+
const Filter = zrequire("Filter", "js/filter");
const {MessageListData} = zrequire("MessageListData", "js/message_list_data");
const narrow_state = zrequire("narrow_state");
@@ -449,3 +453,4 @@ run_test("final corner case", () => {
test_with(fixture);
});
+rewiremock.disable();
diff --git a/frontend_tests/node_tests/narrow_unread.js b/frontend_tests/node_tests/narrow_unread.js
index c078f4a72f2..e5b3081846a 100644
--- a/frontend_tests/node_tests/narrow_unread.js
+++ b/frontend_tests/node_tests/narrow_unread.js
@@ -2,16 +2,20 @@
const {strict: assert} = require("assert");
+const rewiremock = require("rewiremock/node");
+
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const message_store = set_global("message_store", {});
set_global("page_params", {});
-set_global("muting", {
+rewiremock("../../static/js/muting").with({
is_topic_muted: () => false,
});
+rewiremock.enable();
+
const Filter = zrequire("Filter", "js/filter");
const people = zrequire("people");
const stream_data = zrequire("stream_data");
@@ -207,3 +211,4 @@ run_test("defensive code", () => {
flavor: "cannot_compute",
});
});
+rewiremock.disable();
diff --git a/frontend_tests/node_tests/recent_topics.js b/frontend_tests/node_tests/recent_topics.js
index 0691e3d37b4..5e38e5d433e 100644
--- a/frontend_tests/node_tests/recent_topics.js
+++ b/frontend_tests/node_tests/recent_topics.js
@@ -119,7 +119,7 @@ const topic8 = "topic-8";
const topic9 = "topic-9";
const topic10 = "topic-10";
-set_global("muting", {
+rewiremock("../../static/js/muting").with({
is_topic_muted: (stream_id, topic) => {
if (stream_id === stream1 && topic === topic7) {
return true;
diff --git a/frontend_tests/node_tests/topic_generator.js b/frontend_tests/node_tests/topic_generator.js
index 16bddfa0aec..c0849f7a36c 100644
--- a/frontend_tests/node_tests/topic_generator.js
+++ b/frontend_tests/node_tests/topic_generator.js
@@ -103,7 +103,7 @@ run_test("topics", () => {
unread.topic_has_any_unread = (stream_id) =>
[devel_stream_id, muted_stream_id].includes(stream_id);
- muting.is_topic_muted = (stream_name, topic) => topic === "muted";
+ muting.__Rewire__("is_topic_muted", (stream_name, topic) => topic === "muted");
let next_item = tg.get_next_topic("announce", "whatever");
assert.deepEqual(next_item, {
diff --git a/frontend_tests/node_tests/topic_list_data.js b/frontend_tests/node_tests/topic_list_data.js
index 8ca4bb4b6df..116681f4b56 100644
--- a/frontend_tests/node_tests/topic_list_data.js
+++ b/frontend_tests/node_tests/topic_list_data.js
@@ -3,6 +3,7 @@
const {strict: assert} = require("assert");
const _ = require("lodash");
+const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
@@ -11,13 +12,18 @@ const narrow_state = set_global("narrow_state", {
topic() {},
});
set_global("unread", {});
-const muting = set_global("muting", {
+const muting = {
+ __esModule: true,
+
is_topic_muted() {
return false;
},
-});
+};
+rewiremock("../../static/js/muting").with(muting);
set_global("message_list", {});
+rewiremock.enable();
+
zrequire("hash_util");
const stream_data = zrequire("stream_data");
const unread = zrequire("unread");
@@ -146,3 +152,4 @@ run_test("get_list_info unreads", (override) => {
["topic 0", "topic 1", "topic 2", "topic 3", "topic 5", "topic 6", "topic 7", "topic 8"],
);
});
+rewiremock.disable();
diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js
index 41caf5f211b..0fca55234f4 100644
--- a/frontend_tests/node_tests/ui_init.js
+++ b/frontend_tests/node_tests/ui_init.js
@@ -108,7 +108,6 @@ zrequire("stream_color");
zrequire("stream_edit");
zrequire("color_data");
zrequire("stream_data");
-zrequire("muting");
zrequire("condense");
zrequire("lightbox");
zrequire("overlays");
diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js
index 6afe3a8d413..f7fdb08bb6a 100644
--- a/static/js/bundles/app.js
+++ b/static/js/bundles/app.js
@@ -18,7 +18,6 @@ import "../fold_dict";
import "../input_pill";
import "../setup";
import "../unread_ops";
-import "../muting";
import "../muting_ui";
import "../message_viewport";
import "../rows";
diff --git a/static/js/global.d.ts b/static/js/global.d.ts
index 6f3fcbfaf96..843c8c2fc81 100644
--- a/static/js/global.d.ts
+++ b/static/js/global.d.ts
@@ -55,7 +55,6 @@ declare let message_scroll: any;
declare let message_store: any;
declare let message_util: any;
declare let message_viewport: any;
-declare let muting: any;
declare let muting_ui: any;
declare let narrow: any;
declare let narrow_state: any;
diff --git a/static/js/message_list_data.js b/static/js/message_list_data.js
index 84195a755b4..d0d6332ff61 100644
--- a/static/js/message_list_data.js
+++ b/static/js/message_list_data.js
@@ -1,6 +1,7 @@
import _ from "lodash";
import {FetchStatus} from "./fetch_status";
+import * as muting from "./muting";
import * as util from "./util";
export class MessageListData {
diff --git a/static/js/muting.js b/static/js/muting.js
index 58a80554df1..a01868555f8 100644
--- a/static/js/muting.js
+++ b/static/js/muting.js
@@ -1,10 +1,8 @@
-"use strict";
-
-const {FoldDict} = require("./fold_dict");
+import {FoldDict} from "./fold_dict";
const muted_topics = new Map();
-exports.add_muted_topic = function (stream_id, topic, date_muted) {
+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();
@@ -17,24 +15,24 @@ exports.add_muted_topic = function (stream_id, topic, date_muted) {
time = date_muted * 1000;
}
sub_dict.set(topic, time);
-};
+}
-exports.remove_muted_topic = function (stream_id, topic) {
+export function remove_muted_topic(stream_id, topic) {
const sub_dict = muted_topics.get(stream_id);
if (sub_dict) {
sub_dict.delete(topic);
}
-};
+}
-exports.is_topic_muted = function (stream_id, 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);
-};
+}
-exports.get_muted_topics = function () {
+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);
@@ -51,9 +49,9 @@ exports.get_muted_topics = function () {
}
}
return topics;
-};
+}
-exports.set_muted_topics = function (tuples) {
+export function set_muted_topics(tuples) {
muted_topics.clear();
for (const tuple of tuples) {
@@ -68,12 +66,10 @@ exports.set_muted_topics = function (tuples) {
continue;
}
- exports.add_muted_topic(stream_id, topic, date_muted);
+ add_muted_topic(stream_id, topic, date_muted);
}
-};
-
-exports.initialize = function () {
- exports.set_muted_topics(page_params.muted_topics);
-};
+}
-window.muting = exports;
+export function initialize() {
+ set_muted_topics(page_params.muted_topics);
+}
diff --git a/static/js/muting_ui.js b/static/js/muting_ui.js
index a33c8db35f2..6d5c8090628 100644
--- a/static/js/muting_ui.js
+++ b/static/js/muting_ui.js
@@ -5,6 +5,7 @@ const render_topic_muted = require("../templates/topic_muted.hbs");
const channel = require("./channel");
const feedback_widget = require("./feedback_widget");
+const muting = require("./muting");
const settings_muting = require("./settings_muting");
const unread_ui = require("./unread_ui");
diff --git a/static/js/notifications.js b/static/js/notifications.js
index 498f4199b61..2a7ca6073bf 100644
--- a/static/js/notifications.js
+++ b/static/js/notifications.js
@@ -8,6 +8,7 @@ const render_notification = require("../templates/notification.hbs");
const alert_words = require("./alert_words");
const channel = require("./channel");
const favicon = require("./favicon");
+const muting = require("./muting");
const people = require("./people");
const settings_config = require("./settings_config");
const spoilers = require("./spoilers");
diff --git a/static/js/popovers.js b/static/js/popovers.js
index b3830dd2b65..fe9f1b12c06 100644
--- a/static/js/popovers.js
+++ b/static/js/popovers.js
@@ -18,6 +18,7 @@ const render_user_profile_modal = require("../templates/user_profile_modal.hbs")
const feature_flags = require("./feature_flags");
const message_edit_history = require("./message_edit_history");
+const muting = require("./muting");
const people = require("./people");
const settings_config = require("./settings_config");
const settings_data = require("./settings_data");
diff --git a/static/js/recent_topics.js b/static/js/recent_topics.js
index b20844a37df..c08e480cbac 100644
--- a/static/js/recent_topics.js
+++ b/static/js/recent_topics.js
@@ -6,6 +6,7 @@ const render_recent_topics_body = require("../templates/recent_topics_table.hbs"
const drafts = require("./drafts");
const {localstorage} = require("./localstorage");
+const muting = require("./muting");
const people = require("./people");
const topics = new Map(); // Key is stream-id:topic.
diff --git a/static/js/stream_popover.js b/static/js/stream_popover.js
index 07423f60535..81602025507 100644
--- a/static/js/stream_popover.js
+++ b/static/js/stream_popover.js
@@ -9,6 +9,7 @@ const render_topic_sidebar_actions = require("../templates/topic_sidebar_actions
const render_unstar_messages_modal = require("../templates/unstar_messages_modal.hbs");
const channel = require("./channel");
+const muting = require("./muting");
// We handle stream popovers and topic popovers in this
// module. Both are popped up from the left sidebar.
diff --git a/static/js/topic_generator.js b/static/js/topic_generator.js
index a2f06daa168..6ad7b6727b5 100644
--- a/static/js/topic_generator.js
+++ b/static/js/topic_generator.js
@@ -1,3 +1,4 @@
+import * as muting from "./muting";
import * as pm_conversations from "./pm_conversations";
import * as stream_sort from "./stream_sort";
diff --git a/static/js/topic_list_data.js b/static/js/topic_list_data.js
index 6e5290f02b2..76c6f93f9a6 100644
--- a/static/js/topic_list_data.js
+++ b/static/js/topic_list_data.js
@@ -1,3 +1,5 @@
+import * as muting from "./muting";
+
const max_topics = 5;
const max_topics_with_unread = 8;
diff --git a/static/js/ui_init.js b/static/js/ui_init.js
index 7d136b50db7..61be05094b6 100644
--- a/static/js/ui_init.js
+++ b/static/js/ui_init.js
@@ -16,6 +16,7 @@ const echo = require("./echo");
const emojisets = require("./emojisets");
const invite = require("./invite");
const markdown_config = require("./markdown_config");
+const muting = require("./muting");
const people = require("./people");
const pm_conversations = require("./pm_conversations");
const spoilers = require("./spoilers");
diff --git a/static/js/unread.js b/static/js/unread.js
index c740d84dfe7..eaeed543bd3 100644
--- a/static/js/unread.js
+++ b/static/js/unread.js
@@ -1,6 +1,7 @@
"use strict";
const {FoldDict} = require("./fold_dict");
+const muting = require("./muting");
const people = require("./people");
const util = require("./util");