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

This commit is contained in:
Bianca Nenciu 2019-07-15 17:32:03 +03:00 committed by GitHub
parent 6a2f30c277
commit 8e133de831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)