From fcaf1fe7d59e532966bc3eeff0c074174188ad04 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 6 Oct 2023 15:09:47 -0700 Subject: [PATCH] notifications: Split out audible_notifications module. Signed-off-by: Anders Kaseorg --- tools/test-js-with-node | 1 + web/src/audible_notifications.js | 24 +++++++++++++++++++ web/src/notifications.js | 21 ---------------- web/src/server_events_dispatch.js | 6 ++--- web/src/settings_org.js | 4 ++-- .../settings_realm_user_settings_defaults.js | 6 ++--- web/src/ui_init.js | 2 ++ web/tests/dispatch.test.js | 6 ++--- 8 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 web/src/audible_notifications.js diff --git a/tools/test-js-with-node b/tools/test-js-with-node index d959e2a579..66e60fe08c 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -54,6 +54,7 @@ EXEMPT_FILES = make_set( "web/src/archive.js", "web/src/assets.d.ts", "web/src/attachments_ui.ts", + "web/src/audible_notifications.js", "web/src/avatar.ts", "web/src/billing/event_status.ts", "web/src/billing/helpers.ts", diff --git a/web/src/audible_notifications.js b/web/src/audible_notifications.js new file mode 100644 index 0000000000..20f317b7ad --- /dev/null +++ b/web/src/audible_notifications.js @@ -0,0 +1,24 @@ +import $ from "jquery"; + +import {user_settings} from "./user_settings"; + +export function initialize() { + update_notification_sound_source($("#user-notification-sound-audio"), user_settings); +} + +export function update_notification_sound_source(container_elem, settings_object) { + const notification_sound = settings_object.notification_sound; + const audio_file_without_extension = "/static/audio/notification_sounds/" + notification_sound; + container_elem + .find(".notification-sound-source-ogg") + .attr("src", `${audio_file_without_extension}.ogg`); + container_elem + .find(".notification-sound-source-mp3") + .attr("src", `${audio_file_without_extension}.mp3`); + + if (notification_sound !== "none") { + // Load it so that it is ready to be played; without this the old sound + // is played. + container_elem[0].load(); + } +} diff --git a/web/src/notifications.js b/web/src/notifications.js index 154bc98769..a8899305b6 100644 --- a/web/src/notifications.js +++ b/web/src/notifications.js @@ -1,7 +1,5 @@ import $ from "jquery"; -import {user_settings} from "./user_settings"; - export const notice_memory = new Map(); export let NotificationAPI; @@ -50,25 +48,6 @@ export function initialize() { } notice_memory.clear(); }); - - update_notification_sound_source($("#user-notification-sound-audio"), user_settings); -} - -export function update_notification_sound_source(container_elem, settings_object) { - const notification_sound = settings_object.notification_sound; - const audio_file_without_extension = "/static/audio/notification_sounds/" + notification_sound; - container_elem - .find(".notification-sound-source-ogg") - .attr("src", `${audio_file_without_extension}.ogg`); - container_elem - .find(".notification-sound-source-mp3") - .attr("src", `${audio_file_without_extension}.mp3`); - - if (notification_sound !== "none") { - // Load it so that it is ready to be played; without this the old sound - // is played. - container_elem[0].load(); - } } export function permission_state() { diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.js index 5cbdee397f..4419a8d128 100644 --- a/web/src/server_events_dispatch.js +++ b/web/src/server_events_dispatch.js @@ -4,6 +4,7 @@ import * as activity_ui from "./activity_ui"; import * as alert_words from "./alert_words"; import * as alert_words_ui from "./alert_words_ui"; import * as attachments_ui from "./attachments_ui"; +import * as audible_notifications from "./audible_notifications"; import * as blueslip from "./blueslip"; import * as bot_data from "./bot_data"; import * as browser_history from "./browser_history"; @@ -30,7 +31,6 @@ import * as muted_users_ui from "./muted_users_ui"; import * as narrow_state from "./narrow_state"; import * as narrow_title from "./narrow_title"; import * as navbar_alerts from "./navbar_alerts"; -import * as notifications from "./notifications"; import * as overlays from "./overlays"; import {page_params} from "./page_params"; import * as peer_data from "./peer_data"; @@ -450,7 +450,7 @@ export function dispatch_normal_event(event) { settings_realm_user_settings_defaults.update_page(event.property); if (event.property === "notification_sound") { - notifications.update_notification_sound_source( + audible_notifications.update_notification_sound_source( $("#realm-default-notification-sound-audio"), realm_user_settings_defaults, ); @@ -673,7 +673,7 @@ export function dispatch_normal_event(event) { if (notification_name === "notification_sound") { // Change the sound source with the new page `notification_sound`. - notifications.update_notification_sound_source( + audible_notifications.update_notification_sound_source( $("#user-notification-sound-audio"), user_settings, ); diff --git a/web/src/settings_org.js b/web/src/settings_org.js index 70a30b9a56..6c43acf7c5 100644 --- a/web/src/settings_org.js +++ b/web/src/settings_org.js @@ -5,6 +5,7 @@ import render_compose_banner from "../templates/compose_banner/compose_banner.hb import render_settings_deactivate_realm_modal from "../templates/confirm_dialog/confirm_deactivate_realm.hbs"; import render_settings_admin_auth_methods_list from "../templates/settings/admin_auth_methods_list.hbs"; +import * as audible_notifications from "./audible_notifications"; import * as blueslip from "./blueslip"; import * as channel from "./channel"; import * as compose_banner from "./compose_banner"; @@ -14,7 +15,6 @@ import * as dropdown_widget from "./dropdown_widget"; import {$t, $t_html, get_language_name} from "./i18n"; import * as keydown_util from "./keydown_util"; import * as loading from "./loading"; -import * as notifications from "./notifications"; import {page_params} from "./page_params"; import * as realm_icon from "./realm_icon"; import * as realm_logo from "./realm_logo"; @@ -717,7 +717,7 @@ export function discard_property_element_changes(elem, for_realm_default_setting switch (property_name) { case "notification_sound": - notifications.update_notification_sound_source( + audible_notifications.update_notification_sound_source( $("#realm-default-notification-sound-audio"), { notification_sound: property_value, diff --git a/web/src/settings_realm_user_settings_defaults.js b/web/src/settings_realm_user_settings_defaults.js index 9f88191991..405a02ff26 100644 --- a/web/src/settings_realm_user_settings_defaults.js +++ b/web/src/settings_realm_user_settings_defaults.js @@ -1,6 +1,6 @@ import $ from "jquery"; -import * as notifications from "./notifications"; +import * as audible_notifications from "./audible_notifications"; import * as overlays from "./overlays"; import {page_params} from "./page_params"; import {realm_user_settings_defaults} from "./realm_user_settings_defaults"; @@ -59,14 +59,14 @@ export function set_up() { settings_display.set_up(realm_default_settings_panel); - notifications.update_notification_sound_source( + audible_notifications.update_notification_sound_source( $notification_sound_elem, realm_default_settings_panel.settings_object, ); $notification_sound_dropdown.on("change", () => { const sound = $notification_sound_dropdown.val().toLowerCase(); - notifications.update_notification_sound_source($notification_sound_elem, { + audible_notifications.update_notification_sound_source($notification_sound_elem, { notification_sound: sound, }); }); diff --git a/web/src/ui_init.js b/web/src/ui_init.js index 52170c8501..55afc386cf 100644 --- a/web/src/ui_init.js +++ b/web/src/ui_init.js @@ -16,6 +16,7 @@ import * as activity from "./activity"; import * as activity_ui from "./activity_ui"; import * as add_stream_options_popover from "./add_stream_options_popover"; import * as alert_words from "./alert_words"; +import * as audible_notifications from "./audible_notifications"; import * as blueslip from "./blueslip"; import * as bot_data from "./bot_data"; import * as channel from "./channel"; @@ -670,6 +671,7 @@ export function initialize_everything() { }); tutorial.initialize(); notifications.initialize(); + audible_notifications.initialize(); compose_notifications.initialize({ on_click_scroll_to_selected: navigate.scroll_to_selected, on_narrow_to_recipient(message_id) { diff --git a/web/tests/dispatch.test.js b/web/tests/dispatch.test.js index 17f02e8ecb..9b25c2c193 100644 --- a/web/tests/dispatch.test.js +++ b/web/tests/dispatch.test.js @@ -22,6 +22,7 @@ set_global("requestAnimationFrame", (func) => func()); const activity_ui = mock_esm("../src/activity_ui"); const alert_words_ui = mock_esm("../src/alert_words_ui"); const attachments_ui = mock_esm("../src/attachments_ui"); +const audible_notifications = mock_esm("../src/audible_notifications"); const bot_data = mock_esm("../src/bot_data"); const compose_pm_pill = mock_esm("../src/compose_pm_pill"); const composebox_typeahead = mock_esm("../src/composebox_typeahead"); @@ -35,7 +36,6 @@ const message_lists = mock_esm("../src/message_lists"); const user_topics_ui = mock_esm("../src/user_topics_ui"); const muted_users_ui = mock_esm("../src/muted_users_ui"); const narrow_title = mock_esm("../src/narrow_title"); -const notifications = mock_esm("../src/notifications"); const pm_list = mock_esm("../src/pm_list"); const reactions = mock_esm("../src/reactions"); const realm_icon = mock_esm("../src/realm_icon"); @@ -1016,7 +1016,7 @@ run_test("user_settings", ({override}) => { } event = event_fixtures.user_settings__notification_sound; - override(notifications, "update_notification_sound_source", noop); + override(audible_notifications, "update_notification_sound_source", noop); dispatch(event); event = event_fixtures.user_settings__email_address_visibility; @@ -1189,7 +1189,7 @@ run_test("realm_user_settings_defaults", ({override}) => { event = event_fixtures.realm_user_settings_defaults__notification_sound; realm_user_settings_defaults.notification_sound = "zulip"; let called = false; - notifications.update_notification_sound_source = () => { + audible_notifications.update_notification_sound_source = () => { called = true; }; dispatch(event);