From 46cabdd59e1646d5d1cf682c47e876e818481d24 Mon Sep 17 00:00:00 2001 From: Hardik Dharmani Date: Tue, 11 Apr 2023 11:04:13 +0530 Subject: [PATCH] user_topics: Use new API endpoint to update topics_visibility_policy. Removed functions mute_topic and unmute_topic in muted_topics_ui.js which were using old API endpoints to mute/unmute topics. In user_topics.js added new function set_user_topic_visibility_policy which uses new API endpoint `/user_topics` to update the topic's visibility_policy to visibility_policy passed to set_user_topic_visibility_policy function as a parameter. In functions toggle_topic_mute and mute_or_unmute_topic, replaced the calls of mute_topic and unmute_topic with the new user_topics.set_user_topic_visibility_policy function. Added "web/src/user_topics.js" in EXEMPT_FILES in test-js-with-node as adding function `set_user_topic_visibility_policy` resulted in user_topics.js losing 100% test coverage. Fixes #24244 --- tools/test-js-with-node | 1 + web/src/muted_topics_ui.js | 89 ++++++------------------- web/src/settings_muted_topics.js | 7 +- web/src/user_topics.js | 53 +++++++++++++++ web/tests/settings_muted_topics.test.js | 3 +- 5 files changed, 81 insertions(+), 72 deletions(-) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 75c029558c..b6386a702d 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -220,6 +220,7 @@ EXEMPT_FILES = make_set( "web/src/user_settings.ts", "web/src/user_status.js", "web/src/user_status_ui.js", + "web/src/user_topics.js", "web/src/webpack_public_path.js", "web/src/zcommand.js", "web/src/zform.js", diff --git a/web/src/muted_topics_ui.js b/web/src/muted_topics_ui.js index b51a0407bf..e00401cb67 100644 --- a/web/src/muted_topics_ui.js +++ b/web/src/muted_topics_ui.js @@ -1,16 +1,10 @@ import _ from "lodash"; -import render_topic_muted from "../templates/topic_muted.hbs"; - -import * as channel from "./channel"; -import * as feedback_widget from "./feedback_widget"; -import {$t} from "./i18n"; import * as message_lists from "./message_lists"; import * as overlays from "./overlays"; import * as popover_menus from "./popover_menus"; import * as recent_topics_ui from "./recent_topics_ui"; import * as settings_muted_topics from "./settings_muted_topics"; -import * as stream_data from "./stream_data"; import * as stream_list from "./stream_list"; import * as unread_ui from "./unread_ui"; import * as user_topics from "./user_topics"; @@ -44,72 +38,23 @@ export function handle_topic_updates(user_topic) { rerender_for_muted_topic(old_muted_topics); } -export function mute_topic(stream_id, topic, from_hotkey) { - const stream_name = stream_data.maybe_get_stream_name(stream_id); - const data = { - stream_id, - topic, - op: "add", - }; - - channel.patch({ - url: "/json/users/me/subscriptions/muted_topics", - data, - success() { - if (!from_hotkey) { - return; - } - - // The following feedback_widget notice helps avoid - // confusion when a user who is not familiar with Zulip's - // keyboard UI hits "M" in the wrong context and has a - // bunch of messages suddenly disappear. This notice is - // only useful when muting from the keyboard, since you - // know what you did if you triggered muting with the - // mouse. - feedback_widget.show({ - populate($container) { - const rendered_html = render_topic_muted(); - $container.html(rendered_html); - $container.find(".stream").text(stream_name); - $container.find(".topic").text(topic); - }, - on_undo() { - unmute_topic(stream_id, topic); - }, - title_text: $t({defaultMessage: "Topic muted"}), - undo_button_text: $t({defaultMessage: "Unmute"}), - }); - }, - }); -} - -export function unmute_topic(stream_id, topic) { - // Accidentally unmuting a topic isn't as much an issue as accidentally muting - // a topic, so we don't show a popup after unmuting. - const data = { - stream_id, - topic, - op: "remove", - }; - - channel.patch({ - url: "/json/users/me/subscriptions/muted_topics", - data, - success() { - feedback_widget.dismiss(); - }, - }); -} - export function toggle_topic_mute(message) { const stream_id = message.stream_id; const topic = message.topic; if (user_topics.is_topic_muted(stream_id, topic)) { - unmute_topic(stream_id, topic); + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.INHERIT, + ); } else if (message.type === "stream") { - mute_topic(stream_id, topic, true); + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.MUTED, + true, + ); } } @@ -117,8 +62,16 @@ export function mute_or_unmute_topic($elt, mute) { const stream_id = Number.parseInt($elt.attr("data-stream-id"), 10); const topic = $elt.attr("data-topic-name"); if (mute) { - mute_topic(stream_id, topic); + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.MUTED, + ); } else { - unmute_topic(stream_id, topic); + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.INHERIT, + ); } } diff --git a/web/src/settings_muted_topics.js b/web/src/settings_muted_topics.js index 8c9fee4b0c..4cfb8fe9a8 100644 --- a/web/src/settings_muted_topics.js +++ b/web/src/settings_muted_topics.js @@ -3,7 +3,6 @@ import $ from "jquery"; import render_muted_topic_ui_row from "../templates/muted_topic_ui_row.hbs"; import * as ListWidget from "./list_widget"; -import * as muted_topics_ui from "./muted_topics_ui"; import * as ui from "./ui"; import * as user_topics from "./user_topics"; @@ -42,7 +41,11 @@ export function set_up() { e.stopPropagation(); - muted_topics_ui.unmute_topic(stream_id, topic); + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.INHERIT, + ); }); populate_list(); diff --git a/web/src/user_topics.js b/web/src/user_topics.js index c6bd112e3f..53b938facd 100644 --- a/web/src/user_topics.js +++ b/web/src/user_topics.js @@ -1,5 +1,10 @@ +import render_topic_muted from "../templates/topic_muted.hbs"; + import * as blueslip from "./blueslip"; +import * as channel from "./channel"; +import * as feedback_widget from "./feedback_widget"; import {FoldDict} from "./fold_dict"; +import {$t} from "./i18n"; import {page_params} from "./page_params"; import * as stream_data from "./stream_data"; import * as timerender from "./timerender"; @@ -58,6 +63,54 @@ export function get_muted_topics() { return topics; } +export function set_user_topic_visibility_policy(stream_id, topic, visibility_policy, from_hotkey) { + const data = { + stream_id, + topic, + visibility_policy, + }; + + channel.post({ + url: "/json/user_topics", + data, + success() { + if (visibility_policy === all_visibility_policies.INHERIT) { + feedback_widget.dismiss(); + return; + } + if (!from_hotkey) { + return; + } + + // The following feedback_widget notice helps avoid + // confusion when a user who is not familiar with Zulip's + // keyboard UI hits "M" in the wrong context and has a + // bunch of messages suddenly disappear. This notice is + // only useful when muting from the keyboard, since you + // know what you did if you triggered muting with the + // mouse. + const stream_name = stream_data.maybe_get_stream_name(stream_id); + feedback_widget.show({ + populate($container) { + const rendered_html = render_topic_muted(); + $container.html(rendered_html); + $container.find(".stream").text(stream_name); + $container.find(".topic").text(topic); + }, + on_undo() { + set_user_topic_visibility_policy( + stream_id, + topic, + all_visibility_policies.INHERIT, + ); + }, + title_text: $t({defaultMessage: "Topic muted"}), + undo_button_text: $t({defaultMessage: "Undo mute"}), + }); + }, + }); +} + export function set_user_topic(user_topic) { const stream_id = user_topic.stream_id; const topic = user_topic.topic_name; diff --git a/web/tests/settings_muted_topics.test.js b/web/tests/settings_muted_topics.test.js index aaac3eb013..9e294ff411 100644 --- a/web/tests/settings_muted_topics.test.js +++ b/web/tests/settings_muted_topics.test.js @@ -7,7 +7,6 @@ const {run_test} = require("./lib/test"); const $ = require("./lib/zjquery"); const list_widget = mock_esm("../src/list_widget"); -const muted_topics_ui = mock_esm("../src/muted_topics_ui"); const settings_muted_topics = zrequire("settings_muted_topics"); const stream_data = zrequire("stream_data"); @@ -73,7 +72,7 @@ run_test("settings", ({override}) => { }; let unmute_topic_called = false; - muted_topics_ui.unmute_topic = (stream_id, topic) => { + user_topics.set_user_topic_visibility_policy = (stream_id, topic) => { assert.equal(stream_id, frontend.stream_id); assert.equal(topic, "js"); unmute_topic_called = true;