A platform for community discussion. Free, open, simple.
FEATURE: allow user to override watched_precedence_over_muted setting (#22340)
Krzysztof Kotlarek134dcdd63a0b
diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/tracking.js b/app/assets/javascripts/discourse/app/controllers/preferences/tracking.jsindex 0900a9fc6ff..2f515b94253 100644--- a/app/assets/javascripts/discourse/app/controllers/preferences/tracking.js+++ b/app/assets/javascripts/discourse/app/controllers/preferences/tracking.js@@ -101,6 +101,20 @@ export default class extends Controller {).filter((t) => t);}+ @computed(+ "model.watchedCategories",+ "model.mutedCategories",+ "model.watched_tags.[]",+ "model.muted_tags.[]"+ )+ get showMutePrecedenceSetting() {+ return (+ (this.model.watchedCategories?.length > 0 &&+ this.model.muted_tags?.length > 0) ||+ (this.model.watched_tags?.length > 0 &&+ this.model.mutedCategories?.length > 0)+ );+ }@computed("model.watchedCategories",@@ -147,6 +161,7 @@ export default class extends Controller {"watched_category_ids","tracked_category_ids","watched_first_post_category_ids",+ "watched_precedence_over_muted",];if (this.siteSettings.tagging_enabled) {diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.jsindex 3bf74e96858..435c2faa33e 100644--- a/app/assets/javascripts/discourse/app/models/user.js+++ b/app/assets/javascripts/discourse/app/models/user.js@@ -131,6 +131,7 @@ let userOptionFields = ["bookmark_auto_delete_preference","sidebar_link_to_filtered_list","sidebar_show_count_of_new_items",+ "watched_precedence_over_muted",];export function addSaveableUserOptionField(fieldName) {diff --git a/app/assets/javascripts/discourse/app/templates/preferences/tracking.hbs b/app/assets/javascripts/discourse/app/templates/preferences/tracking.hbsindex 5c95660f7de..2756e30642d 100644--- a/app/assets/javascripts/discourse/app/templates/preferences/tracking.hbs+++ b/app/assets/javascripts/discourse/app/templates/preferences/tracking.hbs@@ -72,6 +72,15 @@/></div></div>+{{#if this.showMutePrecedenceSetting}}+ <div class="control-group user-preferences__watched-precedence-over-muted">+ <PreferenceCheckbox+ data-setting-name="watched-precedence-over-muted"+ @labelKey="user.watched_precedence_over_muted"+ @checked={{this.model.user_option.watched_precedence_over_muted}}+ />+ </div>+{{/if}}{{#if this.canSave}}<SaveControlsdiff --git a/app/models/user.rb b/app/models/user.rbindex 04010c61f4a..981f6dd2d65 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -1811,6 +1811,14 @@ class User < ActiveRecord::Basein_any_groups?(SiteSetting.experimental_search_menu_groups_map)end+ def watched_precedence_over_muted+ if user_option.watched_precedence_over_muted.nil?+ SiteSetting.watched_precedence_over_muted+ else+ user_option.watched_precedence_over_muted+ end+ end+protecteddef badge_grantdiff --git a/app/models/user_option.rb b/app/models/user_option.rbindex 7b76591237a..95e4047281f 100644--- a/app/models/user_option.rb+++ b/app/models/user_option.rb@@ -289,6 +289,7 @@ end# chat_header_indicator_preference :integer default(0), not null# sidebar_link_to_filtered_list :boolean default(FALSE), not null# sidebar_show_count_of_new_items :boolean default(FALSE), not null+# watched_precedence_over_muted :boolean## Indexes#diff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rbindex 1eba9b30951..562a06ba8c3 100644--- a/app/serializers/user_option_serializer.rb+++ b/app/serializers/user_option_serializer.rb@@ -38,7 +38,8 @@ class UserOptionSerializer < ApplicationSerializer:oldest_search_log_date,:seen_popups,:sidebar_link_to_filtered_list,- :sidebar_show_count_of_new_items+ :sidebar_show_count_of_new_items,+ :watched_precedence_over_muteddef auto_track_topics_after_msecsobject.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecsdiff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex a17e675da2f..25a2b982d82 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -200,9 +200,7 @@ class PostAlerterDiscourseEvent.trigger(:post_alerter_before_post, post, new_record, notified)- if !SiteSetting.watched_precedence_over_muted- notified = notified + category_or_tag_muters(post.topic)- end+ notified = notified + category_or_tag_muters(post.topic)if new_recordif post.topic.private_message?@@ -275,7 +273,12 @@ class PostAlerterUNIONSELECT user_id FROM tag_users tu JOIN topic_tags tt ON tt.tag_id = tu.tag_id AND tt.topic_id = #{topic.id} AND tu.notification_level = #{TagUser.notification_levels[:muted]}SQL- User.where("id IN (#{user_ids_sql})")+ User+ .where("id IN (#{user_ids_sql})")+ .joins("LEFT JOIN user_options ON user_options.user_id = users.id")+ .where(+ "user_options.watched_precedence_over_muted IS false OR (user_options.watched_precedence_over_muted IS NULL AND #{!SiteSetting.watched_precedence_over_muted})",+ )enddef notify_first_post_watchers(post, user_ids, notified = nil)diff --git a/app/services/user_updater.rb b/app/services/user_updater.rbindex b660728ef5d..4ceee214d22 100644--- a/app/services/user_updater.rb+++ b/app/services/user_updater.rb@@ -51,6 +51,7 @@ class UserUpdaterbookmark_auto_delete_preferencesidebar_link_to_filtered_listsidebar_show_count_of_new_items+ watched_precedence_over_muted]NOTIFICATION_SCHEDULE_ATTRS = -> dodiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 6de7e9f2228..6ae5abc6d18 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -1248,6 +1248,7 @@ en:watched_first_post_categories_instructions: "You will be notified of the first post in each new topic in these categories."watched_first_post_tags: "Watching First Post"watched_first_post_tags_instructions: "You will be notified of the first post in each new topic with these tags."+ watched_precedence_over_muted: "Notify me about topics in categories or tags I’m watching that also belong to one I have muted"muted_categories: "Muted"muted_categories_instructions: "You will not be notified of anything about new topics in these categories, and they will not appear on the categories or latest pages."diff --git a/db/migrate/20230628062236_add_watched_precedence_over_muted_to_user_options.rb b/db/migrate/20230628062236_add_watched_precedence_over_muted_to_user_options.rbnew file mode 100644index 00000000000..ed12eab9b98--- /dev/null+++ b/db/migrate/20230628062236_add_watched_precedence_over_muted_to_user_options.rb@@ -0,0 +1,6 @@+# frozen_string_literal: true+class AddWatchedPrecedenceOverMutedToUserOptions < ActiveRecord::Migration[7.0]+ def change+ add_column :user_options, :watched_precedence_over_muted, :boolean+ end+enddiff --git a/lib/topic_query.rb b/lib/topic_query.rbindex 5998a6d1d8a..3409f754586 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -898,7 +898,7 @@ class TopicQueryif userwatched_tag_ids =- if SiteSetting.watched_precedence_over_muted+ if user.watched_precedence_over_mutedTagUser.where(user: user).where("notification_level >= ?", TopicUser.notification_levels[:watching])@@ -981,6 +981,19 @@ class TopicQueryendend+ query_params = { tag_ids: muted_tag_ids }++ if user && !opts[:skip_categories]+ query_params[:regular] = CategoryUser.notification_levels[:regular]++ query_params[:watching_or_infinite] = if user.watched_precedence_over_muted ||+ SiteSetting.watched_precedence_over_muted+ CategoryUser.notification_levels[:watching]+ else+ 99+ end+ end+if SiteSetting.remove_muted_tags_from_latest == "always"list =list.where(@@ -991,16 +1004,7 @@ class TopicQueryWHERE tt.tag_id IN (:tag_ids)AND tt.topic_id = topics.id#{user && !opts[:skip_categories] ? "AND COALESCE(category_users.notification_level, :regular) < :watching_or_infinite" : ""})",- tag_ids: muted_tag_ids,- regular: CategoryUser.notification_levels[:regular],- watching_or_infinite:- (- if SiteSetting.watched_precedence_over_muted- CategoryUser.notification_levels[:watching]- else- 99- end- ),+ query_params,)elselist =@@ -1013,16 +1017,7 @@ class TopicQueryAND tt.topic_id = topics.id)#{user && !opts[:skip_categories] ? "OR COALESCE(category_users.notification_level, :regular) >= :watching_or_infinite" : ""}) OR NOT EXISTS (SELECT 1 FROM topic_tags tt WHERE tt.topic_id = topics.id)",- tag_ids: muted_tag_ids,- regular: CategoryUser.notification_levels[:regular],- watching_or_infinite:- (- if SiteSetting.watched_precedence_over_muted- CategoryUser.notification_levels[:watching]- else- 99- end- ),+ query_params,)endenddiff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rbindex 603f9f039e0..e3f399ce467 100644--- a/spec/lib/topic_query_spec.rb+++ b/spec/lib/topic_query_spec.rb@@ -2160,5 +2160,18 @@ RSpec.describe TopicQuery doexpect(query.topics.map(&:id)).to contain_exactly(topic.id)endend++ context "when disabled but overridden by user" do+ it "returns topics even if category or tag is muted but another tag or category is watched" do+ SiteSetting.watched_precedence_over_muted = false+ user.user_option.update!(watched_precedence_over_muted: true)+ query = TopicQuery.new(user).list_latest+ expect(query.topics.map(&:id)).to contain_exactly(+ topic.id,+ topic_in_watched_category_and_muted_tag.id,+ topic_in_muted_category_and_watched_tag.id,+ )+ end+ endendenddiff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.jsonindex 01cb1a18b35..ed539d524f3 100644--- a/spec/requests/api/schemas/json/user_get_response.json+++ b/spec/requests/api/schemas/json/user_get_response.json@@ -777,6 +777,9 @@"sidebar_show_count_of_new_items": {"type": "boolean"},+ "watched_precedence_over_muted": {+ "type": ["boolean", "null"]+ },"seen_popups": {"type": ["array", "null"]}diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex 2e49e23bc45..b6d6a634969 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -1918,7 +1918,7 @@ RSpec.describe PostAlerter do)end- it "adds notification when watched_precedence_over_mute setting is true" do+ it "adds notification when watched_precedence_over_muted setting is true" doSiteSetting.watched_precedence_over_muted = trueexpect {PostAlerter.post_created(topic_with_muted_tag_and_watched_category.posts.first)@@ -1928,7 +1928,18 @@ RSpec.describe PostAlerter do}.to change { Notification.count }.by(1)end- it "does not add notification when watched_precedence_over_mute setting is false" do+ it "respects user option even if watched_precedence_over_muted site setting is true" do+ SiteSetting.watched_precedence_over_muted = true+ user.user_option.update!(watched_precedence_over_muted: false)+ expect {+ PostAlerter.post_created(topic_with_muted_tag_and_watched_category.posts.first)+ }.not_to change { Notification.count }+ expect {+ PostAlerter.post_created(topic_with_muted_category_and_watched_tag.posts.first)+ }.not_to change { Notification.count }+ end++ it "does not add notification when watched_precedence_over_muted setting is false" doSiteSetting.watched_precedence_over_muted = falseexpect {PostAlerter.post_created(topic_with_muted_tag_and_watched_category.posts.first)@@ -1940,6 +1951,17 @@ RSpec.describe PostAlerter doNotification.count}.by(1)end++ it "respects user option even if watched_precedence_over_muted site setting is false" do+ SiteSetting.watched_precedence_over_muted = false+ user.user_option.update!(watched_precedence_over_muted: true)+ expect {+ PostAlerter.post_created(topic_with_muted_tag_and_watched_category.posts.first)+ }.to change { Notification.count }.by(1)+ expect {+ PostAlerter.post_created(topic_with_muted_category_and_watched_tag.posts.first)+ }.to change { Notification.count }.by(1)+ endendcontext "with on change" do