Skip to content

A platform for community discussion. Free, open, simple.

FIX: Ensure suppressed categories do not produce any featured topics. (#7863)

Bianca Nenciu8e133de83118
diff --git a/app/models/category.rb b/app/models/category.rb
index 9ba9d914b5c..6009164df89 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -72,6 +72,7 @@ class Category < ActiveRecord::Base
after_save :clear_url_cache
after_save :index_search
after_save :update_reviewables
+ after_save :clear_featured_cache
after_destroy :reset_topic_ids_cache
after_destroy :publish_category_deletion
@@ -568,6 +569,10 @@ class Category < ActiveRecord::Base
@@url_cache.clear
end
+ def clear_featured_cache
+ CategoryFeaturedTopic.clear_exclude_category_ids
+ end
+
def full_slug(separator = "-")
start_idx = "#{Discourse.base_uri}/c/".length
url[start_idx..-1].gsub("/", separator)
diff --git a/app/models/category_featured_topic.rb b/app/models/category_featured_topic.rb
index 9ffb283b18f..fef24c07516 100644
--- a/app/models/category_featured_topic.rb
+++ b/app/models/category_featured_topic.rb
@@ -38,6 +38,16 @@ class CategoryFeaturedTopic < ActiveRecord::Base
end
end
+ @@exclude_category_ids = DistributedCache.new('excluded_category_ids_from_featured')
+
+ def self.cached_exclude_category_ids
+ @@exclude_category_ids['ids'] ||= Category.where(suppress_from_latest: true).pluck(:id)
+ end
+
+ def self.clear_exclude_category_ids
+ @@exclude_category_ids.clear
+ end
+
def self.clear_batch!
$redis.del(NEXT_CATEGORY_ID_KEY)
end
@@ -49,7 +59,8 @@ class CategoryFeaturedTopic < ActiveRecord::Base
per_page: c.num_featured_topics,
except_topic_ids: [c.topic_id],
visible: true,
- no_definitions: true
+ no_definitions: true,
+ exclude_category_ids: CategoryFeaturedTopic.cached_exclude_category_ids
}
# It may seem a bit odd that we are running 2 queries here, when admin
diff --git a/spec/models/category_featured_topic_spec.rb b/spec/models/category_featured_topic_spec.rb
index 0812237863b..f994fe53718 100644
--- a/spec/models/category_featured_topic_spec.rb
+++ b/spec/models/category_featured_topic_spec.rb
@@ -53,6 +53,20 @@ describe CategoryFeaturedTopic do
expect(CategoryFeaturedTopic.count).to be(1)
end
+ it 'should not include topics from suppressed categories' do
+ CategoryFeaturedTopic.feature_topics_for(category)
+ expect(
+ CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)
+ ).to contain_exactly(category_post.topic.id)
+
+ category.update(suppress_from_latest: true)
+
+ CategoryFeaturedTopic.feature_topics_for(category)
+ expect(
+ CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)
+ ).to_not contain_exactly(category_post.topic.id)
+ end
+
it 'should feature stuff in the correct order' do
category = Fabricate(:category, num_featured_topics: 2)
_t5 = Fabricate(:topic, category_id: category.id, bumped_at: 12.minutes.ago)