diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index caa866b5d3e..6d49da6a6e0 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1036,6 +1036,7 @@ en: suggested_topics: "Number of suggested topics shown at the bottom of a topic." limit_suggested_to_category: "Only show topics from the current category in suggested topics." + suggested_topics_max_days_old: "Suggested topics should not be more than n days old." clean_up_uploads: "Remove orphan unreferenced uploads to prevent illegal hosting. WARNING: you may want to back up of your /uploads directory before enabling this setting." clean_orphan_uploads_grace_period_hours: "Grace period (in hours) before an orphan upload is removed." diff --git a/config/site_settings.yml b/config/site_settings.yml index 150b60fb135..dde76ceccd3 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -77,6 +77,10 @@ basic: min: 0 limit_suggested_to_category: default: false + suggested_topics_max_days_old: + default: 365 + min: 7 + max: 10000 track_external_right_clicks: client: true default: false diff --git a/lib/suggested_topics_builder.rb b/lib/suggested_topics_builder.rb index 73158495f86..0fcce2671c3 100644 --- a/lib/suggested_topics_builder.rb +++ b/lib/suggested_topics_builder.rb @@ -26,6 +26,9 @@ class SuggestedTopicsBuilder 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 c860fe0dc11..a87db194024 100644 --- a/spec/components/suggested_topics_builder_spec.rb +++ b/spec/components/suggested_topics_builder_spec.rb @@ -95,6 +95,21 @@ 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) }