Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
js: Convert static/js/notifications.js to ES6 module.
Anders Kaseorg9d318f054f77
diff --git a/.eslintrc.json b/.eslintrc.jsonindex 964796e2a03..e5a5afd96db 100644--- a/.eslintrc.json+++ b/.eslintrc.json@@ -162,7 +162,6 @@"message_store": false,"message_util": false,"narrow": false,- "notifications": false,"padded_widget": false,"page_params": false,"panels": false,diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.jsindex 4cb103eabb4..1191231e912 100644--- a/frontend_tests/node_tests/compose.js+++ b/frontend_tests/node_tests/compose.js@@ -42,6 +42,7 @@ const sent_messages = {start_tracking_message: noop,};const _notifications = {+ __esModule: true,notify_above_composebox: noop,clear_compose_notifications: noop,};@@ -52,7 +53,7 @@ const reminder = {set_global("document", _document);rewiremock("../../static/js/drafts").with(_drafts);set_global("navigator", _navigator);-set_global("notifications", _notifications);+rewiremock("../../static/js/notifications").with(_notifications);set_global("reminder", reminder);rewiremock("../../static/js/sent_messages").with(sent_messages);rewiremock("../../static/js/rendered_markdown").with({diff --git a/frontend_tests/node_tests/compose_actions.js b/frontend_tests/node_tests/compose_actions.jsindex 3638d92f368..6971c24e685 100644--- a/frontend_tests/node_tests/compose_actions.js+++ b/frontend_tests/node_tests/compose_actions.js@@ -27,7 +27,7 @@ rewiremock("../../static/js/reload_state").with({is_in_progress: () => false,});-set_global("notifications", {+rewiremock("../../static/js/notifications").with({clear_compose_notifications: noop,});diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.jsindex 87a39bcbd7c..25421b6cf98 100644--- a/frontend_tests/node_tests/dispatch.js+++ b/frontend_tests/node_tests/dispatch.js@@ -40,7 +40,8 @@ const muting_ui = {__esModule: true};rewiremock("../../static/js/muting_ui").with(muting_ui);const night_mode = {__esModule: true};rewiremock("../../static/js/night_mode").with(night_mode);-const notifications = set_global("notifications", {});+const notifications = {__esModule: true};+rewiremock("../../static/js/notifications").with(notifications);const reactions = set_global("reactions", {});const realm_icon = set_global("realm_icon", {});const realm_logo = set_global("realm_logo", {});diff --git a/frontend_tests/node_tests/message_events.js b/frontend_tests/node_tests/message_events.jsindex a16706c91dd..a3c378d5569 100644--- a/frontend_tests/node_tests/message_events.js+++ b/frontend_tests/node_tests/message_events.js@@ -13,7 +13,8 @@ set_global("current_msg_list", {});const message_edit = {__esModule: true};rewiremock("../../static/js/message_edit").with(message_edit);const message_list = set_global("message_list", {});-const notifications = set_global("notifications", {});+const notifications = {__esModule: true};+rewiremock("../../static/js/notifications").with(notifications);const page_params = set_global("page_params", {});const pm_list = {__esModule: true};rewiremock("../../static/js/pm_list").with(pm_list);diff --git a/frontend_tests/node_tests/narrow_activate.js b/frontend_tests/node_tests/narrow_activate.jsindex 58d0b760f6f..eaa67cd181a 100644--- a/frontend_tests/node_tests/narrow_activate.js+++ b/frontend_tests/node_tests/narrow_activate.js@@ -28,7 +28,8 @@ const message_list = set_global("message_list", {const message_scroll = {__esModule: true};rewiremock("../../static/js/message_scroll").with(message_scroll);set_global("message_util", {});-const notifications = set_global("notifications", {});+const notifications = {__esModule: true};+rewiremock("../../static/js/notifications").with(notifications);set_global("page_params", {});const search = {__esModule: true};rewiremock("../../static/js/search").with(search);diff --git a/frontend_tests/node_tests/recent_topics.js b/frontend_tests/node_tests/recent_topics.jsindex fd6ab3a00b7..585bb592348 100644--- a/frontend_tests/node_tests/recent_topics.js+++ b/frontend_tests/node_tests/recent_topics.js@@ -22,7 +22,7 @@ set_global("compose_actions", {set_global("narrow", {narrow_title: "",});-set_global("notifications", {+rewiremock("../../static/js/notifications").with({redraw_title: noop,});set_global("message_view_header", {diff --git a/frontend_tests/node_tests/tutorial.js b/frontend_tests/node_tests/tutorial.jsindex 8873ab5fd35..760bdca11e3 100644--- a/frontend_tests/node_tests/tutorial.js+++ b/frontend_tests/node_tests/tutorial.js@@ -30,7 +30,8 @@ const settings_users = set_global("settings_users", {});const home_msg_list = set_global("home_msg_list", {});const message_list = set_global("message_list", {});const message_util = set_global("message_util", {});-const notifications = set_global("notifications", {});+const notifications = {__esModule: true};+rewiremock("../../static/js/notifications").with(notifications);const overlays = {__esModule: true};rewiremock("../../static/js/overlays").with(overlays);const resize = {__esModule: true};diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.jsindex b8924cbc38f..f4d845dffbb 100644--- a/frontend_tests/node_tests/ui_init.js+++ b/frontend_tests/node_tests/ui_init.js@@ -119,7 +119,6 @@ const upload = zrequire("upload");const compose = zrequire("compose");zrequire("composebox_typeahead");zrequire("narrow");-zrequire("notifications");zrequire("stream_list");zrequire("starred_messages");zrequire("recent_topics");diff --git a/static/js/click_handlers.js b/static/js/click_handlers.jsindex 452f9ac6801..f783b84b6ef 100644--- a/static/js/click_handlers.js+++ b/static/js/click_handlers.js@@ -12,6 +12,7 @@ import * as compose_state from "./compose_state";import * as message_edit from "./message_edit";import * as message_edit_history from "./message_edit_history";import * as muting_ui from "./muting_ui";+import * as notifications from "./notifications";import * as overlays from "./overlays";import * as popovers from "./popovers";import * as rows from "./rows";diff --git a/static/js/compose.js b/static/js/compose.jsindex 6e8b482cfb9..7bd442399bc 100644--- a/static/js/compose.js+++ b/static/js/compose.js@@ -16,6 +16,7 @@ import * as drafts from "./drafts";import * as echo from "./echo";import * as loading from "./loading";import * as markdown from "./markdown";+import * as notifications from "./notifications";import * as peer_data from "./peer_data";import * as people from "./people";import * as rendered_markdown from "./rendered_markdown";diff --git a/static/js/compose_actions.js b/static/js/compose_actions.jsindex 91da131ceab..7fd61dd40c7 100644--- a/static/js/compose_actions.js+++ b/static/js/compose_actions.js@@ -13,6 +13,7 @@ const compose_state = require("./compose_state");const drafts = require("./drafts");const message_viewport = require("./message_viewport");const narrow_state = require("./narrow_state");+const notifications = require("./notifications");const people = require("./people");const reload_state = require("./reload_state");const stream_data = require("./stream_data");diff --git a/static/js/echo.js b/static/js/echo.jsindex 5789371ba18..cc7c9067d1d 100644--- a/static/js/echo.js+++ b/static/js/echo.js@@ -3,6 +3,7 @@ import * as compose from "./compose";import * as local_message from "./local_message";import * as markdown from "./markdown";import * as narrow_state from "./narrow_state";+import * as notifications from "./notifications";import * as people from "./people";import * as pm_list from "./pm_list";import * as popovers from "./popovers";diff --git a/static/js/global.d.ts b/static/js/global.d.tsindex 8b97045eb0d..a7c7cc290c8 100644--- a/static/js/global.d.ts+++ b/static/js/global.d.ts@@ -35,7 +35,6 @@ declare let message_list: any;declare let message_store: any;declare let message_util: any;declare let narrow: any;-declare let notifications: any;declare let padded_widget: any;declare let page_params: any;declare let panels: any;diff --git a/static/js/message_events.js b/static/js/message_events.jsindex 04a76a397a7..2cbadd4df2b 100644--- a/static/js/message_events.js+++ b/static/js/message_events.js@@ -9,6 +9,7 @@ const huddle_data = require("./huddle_data");const message_edit = require("./message_edit");const message_edit_history = require("./message_edit_history");const narrow_state = require("./narrow_state");+const notifications = require("./notifications");const pm_list = require("./pm_list");const recent_senders = require("./recent_senders");const resize = require("./resize");diff --git a/static/js/narrow.js b/static/js/narrow.jsindex 2a7677241e6..e56a255e681 100644--- a/static/js/narrow.js+++ b/static/js/narrow.js@@ -10,6 +10,7 @@ const message_edit = require("./message_edit");const {MessageListData} = require("./message_list_data");const message_scroll = require("./message_scroll");const narrow_state = require("./narrow_state");+const notifications = require("./notifications");const people = require("./people");const resize = require("./resize");const search = require("./search");diff --git a/static/js/notifications.js b/static/js/notifications.jsindex 614d9a746be..2d319eb16eb 100644--- a/static/js/notifications.js+++ b/static/js/notifications.js@@ -1,21 +1,19 @@-"use strict";--const _ = require("lodash");--const render_compose_notification = require("../templates/compose_notification.hbs");-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 narrow_state = require("./narrow_state");-const navigate = require("./navigate");-const people = require("./people");-const settings_config = require("./settings_config");-const spoilers = require("./spoilers");-const stream_data = require("./stream_data");-const stream_ui_updates = require("./stream_ui_updates");+import _ from "lodash";++import render_compose_notification from "../templates/compose_notification.hbs";+import render_notification from "../templates/notification.hbs";++import * as alert_words from "./alert_words";+import * as channel from "./channel";+import * as favicon from "./favicon";+import * as muting from "./muting";+import * as narrow_state from "./narrow_state";+import * as navigate from "./navigate";+import * as people from "./people";+import * as settings_config from "./settings_config";+import * as spoilers from "./spoilers";+import * as stream_data from "./stream_data";+import * as stream_ui_updates from "./stream_ui_updates";const notice_memory = new Map();@@ -27,9 +25,9 @@ let supports_sound;let NotificationAPI;-exports.set_notification_api = function (n) {+export function set_notification_api(n) {NotificationAPI = n;-};+}if (window.electron_bridge && window.electron_bridge.new_notification) {class ElectronBridgeNotification extends EventTarget {@@ -60,9 +58,9 @@ if (window.electron_bridge && window.electron_bridge.new_notification) {NotificationAPI = window.Notification;}-exports.get_notifications = function () {+export function get_notifications() {return notice_memory;-};+}function get_audio_file_path(audio_element, audio_file_without_extension) {if (audio_element.canPlayType('audio/ogg; codecs="vorbis"')) {@@ -72,7 +70,7 @@ function get_audio_file_path(audio_element, audio_file_without_extension) {return audio_file_without_extension + ".mp3";}-exports.initialize = function () {+export function initialize() {$(window).on("focus", () => {window_focused = true;@@ -110,7 +108,7 @@ exports.initialize = function () {"/static/audio/notification_sounds/" + page_params.notification_sound;source.attr("src", get_audio_file_path(audio[0], audio_file_without_extension));}-};+}function update_notification_sound_source() {// Simplified version of the source creation in `exports.initialize`, for@@ -126,19 +124,19 @@ function update_notification_sound_source() {$("#notifications-area").find("audio")[0].load();}-exports.permission_state = function () {+export function permission_state() {if (NotificationAPI === undefined) {// act like notifications are blocked if they do not have access to// the notification API.return "denied";}return NotificationAPI.permission;-};+}let unread_count = 0;let pm_count = 0;-exports.redraw_title = function () {+export function redraw_title() {// Update window title to reflect unread messages in current viewconst new_title =(unread_count ? "(" + unread_count + ") " : "") +@@ -149,9 +147,9 @@ exports.redraw_title = function () {"Zulip";document.title = new_title;-};+}-exports.update_unread_counts = function (new_unread_count, new_pm_count) {+export function update_unread_counts(new_unread_count, new_pm_count) {if (new_unread_count === unread_count && new_pm_count === pm_count) {return;}@@ -169,12 +167,12 @@ exports.update_unread_counts = function (new_unread_count, new_pm_count) {// TODO: Add a `window.electron_bridge.updatePMCount(new_pm_count);` call?- exports.redraw_title();-};+ redraw_title();+}-exports.is_window_focused = function () {+export function is_window_focused() {return window_focused;-};+}function in_browser_notify(message, title, content, raw_operators, opts) {const notification_html = $(@@ -206,7 +204,7 @@ function in_browser_notify(message, title, content, raw_operators, opts) {});}-exports.notify_above_composebox = function (note, link_class, link_msg_id, link_text) {+export function notify_above_composebox(note, link_class, link_msg_id, link_text) {const notification_html = $(render_compose_notification({note,@@ -215,10 +213,10 @@ exports.notify_above_composebox = function (note, link_class, link_msg_id, link_link_text,}),);- exports.clear_compose_notifications();+ clear_compose_notifications();$("#out-of-view-notification").append(notification_html);$("#out-of-view-notification").show();-};+}if (window.electron_bridge !== undefined) {// The code below is for sending a message received from notification reply which@@ -262,7 +260,7 @@ if (window.electron_bridge !== undefined) {});}-function process_notification(notification) {+export function process_notification(notification) {let i;let notification_object;let key;@@ -388,18 +386,16 @@ function process_notification(notification) {}}-exports.process_notification = process_notification;--exports.close_notification = function (message) {+export function close_notification(message) {for (const [key, notice_mem_entry] of notice_memory) {if (notice_mem_entry.message_id === message.id) {notice_mem_entry.obj.close();notice_memory.delete(key);}}-};+}-exports.message_is_notifiable = function (message) {+export function message_is_notifiable(message) {// Independent of the user's notification settings, are there// properties of the message that unconditionally mean we// shouldn't notify about it.@@ -433,9 +429,9 @@ exports.message_is_notifiable = function (message) {// Everything else is on the table; next filter based on notification// settings.return true;-};+}-exports.should_send_desktop_notification = function (message) {+export function should_send_desktop_notification(message) {// Always notify for testing notifications.if (message.type === "test-notification") {return true;@@ -479,9 +475,9 @@ exports.should_send_desktop_notification = function (message) {}return false;-};+}-exports.should_send_audible_notification = function (message) {+export function should_send_audible_notification(message) {// For streams, ding if sounds are enabled for all messages on// this stream.if (@@ -519,21 +515,21 @@ exports.should_send_audible_notification = function (message) {}return false;-};+}-exports.granted_desktop_notifications_permission = function () {+export function granted_desktop_notifications_permission() {return NotificationAPI && NotificationAPI.permission === "granted";-};+}-exports.request_desktop_notifications_permission = function () {+export function request_desktop_notifications_permission() {if (NotificationAPI) {NotificationAPI.requestPermission();}-};+}-exports.received_messages = function (messages) {+export function received_messages(messages) {for (const message of messages) {- if (!exports.message_is_notifiable(message)) {+ if (!message_is_notifiable(message)) {continue;}if (!unread.message_unread(message)) {@@ -543,20 +539,20 @@ exports.received_messages = function (messages) {message.notification_sent = true;- if (exports.should_send_desktop_notification(message)) {+ if (should_send_desktop_notification(message)) {process_notification({message,- desktop_notify: exports.granted_desktop_notifications_permission(),+ desktop_notify: granted_desktop_notifications_permission(),});}- if (exports.should_send_audible_notification(message) && supports_sound) {+ if (should_send_audible_notification(message) && supports_sound) {$("#notifications-area").find("audio")[0].play();}}-};+}-exports.send_test_notification = function (content) {- exports.received_messages([+export function send_test_notification(content) {+ received_messages([{id: Math.random(),type: "test-notification",@@ -567,7 +563,7 @@ exports.send_test_notification = function (content) {unread: true,},]);-};+}// Note that this returns values that are not HTML-escaped, for use in// handlebars templates that will do further escaping.@@ -586,7 +582,7 @@ function get_message_header(message) {return i18n.t("private messages with __- recipient__", {recipient: message.display_reply_to});}-exports.get_local_notify_mix_reason = function (message) {+export function get_local_notify_mix_reason(message) {const row = current_msg_list.get_row(message.id);if (row.length > 0) {// If our message is in the current message list, we do@@ -615,9 +611,9 @@ exports.get_local_notify_mix_reason = function (message) {}return undefined;-};+}-exports.notify_local_mixes = function (messages, need_user_to_scroll) {+export function notify_local_mixes(messages, need_user_to_scroll) {/*This code should only be called when we are displayingmessages sent by current client. It notifies users that@@ -646,12 +642,12 @@ exports.notify_local_mixes = function (messages, need_user_to_scroll) {continue;}- let reason = exports.get_local_notify_mix_reason(message);+ let reason = get_local_notify_mix_reason(message);if (!reason) {if (need_user_to_scroll) {reason = i18n.t("Sent! Scroll down to view your message.");- exports.notify_above_composebox(reason, "", null, "");+ notify_above_composebox(reason, "", null, "");setTimeout(() => {$("#out-of-view-notification").hide();}, 3000);@@ -668,13 +664,13 @@ exports.notify_local_mixes = function (messages, need_user_to_scroll) {message_recipient: get_message_header(message),});- exports.notify_above_composebox(reason, link_class, link_msg_id, link_text);+ notify_above_composebox(reason, link_class, link_msg_id, link_text);}-};+}// for callback when we have to check with the server if a message should be in// the current_msg_list (!can_apply_locally; a.k.a. "a search").-exports.notify_messages_outside_current_search = function (messages) {+export function notify_messages_outside_current_search(messages) {for (const message of messages) {if (!people.is_current_user(message.sender_email)) {continue;@@ -682,22 +678,22 @@ exports.notify_messages_outside_current_search = function (messages) {const link_text = i18n.t("Narrow to __- message_recipient__", {message_recipient: get_message_header(message),});- exports.notify_above_composebox(+ notify_above_composebox(i18n.t("Sent! Your recent message is outside the current search."),"compose_notification_narrow_by_topic",message.id,link_text,);}-};+}-exports.clear_compose_notifications = function () {+export function clear_compose_notifications() {$("#out-of-view-notification").empty();$("#out-of-view-notification").stop(true, true);$("#out-of-view-notification").hide();-};+}-exports.reify_message_id = function (opts) {+export function reify_message_id(opts) {const old_id = opts.old_id;const new_id = opts.new_id;@@ -711,9 +707,9 @@ exports.reify_message_id = function (opts) {elem.data("message-id", new_id);}}-};+}-exports.register_click_handlers = function () {+export function register_click_handlers() {$("#out-of-view-notification").on("click", ".compose_notification_narrow_by_topic", (e) => {const message_id = $(e.currentTarget).data("message-id");narrow.by_topic(message_id, {trigger: "compose_notification"});@@ -728,13 +724,13 @@ exports.register_click_handlers = function () {e.preventDefault();});$("#out-of-view-notification").on("click", ".out-of-view-notification-close", (e) => {- exports.clear_compose_notifications();+ clear_compose_notifications();e.stopPropagation();e.preventDefault();});-};+}-exports.handle_global_notification_updates = function (notification_name, setting) {+export function handle_global_notification_updates(notification_name, setting) {// Update the global settings checked when determining if we should notify// for a given message. These settings do not affect whether or not a// particular stream should receive notifications.@@ -751,6 +747,4 @@ exports.handle_global_notification_updates = function (notification_name, settin// Change the sound source with the new page `notification_sound`.update_notification_sound_source();}-};--window.notifications = exports;+}diff --git a/static/js/panels.js b/static/js/panels.jsindex 6953b487cf7..39069885bea 100644--- a/static/js/panels.js+++ b/static/js/panels.js@@ -1,6 +1,7 @@"use strict";const {localstorage} = require("./localstorage");+const notifications = require("./notifications");const unread_ui = require("./unread_ui");const util = require("./util");diff --git a/static/js/recent_topics.js b/static/js/recent_topics.jsindex bc6e8c6d1cd..444fcb2baf1 100644--- a/static/js/recent_topics.js+++ b/static/js/recent_topics.js@@ -10,6 +10,7 @@ const {localstorage} = require("./localstorage");const muting = require("./muting");const narrow_state = require("./narrow_state");const navigate = require("./navigate");+const notifications = require("./notifications");const people = require("./people");const recent_senders = require("./recent_senders");const stream_data = require("./stream_data");diff --git a/static/js/reminder.js b/static/js/reminder.jsindex 5c6dfc3cd60..02f93bde7d8 100644--- a/static/js/reminder.js+++ b/static/js/reminder.js@@ -2,6 +2,7 @@const channel = require("./channel");const compose = require("./compose");+const notifications = require("./notifications");const people = require("./people");const transmit = require("./transmit");const util = require("./util");diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.jsindex fc4b64cf659..b62d6c3aae5 100644--- a/static/js/server_events_dispatch.js+++ b/static/js/server_events_dispatch.js@@ -8,6 +8,7 @@ import * as message_edit from "./message_edit";import * as muting_ui from "./muting_ui";import * as narrow_state from "./narrow_state";import * as night_mode from "./night_mode";+import * as notifications from "./notifications";import * as overlays from "./overlays";import * as peer_data from "./peer_data";import * as people from "./people";diff --git a/static/js/settings_notifications.js b/static/js/settings_notifications.jsindex d5e3b0f5eb0..1acb23d62c9 100644--- a/static/js/settings_notifications.js+++ b/static/js/settings_notifications.js@@ -3,6 +3,7 @@const render_stream_specific_notification_row = require("../templates/settings/stream_specific_notification_row.hbs");const channel = require("./channel");+const notifications = require("./notifications");const settings_config = require("./settings_config");const settings_ui = require("./settings_ui");const stream_data = require("./stream_data");diff --git a/static/js/ui_init.js b/static/js/ui_init.jsindex 0d345583908..ced25f6a3d1 100644--- a/static/js/ui_init.js+++ b/static/js/ui_init.js@@ -27,6 +27,7 @@ const message_scroll = require("./message_scroll");const message_viewport = require("./message_viewport");const muting = require("./muting");const navigate = require("./navigate");+const notifications = require("./notifications");const overlays = require("./overlays");const people = require("./people");const pm_conversations = require("./pm_conversations");diff --git a/static/js/unread_ops.js b/static/js/unread_ops.jsindex 254d25b1c83..16fc22e39ee 100644--- a/static/js/unread_ops.js+++ b/static/js/unread_ops.js@@ -2,6 +2,7 @@const channel = require("./channel");const message_viewport = require("./message_viewport");+const notifications = require("./notifications");const overlays = require("./overlays");const reload = require("./reload");const unread_ui = require("./unread_ui");diff --git a/static/js/unread_ui.js b/static/js/unread_ui.jsindex f7978d7f135..ed95b85d565 100644--- a/static/js/unread_ui.js+++ b/static/js/unread_ui.js@@ -1,3 +1,4 @@+import * as notifications from "./notifications";import * as pm_list from "./pm_list";import * as top_left_corner from "./top_left_corner";import * as topic_list from "./topic_list";