From 3e5fb90ce611c65c1f568aa795f29151fb2ff237 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Fri, 25 Feb 2022 13:08:22 +1100 Subject: [PATCH] FIX: new indirectly muted category (#16043) When a new category is created and the parent category is muted or indirectly muted, the new category should be indirectly muted as well. --- .../subscribe-user-notifications.js | 17 ++++++++- .../tests/acceptance/notifications-test.js | 36 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js index 2381e0886e..dba138be2b 100644 --- a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js +++ b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js @@ -117,7 +117,22 @@ export default { const router = container.lookup("router:main"); bus.subscribe("/categories", (data) => { - (data.categories || []).forEach((c) => site.updateCategory(c)); + (data.categories || []).forEach((c) => { + const mutedCategoryIds = user.muted_category_ids?.concat( + user.indirectly_muted_category_ids + ); + if ( + mutedCategoryIds && + mutedCategoryIds.includes(c.parent_category_id) && + !mutedCategoryIds.includes(c.id) + ) { + user.set( + "indirectly_muted_category_ids", + user.indirectly_muted_category_ids.concat(c.id) + ); + } + return site.updateCategory(c); + }); (data.deleted_categories || []).forEach((id) => site.removeCategory(id) ); diff --git a/app/assets/javascripts/discourse/tests/acceptance/notifications-test.js b/app/assets/javascripts/discourse/tests/acceptance/notifications-test.js index 34160f5666..55498b7a87 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/notifications-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/notifications-test.js @@ -8,6 +8,7 @@ import { queryAll, } from "discourse/tests/helpers/qunit-helpers"; import { test } from "qunit"; +import User from "discourse/models/user"; acceptance("User Notifications", function (needs) { needs.user(); @@ -197,6 +198,41 @@ acceptance("User Notifications", function (needs) { }); }); +acceptance("Category Notifications", function (needs) { + needs.user({ muted_category_ids: [1], indirectly_muted_category_ids: [2] }); + + test("New category is muted when parent category is muted", async function (assert) { + await visit("/"); + const user = User.current(); + publishToMessageBus("/categories", { + categories: [ + { + id: 3, + parent_category_id: 99, + }, + { + id: 4, + }, + ], + }); + assert.deepEqual(user.indirectly_muted_category_ids, [2]); + + publishToMessageBus("/categories", { + categories: [ + { + id: 4, + parent_category_id: 1, + }, + { + id: 5, + parent_category_id: 2, + }, + ], + }); + assert.deepEqual(user.indirectly_muted_category_ids, [2, 4, 5]); + }); +}); + acceptance( "User Notifications - there is no notifications yet", function (needs) {