From 4143dac95e8746559fdf7c325b91c99f3ca2db53 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sun, 1 Oct 2023 12:48:06 -0700 Subject: [PATCH] muted_users_ui: Move functions to more appropriate modules. --- web/src/muted_users.ts | 13 ++++++++++ web/src/muted_users_ui.js | 35 -------------------------- web/src/settings_muted_users.js | 3 +-- web/src/user_card_popover.js | 24 +++++++++++++++--- web/tests/settings_muted_users.test.js | 16 +++++++++--- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/web/src/muted_users.ts b/web/src/muted_users.ts index c377e33f92..5ad89d52bf 100644 --- a/web/src/muted_users.ts +++ b/web/src/muted_users.ts @@ -1,3 +1,4 @@ +import * as channel from "./channel"; import * as timerender from "./timerender"; import {get_time_from_date_muted} from "./util"; @@ -68,6 +69,18 @@ export function set_muted_users(list: RawMutedUser[]): void { } } +export function mute_user(user_id: number): void { + void channel.post({ + url: "/json/users/me/muted_users/" + user_id, + }); +} + +export function unmute_user(user_id: number): void { + void channel.del({ + url: "/json/users/me/muted_users/" + user_id, + }); +} + export function initialize(params: {muted_users: RawMutedUser[]}): void { set_muted_users(params.muted_users); } diff --git a/web/src/muted_users_ui.js b/web/src/muted_users_ui.js index 8a81019d40..f08f9ca97e 100644 --- a/web/src/muted_users_ui.js +++ b/web/src/muted_users_ui.js @@ -1,47 +1,12 @@ -import render_confirm_mute_user from "../templates/confirm_dialog/confirm_mute_user.hbs"; - import * as activity from "./activity"; -import * as channel from "./channel"; -import * as confirm_dialog from "./confirm_dialog"; -import {$t_html} from "./i18n"; import * as message_lists from "./message_lists"; import * as muted_users from "./muted_users"; import * as overlays from "./overlays"; -import * as people from "./people"; import * as pm_list from "./pm_list"; import * as popovers from "./popovers"; import * as recent_view_ui from "./recent_view_ui"; import * as settings_muted_users from "./settings_muted_users"; -export function mute_user(user_id) { - channel.post({ - url: "/json/users/me/muted_users/" + user_id, - }); -} - -export function confirm_mute_user(user_id) { - function on_click() { - mute_user(user_id); - } - - const html_body = render_confirm_mute_user({ - user_name: people.get_full_name(user_id), - }); - - confirm_dialog.launch({ - html_heading: $t_html({defaultMessage: "Mute user"}), - help_link: "/help/mute-a-user", - html_body, - on_click, - }); -} - -export function unmute_user(user_id) { - channel.del({ - url: "/json/users/me/muted_users/" + user_id, - }); -} - export function rerender_for_muted_user() { message_lists.current.update_muting_and_rerender(); if (message_lists.current !== message_lists.home) { diff --git a/web/src/settings_muted_users.js b/web/src/settings_muted_users.js index d976e86c93..7ef4227689 100644 --- a/web/src/settings_muted_users.js +++ b/web/src/settings_muted_users.js @@ -4,7 +4,6 @@ import render_muted_user_ui_row from "../templates/muted_user_ui_row.hbs"; import * as ListWidget from "./list_widget"; import * as muted_users from "./muted_users"; -import * as muted_users_ui from "./muted_users_ui"; import * as people from "./people"; import * as scroll_util from "./scroll_util"; @@ -52,7 +51,7 @@ export function set_up() { const user_id = Number.parseInt($row.attr("data-user-id"), 10); e.stopPropagation(); - muted_users_ui.unmute_user(user_id); + muted_users.unmute_user(user_id); }); populate_list(); diff --git a/web/src/user_card_popover.js b/web/src/user_card_popover.js index 9137db0554..c2a9f48eda 100644 --- a/web/src/user_card_popover.js +++ b/web/src/user_card_popover.js @@ -3,6 +3,7 @@ import {parseISO} from "date-fns"; import $ from "jquery"; import tippy from "tippy.js"; +import render_confirm_mute_user from "../templates/confirm_dialog/confirm_mute_user.hbs"; import render_user_card_popover_content from "../templates/user_card_popover_content.hbs"; import render_user_card_popover_manage_menu from "../templates/user_card_popover_manage_menu.hbs"; import render_user_card_popover_title from "../templates/user_card_popover_title.hbs"; @@ -13,13 +14,13 @@ import * as channel from "./channel"; import * as compose_actions from "./compose_actions"; import * as compose_state from "./compose_state"; import * as compose_ui from "./compose_ui"; +import * as confirm_dialog from "./confirm_dialog"; import {show_copied_confirmation} from "./copied_tooltip"; import * as dialog_widget from "./dialog_widget"; import * as hash_util from "./hash_util"; import {$t, $t_html} from "./i18n"; import * as message_lists from "./message_lists"; import * as muted_users from "./muted_users"; -import * as muted_users_ui from "./muted_users_ui"; import * as narrow from "./narrow"; import * as overlays from "./overlays"; import {page_params} from "./page_params"; @@ -40,6 +41,23 @@ import * as user_status_ui from "./user_status_ui"; let current_user_sidebar_user_id; +export function confirm_mute_user(user_id) { + function on_click() { + muted_users.mute_user(user_id); + } + + const html_body = render_confirm_mute_user({ + user_name: people.get_full_name(user_id), + }); + + confirm_dialog.launch({ + html_heading: $t_html({defaultMessage: "Mute user"}), + help_link: "/help/mute-a-user", + html_body, + on_click, + }); +} + class PopoverMenu { constructor() { this.instance = null; @@ -785,13 +803,13 @@ function register_click_handlers() { hide_all_user_card_popovers(); e.stopPropagation(); e.preventDefault(); - muted_users_ui.confirm_mute_user(user_id); + confirm_mute_user(user_id); }); $("body").on("click", ".sidebar-popover-unmute-user", (e) => { const user_id = elem_to_user_id($(e.target).parents("ul")); hide_all_user_card_popovers(); - muted_users_ui.unmute_user(user_id); + muted_users.unmute_user(user_id); e.stopPropagation(); e.preventDefault(); }); diff --git a/web/tests/settings_muted_users.test.js b/web/tests/settings_muted_users.test.js index 729f01fde9..48272a2f50 100644 --- a/web/tests/settings_muted_users.test.js +++ b/web/tests/settings_muted_users.test.js @@ -8,10 +8,10 @@ const $ = require("./lib/zjquery"); const noop = () => {}; +const channel = mock_esm("../src/channel"); const list_widget = mock_esm("../src/list_widget", { generic_sort_functions: noop, }); -const muted_users_ui = mock_esm("../src/muted_users_ui"); const settings_muted_users = zrequire("settings_muted_users"); const muted_users = zrequire("muted_users"); @@ -61,12 +61,22 @@ run_test("settings", ({override}) => { }; let unmute_user_called = false; - muted_users_ui.unmute_user = (user_id) => { - assert.equal(user_id, 5); + channel.del = (payload) => { + assert.equal(payload.url, "/json/users/me/muted_users/5"); unmute_user_called = true; + return {abort() {}}; }; unmute_click_handler.call($unmute_button, event); assert.ok(unmute_user_called); assert.ok(row_attribute_fetched); + + let mute_user_called = false; + channel.post = (payload) => { + assert.equal(payload.url, "/json/users/me/muted_users/5"); + mute_user_called = true; + return {abort() {}}; + }; + muted_users.mute_user(5); + assert.ok(mute_user_called); });