From 38c018a84bf4d75d051dae3da5a02d5f72344ec9 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 15 Jan 2018 16:13:29 +1100 Subject: [PATCH] FIX: invalid cache for parent category with limit_suggested_to_category --- app/services/random_topic_selector.rb | 18 +++++++++++++----- lib/topic_query.rb | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/services/random_topic_selector.rb b/app/services/random_topic_selector.rb index 4aaa7d3343f..1ac2d8500a9 100644 --- a/app/services/random_topic_selector.rb +++ b/app/services/random_topic_selector.rb @@ -6,9 +6,6 @@ class RandomTopicSelector def self.backfill(category = nil) exclude = category&.topic_id - # don't leak private categories into the "everything" group - user = category ? CategoryFeaturedTopic.fake_admin : nil - options = { per_page: category ? category.num_featured_topics : 3, visible: true, @@ -16,9 +13,20 @@ class RandomTopicSelector } options[:except_topic_ids] = [category.topic_id] if exclude - options[:category] = category.id if category - query = TopicQuery.new(user, options) + if category + options[:category] = category.id + # NOTE: at the moment this site setting scopes tightly to a category (excluding subcats) + # this is done so we don't populate a junk cache + if SiteSetting.limit_suggested_to_category + options[:no_subcategories] = true + end + + # don't leak private categories into the "everything" group + options[:guardian] = Guardian.new(CategoryFeaturedTopic.fake_admin) + end + + query = TopicQuery.new(nil, options) results = query.latest_results.order('RANDOM()') .where(closed: false, archived: false) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 572f19be085..3dae7132e09 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -43,6 +43,7 @@ class TopicQuery page per_page visible + guardian no_definitions) end @@ -90,7 +91,7 @@ class TopicQuery options.assert_valid_keys(TopicQuery.valid_options) @options = options.dup @user = user - @guardian = Guardian.new(@user) + @guardian = options[:guardian] || Guardian.new(@user) end def joined_topic_user(list = nil)