diff --git a/web/src/user_topics_ui.ts b/web/src/user_topics_ui.ts index 43f361bf8a..61aebe75d2 100644 --- a/web/src/user_topics_ui.ts +++ b/web/src/user_topics_ui.ts @@ -1,4 +1,5 @@ import $ from "jquery"; +import assert from "minimalistic-assert"; import * as inbox_util from "./inbox_util"; import * as message_lists from "./message_lists"; @@ -94,23 +95,33 @@ export function toggle_topic_visibility_policy(message: Message): void { return; } - if ( - user_topics.is_topic_muted(stream_id, topic) || - user_topics.is_topic_unmuted(stream_id, topic) - ) { - user_topics.set_user_topic_visibility_policy( - stream_id, - topic, - user_topics.all_visibility_policies.INHERIT, - ); - } else { - if (sub_store.get(stream_id)?.is_muted) { + const sub = sub_store.get(stream_id); + assert(sub !== undefined); + + if (sub.is_muted) { + if (user_topics.is_topic_unmuted_or_followed(stream_id, topic)) { + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.INHERIT, + true, + ); + } else { user_topics.set_user_topic_visibility_policy( stream_id, topic, user_topics.all_visibility_policies.UNMUTED, true, ); + } + } else { + if (user_topics.is_topic_muted(stream_id, topic)) { + user_topics.set_user_topic_visibility_policy( + stream_id, + topic, + user_topics.all_visibility_policies.INHERIT, + true, + ); } else { user_topics.set_user_topic_visibility_policy( stream_id, diff --git a/web/tests/user_topics_ui.test.js b/web/tests/user_topics_ui.test.js index 887f902a4a..65b4f29b61 100644 --- a/web/tests/user_topics_ui.test.js +++ b/web/tests/user_topics_ui.test.js @@ -8,6 +8,7 @@ const {run_test} = require("./lib/test"); const user_topics = zrequire("user_topics"); const user_topics_ui = zrequire("user_topics_ui"); const stream_data = zrequire("stream_data"); +const sub_store = zrequire("sub_store"); const design = { stream_id: 101, @@ -29,7 +30,7 @@ function update_visibility_policy(visibility_policy) { user_topics.update_user_topics(design.stream_id, design.name, "java", visibility_policy); } -test("toggle_topic_visibility_policy", () => { +test("toggle_topic_visibility_policy", ({override_rewire}) => { // Mute a topic assert.ok(!user_topics.is_topic_muted(design.stream_id, "java")); update_visibility_policy(user_topics.all_visibility_policies.MUTED); @@ -47,4 +48,57 @@ test("toggle_topic_visibility_policy", () => { // Verify that we can't toggle visibility policy in unsubscribed channel. user_topics_ui.toggle_topic_visibility_policy(message); assert.ok(user_topics.is_topic_muted(design.stream_id, "java")); + + override_rewire( + user_topics, + "set_user_topic_visibility_policy", + (stream_id, topic_name, visibility_policy) => { + const stream_name = sub_store.maybe_get_stream_name(stream_id); + user_topics.update_user_topics(stream_id, stream_name, topic_name, visibility_policy); + }, + ); + + design.subscribed = true; + + // For NOT muted channel + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok( + user_topics.get_topic_visibility_policy(design.stream_id, "java") === + user_topics.all_visibility_policies.INHERIT, + ); + + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok(user_topics.is_topic_muted(design.stream_id, "java")); + + update_visibility_policy(user_topics.all_visibility_policies.UNMUTED); + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok(user_topics.is_topic_muted(design.stream_id, "java")); + + update_visibility_policy(user_topics.all_visibility_policies.FOLLOWED); + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok(user_topics.is_topic_muted(design.stream_id, "java")); + + // For muted channel + design.is_muted = true; + + update_visibility_policy(user_topics.all_visibility_policies.INHERIT); + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok(user_topics.is_topic_unmuted(design.stream_id, "java")); + + update_visibility_policy(user_topics.all_visibility_policies.MUTED); + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok(user_topics.is_topic_unmuted(design.stream_id, "java")); + + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok( + user_topics.get_topic_visibility_policy(design.stream_id, "java") === + user_topics.all_visibility_policies.INHERIT, + ); + + update_visibility_policy(user_topics.all_visibility_policies.FOLLOWED); + user_topics_ui.toggle_topic_visibility_policy(message); + assert.ok( + user_topics.get_topic_visibility_policy(design.stream_id, "java") === + user_topics.all_visibility_policies.INHERIT, + ); });