From 92daf44daf0c7a7efbb751394cd674563c51ee28 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 4 Jul 2016 10:34:54 +1000 Subject: [PATCH] correct random suggested topic selection --- app/services/random_topic_selector.rb | 4 +++- lib/suggested_topics_builder.rb | 4 ---- .../suggested_topics_builder_spec.rb | 15 -------------- spec/components/topic_query_spec.rb | 20 +++++++++++++++++++ 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app/services/random_topic_selector.rb b/app/services/random_topic_selector.rb index 705bcd51d13..bb71d251e91 100644 --- a/app/services/random_topic_selector.rb +++ b/app/services/random_topic_selector.rb @@ -4,7 +4,6 @@ class RandomTopicSelector BACKFILL_LOW_WATER_MARK = 500 def self.backfill(category=nil) - exclude = category.try(:topic_id) # don't leak private categories into the "everything" group @@ -20,8 +19,11 @@ class RandomTopicSelector options[:category] = category.id if category query = TopicQuery.new(user, options) + + results = query.latest_results.order('RANDOM()') .where(closed: false, archived: false) + .where("topics.created_at > ?", SiteSetting.suggested_topics_max_days_old.days.ago) .limit(BACKFILL_SIZE) .reorder('RANDOM()') .pluck(:id) diff --git a/lib/suggested_topics_builder.rb b/lib/suggested_topics_builder.rb index 0fcce2671c3..33dc6007083 100644 --- a/lib/suggested_topics_builder.rb +++ b/lib/suggested_topics_builder.rb @@ -25,10 +25,6 @@ class SuggestedTopicsBuilder if @category_id && SiteSetting.limit_suggested_to_category? results = results.where(category_id: @category_id) end - - # Suggested topics should not be more than n days old - results = results.where("topics.created_at > ?", SiteSetting.suggested_topics_max_days_old.days.ago) - results = results.to_a.reject { |topic| @category_topic_ids.include?(topic.id) } unless results.empty? diff --git a/spec/components/suggested_topics_builder_spec.rb b/spec/components/suggested_topics_builder_spec.rb index a87db194024..c860fe0dc11 100644 --- a/spec/components/suggested_topics_builder_spec.rb +++ b/spec/components/suggested_topics_builder_spec.rb @@ -95,21 +95,6 @@ describe SuggestedTopicsBuilder do end end - context "adding topics that are more than n days old" do - let!(:new_topic) { Fabricate(:topic, created_at: 2.days.ago) } - let!(:old_topic) { Fabricate(:topic, created_at: 3.years.ago) } - - it "adds topics based on suggested_topics_max_days_old setting" do - SiteSetting.suggested_topics_max_days_old = 365 - - builder.add_results(Topic) - expect(builder.size).to eq(1) - expect(builder).not_to be_full - expect(builder.excluded_topic_ids.include?(old_topic.id)).to eq(false) - expect(builder.excluded_topic_ids.include?(new_topic.id)).to eq(true) - end - end - context "category definition topics" do let!(:category) { Fabricate(:category) } diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 8874bcabbe5..0b9b19f8547 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -620,6 +620,26 @@ describe TopicQuery do expect(suggested_topics).to be_blank end + context 'random suggested' do + + let!(:new_topic) { Fabricate(:topic, created_at: 2.days.ago) } + let!(:old_topic) { Fabricate(:topic, created_at: 3.years.ago) } + + it 'respects suggested_topics_max_days_old' do + SiteSetting.suggested_topics_max_days_old = 1365 + tt = topic + + RandomTopicSelector.clear_cache! + expect(topic_query.list_suggested_for(tt).topics.length).to eq(2) + + SiteSetting.suggested_topics_max_days_old = 365 + RandomTopicSelector.clear_cache! + + expect(topic_query.list_suggested_for(tt).topics.length).to eq(1) + end + + end + context 'with some existing topics' do let!(:partially_read) { Fabricate(:post, user: creator).topic } let!(:new_topic) { Fabricate(:post, user: creator).topic }