A platform for community discussion. Free, open, simple.
FIX: User preferences page for tracking tags should show tag name (#37517)
Natalie Tay2d7f8062faff
diff --git a/app/models/tag_user.rb b/app/models/tag_user.rbindex 263600fd0c2..a8a9fc6e588 100644--- a/app/models/tag_user.rb+++ b/app/models/tag_user.rb@@ -229,29 +229,30 @@ class TagUser < ActiveRecord::Basemuted_tag_names = SiteSetting.default_tags_muted.split("|")- tag_name_to_id = Tag.where(name: default_tag_names + muted_tag_names).pluck(:name, :id).to_h+ tag_data = Tag.where(name: default_tag_names + muted_tag_names).pluck(:name, :id).to_hnotification_levels =default_tag_names.filter_map do |name|- id = tag_name_to_id[name]- [id, self.notification_levels[:regular]] if id+ id = tag_data[name]+ [id, { level: self.notification_levels[:regular], name: }] if idendnotification_levels +=muted_tag_names.filter_map do |name|- id = tag_name_to_id[name]- [id, self.notification_levels[:muted]] if id+ id = tag_data[name]+ [id, { level: self.notification_levels[:muted], name: }] if idendelsenotification_levels =TagUser.notification_level_visible- .where(user: user)+ .where(user:).joins(:tag)- .pluck("tags.id", :notification_level)+ .pluck("tags.id", "tags.name", :notification_level)+ .map { |id, name, level| [id, { level:, name: }] }end- Hash[*notification_levels.flatten]+ notification_levels.to_hendenddiff --git a/app/serializers/concerns/user_tag_notifications_mixin.rb b/app/serializers/concerns/user_tag_notifications_mixin.rbindex 74c56ec1db7..76978a32ed0 100644--- a/app/serializers/concerns/user_tag_notifications_mixin.rb+++ b/app/serializers/concerns/user_tag_notifications_mixin.rb@@ -22,9 +22,9 @@ module UserTagNotificationsMixinenddef tags_with_notification_level(lookup_level)- tag_user_notification_levels- .select { |id, level| level == TagUser.notification_levels[lookup_level] }- .keys+ tag_user_notification_levels.filter_map do |id, data|+ { id:, name: data[:name] } if data[:level] == TagUser.notification_levels[lookup_level]+ endenddef tag_user_notification_levelsdiff --git a/frontend/discourse/app/models/topic-tracking-state.js b/frontend/discourse/app/models/topic-tracking-state.jsindex e6c3afee402..bc1200307b2 100644--- a/frontend/discourse/app/models/topic-tracking-state.js+++ b/frontend/discourse/app/models/topic-tracking-state.js@@ -37,10 +37,11 @@ function isUnseen(topic) {return !topic.is_seen;}-function hasMutedTags(topicTagIds, mutedTagIds, siteSettings) {- if (!mutedTagIds || !topicTagIds) {+function hasMutedTags(topicTagIds, mutedTags, siteSettings) {+ if (!mutedTags || !topicTagIds) {return false;}+ const mutedTagIds = mutedTags.map((t) => t.id);return ((siteSettings.remove_muted_tags_from_latest === "always" &&topicTagIds.some((tagId) => mutedTagIds.includes(tagId))) ||diff --git a/frontend/discourse/tests/unit/models/topic-tracking-state-test.js b/frontend/discourse/tests/unit/models/topic-tracking-state-test.jsindex bb8f7b83699..9fdbac55548 100644--- a/frontend/discourse/tests/unit/models/topic-tracking-state-test.js+++ b/frontend/discourse/tests/unit/models/topic-tracking-state-test.js@@ -1056,7 +1056,7 @@ module("Unit | Model | topic-tracking-state | /new", function (hooks) {});test("topics in muted tags do not get added to the state", async function (assert) {- this.currentUser.set("muted_tags", [44]);+ this.currentUser.set("muted_tags", [{ id: 44, name: "muted-tag" }]);await publishToMessageBus("/new", newTopicPayload);diff --git a/spec/models/tag_user_spec.rb b/spec/models/tag_user_spec.rbindex 370beb8ec49..d413811af33 100644--- a/spec/models/tag_user_spec.rb+++ b/spec/models/tag_user_spec.rb@@ -366,10 +366,10 @@ RSpec.describe TagUser doendit "every tag from the default_tags_* site settings get overridden to watching_first_post, except for muted" dolevels = TagUser.notification_levels_for(user)- expect(levels[tag1.id]).to eq(TagUser.notification_levels[:regular])- expect(levels[tag2.id]).to eq(TagUser.notification_levels[:regular])- expect(levels[tag3.id]).to eq(TagUser.notification_levels[:regular])- expect(levels[tag4.id]).to eq(TagUser.notification_levels[:muted])+ expect(levels[tag1.id][:level]).to eq(TagUser.notification_levels[:regular])+ expect(levels[tag2.id][:level]).to eq(TagUser.notification_levels[:regular])+ expect(levels[tag3.id][:level]).to eq(TagUser.notification_levels[:regular])+ expect(levels[tag4.id][:level]).to eq(TagUser.notification_levels[:muted])endend@@ -402,10 +402,10 @@ RSpec.describe TagUser doinclude tags the user is not tracking at all" dotag5 = Fabricate(:tag)levels = TagUser.notification_levels_for(user)- expect(levels[tag1.id]).to eq(TagUser.notification_levels[:watching])- expect(levels[tag2.id]).to eq(TagUser.notification_levels[:tracking])- expect(levels[tag3.id]).to eq(TagUser.notification_levels[:watching_first_post])- expect(levels[tag4.id]).to eq(TagUser.notification_levels[:muted])+ expect(levels[tag1.id][:level]).to eq(TagUser.notification_levels[:watching])+ expect(levels[tag2.id][:level]).to eq(TagUser.notification_levels[:tracking])+ expect(levels[tag3.id][:level]).to eq(TagUser.notification_levels[:watching_first_post])+ expect(levels[tag4.id][:level]).to eq(TagUser.notification_levels[:muted])expect(levels.key?(tag5.id)).to eq(false)enddiff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rbindex 0887749bd46..81b3c9ea8a1 100644--- a/spec/requests/tags_controller_spec.rb+++ b/spec/requests/tags_controller_spec.rb@@ -1609,7 +1609,7 @@ RSpec.describe TagsController doexpect(response.status).to eq(200)expect(response.parsed_body["watched_tags"]).to eq([])expect(response.parsed_body["watching_first_post_tags"]).to eq([])- expect(response.parsed_body["tracked_tags"]).to eq([tag.id])+ expect(response.parsed_body["tracked_tags"]).to eq([{ "id" => tag.id, "name" => tag.name }])expect(response.parsed_body["muted_tags"]).to eq([])expect(response.parsed_body["regular_tags"]).to eq([])@@ -1630,7 +1630,7 @@ RSpec.describe TagsController doexpect(response.parsed_body["watched_tags"]).to eq([])expect(response.parsed_body["watching_first_post_tags"]).to eq([])expect(response.parsed_body["tracked_tags"]).to eq([])- expect(response.parsed_body["muted_tags"]).to eq([tag.id])+ expect(response.parsed_body["muted_tags"]).to eq([{ "id" => tag.id, "name" => tag.name }])expect(response.parsed_body["regular_tags"]).to eq([])end.to change { user.tag_users.count }.by(1)diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex 456d6116c72..0dc68d59cea 100644--- a/spec/serializers/current_user_serializer_spec.rb+++ b/spec/serializers/current_user_serializer_spec.rb@@ -75,9 +75,9 @@ RSpec.describe CurrentUserSerializer do)end- it "includes muted tag ids" do+ it "includes muted tags" dopayload = serializer.as_json- expect(payload[:muted_tags]).to eq([tag.id])+ expect(payload[:muted_tags]).to eq([{ id: tag.id, name: tag.name }])endenddiff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rbindex 5859d9b7c5e..39ca971c0a5 100644--- a/spec/serializers/user_serializer_spec.rb+++ b/spec/serializers/user_serializer_spec.rb@@ -173,10 +173,10 @@ RSpec.describe UserSerializer donotification_level: TagUser.notification_levels[:watching_first_post],)- expect(json[:muted_tags]).to eq([tag1.id])- expect(json[:tracked_tags]).to eq([tag2.id])- expect(json[:watched_tags]).to eq([tag3.id])- expect(json[:watching_first_post_tags]).to eq([tag4.id])+ expect(json[:muted_tags]).to eq([{ id: tag1.id, name: tag1.name }])+ expect(json[:tracked_tags]).to eq([{ id: tag2.id, name: tag2.name }])+ expect(json[:watched_tags]).to eq([{ id: tag3.id, name: tag3.name }])+ expect(json[:watching_first_post_tags]).to eq([{ id: tag4.id, name: tag4.name }])endenddiff --git a/spec/system/page_objects/components/select_kit.rb b/spec/system/page_objects/components/select_kit.rbindex de14dc4556a..f9ae2f73854 100644--- a/spec/system/page_objects/components/select_kit.rb+++ b/spec/system/page_objects/components/select_kit.rb@@ -66,6 +66,11 @@ module PageObjectscomponent.find(".selected-choice[data-name='#{name}']")end+ def has_selected_names?(*names)+ selected = component.find(".formatted-selection").text.split(", ")+ names.map(&:to_s).sort == selected.sort+ end+def has_option_name?(name)component.find(".select-kit-collection li[data-name='#{name}']")enddiff --git a/spec/system/user_page/user_preferences_tracking_spec.rb b/spec/system/user_page/user_preferences_tracking_spec.rbnew file mode 100644index 00000000000..4dcfc1768b2--- /dev/null+++ b/spec/system/user_page/user_preferences_tracking_spec.rb@@ -0,0 +1,33 @@+# frozen_string_literal: true++RSpec.describe "User preferences tracking page", type: :system do+ fab!(:user)+ fab!(:tag1) { Fabricate(:tag, name: "javascript") }+ fab!(:tag2) { Fabricate(:tag, name: "ruby") }+ fab!(:tag3) { Fabricate(:tag, name: "python") }+ fab!(:tag4) { Fabricate(:tag, name: "golang") }++ let(:watched_tags) do+ PageObjects::Components::SelectKit.new(".tracking-controls__watched-tags .tag-chooser")+ end+ let(:muted_tags) do+ PageObjects::Components::SelectKit.new(".tracking-controls__muted-tags .tag-chooser")+ end++ before do+ SiteSetting.tagging_enabled = true+ sign_in(user)+ end++ it "displays tag names instead of IDs" do+ TagUser.create!(user:, tag: tag1, notification_level: TagUser.notification_levels[:watching])+ TagUser.create!(user:, tag: tag2, notification_level: TagUser.notification_levels[:watching])+ TagUser.create!(user:, tag: tag3, notification_level: TagUser.notification_levels[:muted])+ TagUser.create!(user:, tag: tag4, notification_level: TagUser.notification_levels[:muted])++ visit "/u/#{user.username}/preferences/tracking"++ expect(watched_tags).to have_selected_names("javascript", "ruby")+ expect(muted_tags).to have_selected_names("python", "golang")+ end+end