FEATURE: add `regular_categories` field in site setting & user option. (#10477)

FEATURE: add regular_categories field in site setting & user option. (#10477)

Like “default watching” and “default tracking” categories option now the “regular” categories support is added. It will be useful for sites that are muted by default. The user option will be displayed only if mute_all_categories_by_default site setting is enabled.

diff --git a/app/assets/javascripts/admin/mixins/setting-component.js b/app/assets/javascripts/admin/mixins/setting-component.js
index 44609e8..3b1ec45 100644
--- a/app/assets/javascripts/admin/mixins/setting-component.js
+++ b/app/assets/javascripts/admin/mixins/setting-component.js
@@ -173,6 +173,7 @@ export default Mixin.create({
         "default_categories_tracking",
         "default_categories_muted",
         "default_categories_watching_first_post",
+        "default_categories_regular",
         "default_tags_watching",
         "default_tags_tracking",
         "default_tags_muted",
diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/categories.js b/app/assets/javascripts/discourse/app/controllers/preferences/categories.js
index 13baaf6..b11b853 100644
--- a/app/assets/javascripts/discourse/app/controllers/preferences/categories.js
+++ b/app/assets/javascripts/discourse/app/controllers/preferences/categories.js
@@ -9,6 +9,7 @@ export default Controller.extend({
 
     this.saveAttrNames = [
       "muted_category_ids",
+      "regular_category_ids",
       "watched_category_ids",
       "tracked_category_ids",
       "watched_first_post_category_ids"
@@ -19,10 +20,13 @@ export default Controller.extend({
     "model.watchedCategories",
     "model.watchedFirstPostCategories",
     "model.trackedCategories",
+    "model.regularCategories",
     "model.mutedCategories"
   )
-  selectedCategories(watched, watchedFirst, tracked, muted) {
-    return [].concat(watched, watchedFirst, tracked, muted).filter(t => t);
+  selectedCategories(watched, watchedFirst, tracked, regular, muted) {
+    return []
+      .concat(watched, watchedFirst, tracked, regular, muted)
+      .filter(t => t);
   },
 
   @discourseComputed
diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js
index 2267727..33ef282 100644
--- a/app/assets/javascripts/discourse/app/models/user.js
+++ b/app/assets/javascripts/discourse/app/models/user.js
@@ -332,25 +332,27 @@ const User = RestModel.extend({
 
     var updatedState = {};
 
-    ["muted", "watched", "tracked", "watched_first_post"].forEach(s => {
-      if (fields === undefined || fields.includes(s + "_category_ids")) {
-        let prop =
-          s === "watched_first_post"
-            ? "watchedFirstPostCategories"
-            : s + "Categories";
-        let cats = this.get(prop);
-        if (cats) {
-          let cat_ids = cats.map(c => c.get("id"));
-          updatedState[s + "_category_ids"] = cat_ids;
-
-          // HACK: denote lack of categories
-          if (cats.length === 0) {
-            cat_ids = [-1];
+    ["muted", "regular", "watched", "tracked", "watched_first_post"].forEach(
+      s => {
+        if (fields === undefined || fields.includes(s + "_category_ids")) {
+          let prop =
+            s === "watched_first_post"
+              ? "watchedFirstPostCategories"
+              : s + "Categories";
+          let cats = this.get(prop);
+          if (cats) {
+            let cat_ids = cats.map(c => c.get("id"));
+            updatedState[s + "_category_ids"] = cat_ids;
+
+            // HACK: denote lack of categories
+            if (cats.length === 0) {
+              cat_ids = [-1];
+            }
+            data[s + "_category_ids"] = cat_ids;
           }
-          data[s + "_category_ids"] = cat_ids;
         }
       }
-    });
+    );
 
     [
       "muted_tags",
@@ -704,6 +706,14 @@ const User = RestModel.extend({
     this.set("mutedCategories", Category.findByIds(this.muted_category_ids));
   },
 
+  @observes("regular_category_ids")
+  updateRegularCategories() {
+    this.set(
+      "regularCategories",
+      Category.findByIds(this.regular_category_ids)
+    );
+  },
+
   @observes("tracked_category_ids")
   updateTrackedCategories() {
     this.set(
diff --git a/app/assets/javascripts/discourse/app/templates/preferences/categories.hbs b/app/assets/javascripts/discourse/app/templates/preferences/categories.hbs
index 7b36d0f..e76d19a 100644
--- a/app/assets/javascripts/discourse/app/templates/preferences/categories.hbs
+++ b/app/assets/javascripts/discourse/app/templates/preferences/categories.hbs
@@ -37,7 +37,17 @@
   </div>
   <div class="instructions">{{i18n "user.watched_first_post_categories_instructions"}}</div>
 
-  {{#unless siteSettings.mute_all_categories_by_default}}
+  {{#if siteSettings.mute_all_categories_by_default}}
+    <div class="controls tracking-controls">
+      <label>{{d-icon "d-regular"}} {{i18n "user.regular_categories"}}</label>
+      {{category-selector
+        categories=model.regularCategories
+        blocklist=selectedCategories
+        onChange=(action (mut model.regularCategories))
+      }}
+    </div>
+    <div class="instructions">{{i18n "user.regular_categories_instructions"}}</div>
+  {{else}}
     <div class="controls tracking-controls">
       <label>{{d-icon "d-muted"}} {{i18n "user.muted_categories"}}</label>
       {{#if canSee}}
@@ -50,7 +60,7 @@
       }}
     </div>
     <div class="instructions">{{i18n (if hideMutedTags "user.muted_categories_instructions" "user.muted_categories_instructions_dont_hide")}}</div>
-  {{/unless}}
+  {{/if}}
 </div>
 
 {{plugin-outlet name="user-preferences-categories" args=(hash model=model save=(action "save"))}}
diff --git a/app/controllers/admin/site_settings_controller.rb b/app/controllers/admin/site_settings_controller.rb
index 46b7409..3b7ac11 100644
--- a/app/controllers/admin/site_settings_controller.rb
+++ b/app/controllers/admin/site_settings_controller.rb
@@ -54,6 +54,8 @@ class Admin::SiteSettingsController < Admin::AdminController
           notification_level = NotificationLevels.all[:muted]
         when "default_categories_watching_first_post"
           notification_level = NotificationLevels.all[:watching_first_post]
+        when "default_categories_regular"
+          notification_level = NotificationLevels.all[:regular]
         end
 
         CategoryUser.where(category_id: (previous_category_ids - new_category_ids), notification_level: notification_level).delete_all
@@ -131,6 +133,8 @@ class Admin::SiteSettingsController < Admin::AdminController
         notification_level = NotificationLevels.all[:muted]
       when "default_categories_watching_first_post"
         notification_level = NotificationLevels.all[:watching_first_post]
+      when "default_categories_regular"
+        notification_level = NotificationLevels.all[:regular]
       end
 
       user_ids = CategoryUser.where(category_id: previous_category_ids - new_category_ids, notification_level: notification_level).distinct.pluck(:user_id)
diff --git a/app/models/category_user.rb b/app/models/category_user.rb
index afcd56c..04fe469 100644
--- a/app/models/category_user.rb
+++ b/app/models/category_user.rb
@@ -207,6 +207,7 @@ class CategoryUser < ActiveRecord::Base
         SiteSetting.default_categories_watching.split("|"),
         SiteSetting.default_categories_tracking.split("|"),
         SiteSetting.default_categories_watching_first_post.split("|"),
+        SiteSetting.default_categories_regular.split("|")
       ].flatten.map { |id| [id.to_i, self.notification_levels[:regular]] }
 
       notification_levels += SiteSetting.default_categories_muted.split("|").map { |id| [id.to_i, self.notification_levels[:muted]] }
diff --git a/app/models/user.rb b/app/models/user.rb
index 440ed65..7f64e5a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1448,7 +1448,7 @@ class User < ActiveRecord::Base
 
     values = []
 
-    %w{watching watching_first_post tracking muted}.each do |s|
+    %w{watching watching_first_post tracking regular muted}.each do |s|
       category_ids = SiteSetting.get("default_categories_#{s}").split("|").map(&:to_i)
       category_ids.each do |category_id|
         next if category_id == 0
diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb

[... diff too long, it was truncated ...]

GitHub sha: 8348a411

This commit appears in #10477 which was merged by vinothkannans.

copy:

“You will see these topics in the latest and top topic lists.”

1 Like

“List of categories that are not muted by default. Useful when mute_all_categories_by_default site setting is enabled.”

2 Likes

Overall looks good! thanks @vinothkannans

2 Likes

Follow-up: