diff --git a/app/models/topic.rb b/app/models/topic.rb index e886d25a2c2..aa494fa1ea4 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -2035,6 +2035,10 @@ class Topic < ActiveRecord::Base end end + def visible_tags(guardian) + tags.reject { |tag| guardian.hidden_tag_names.include?(tag[:name]) } + end + private def invite_to_private_message(invited_by, target_user, guardian) diff --git a/app/serializers/concerns/topic_tags_mixin.rb b/app/serializers/concerns/topic_tags_mixin.rb index 0c001d46334..493ec0f6cce 100644 --- a/app/serializers/concerns/topic_tags_mixin.rb +++ b/app/serializers/concerns/topic_tags_mixin.rb @@ -26,17 +26,20 @@ module TopicTagsMixin def all_tags return @tags if defined?(@tags) + + tags = topic.visible_tags(scope) + # Calling method `pluck` or `order` along with `includes` causing N+1 queries tags = ( if SiteSetting.tags_sort_alphabetically - topic.tags.sort_by(&:name) + tags.sort_by(&:name) else topic_count_column = Tag.topic_count_column(scope) - topic.tags.sort_by { |tag| tag.public_send(topic_count_column) }.reverse + tags.sort_by { |tag| tag.public_send(topic_count_column) }.reverse end ) - tags = tags.reject { |tag| scope.hidden_tag_names.include?(tag[:name]) } if !scope.is_staff? + @tags = tags end end diff --git a/app/views/list/list.erb b/app/views/list/list.erb index d6ea1749631..8d35d0f0c87 100644 --- a/app/views/list/list.erb +++ b/app/views/list/list.erb @@ -70,11 +70,11 @@ <% end %> - <% if t.tags %> + <% if tags = t.visible_tags(guardian) %>
- <% t.tags.each_with_index do |tag, index| %> + <% tags.each_with_index do |tag, index| %> <%= tag.name %> - <% if index < t.tags.size - 1 %>, <% end %> + <% if index < tags.size - 1 %>, <% end %> <% end %>
<% end %> diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb index 547e0573139..b465b449626 100644 --- a/spec/requests/list_controller_spec.rb +++ b/spec/requests/list_controller_spec.rb @@ -151,6 +151,28 @@ RSpec.describe ListController do expect(new_sql_queries_count).to be <= initial_sql_queries_count end + + context "with topics with tags" do + let(:tag_group) { Fabricate.build(:tag_group) } + let(:tag_group_permission) { Fabricate.build(:tag_group_permission, tag_group: tag_group) } + let(:restricted_tag) { Fabricate(:tag) } + let(:public_tag) { Fabricate(:tag) } + + before do + tag_group.tag_group_permissions << tag_group_permission + tag_group.save! + tag_group_permission.tag_group.tags << restricted_tag + topic.tags << [public_tag, restricted_tag] + end + + it "does not show hidden tags" do + get "/latest" + + expect(response.status).to eq(200) + expect(response.body).to include(public_tag.name) + expect(response.body).not_to include(restricted_tag.name) + end + end end describe "categories and X" do