diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 185ba7e106e..2c54ae1507c 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -84,7 +84,11 @@ class TopicsController < ApplicationController raise Discourse::InvalidParameters.new(:title) if title.length < SiteSetting.min_title_similar_length raise Discourse::InvalidParameters.new(:raw) if raw.length < SiteSetting.min_body_similar_length - topics = Topic.similar_to(title, raw, current_user) + # Only suggest similar topics if the site has a minimmum amount of topics present. + if Topic.count > SiteSetting.minimum_topics_similar + topics = Topic.similar_to(title, raw, current_user) + end + render_serialized(topics, BasicTopicSerializer) end diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index e12a9bd3764..10e19ceae86 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -222,6 +222,8 @@ class SiteSetting < ActiveRecord::Base client_setting(:topic_views_heat_medium, 2000) client_setting(:topic_views_heat_high, 5000) + setting(:minimum_topics_similar, 50) + def self.generate_api_key! self.api_key = SecureRandom.hex(32) end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 13b8f901e52..5649f1a48ef 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -383,7 +383,7 @@ en: memory_warning: 'Your server is running with less than 1 GB of total memory. At least 1 GB of memory is recommended.' facebook_config_warning: 'The server is configured to allow signup and log in with Facebook (enable_facebook_logins), but the app id and app secret values are not set. Go to the Site Settings and update the settings. See this guide to learn more.' cas_config_warning: 'The server is configured to allow signup and log in with CAS (enable_cas_logins), but the hostname and domain name values are not set.' - twitter_config_warning: 'The server is configured to allow signup and log in with Twitter (enable_twitter_logins), but the key and secret values are not set. Go to the Site Settings and update the settings. See this guide to learn more.' + twitter_config_warning: 'The server is configured to allow signup and log in with Twitter (enable_twitter_logins), but the key and secret values are not set. Go to the Site Settings and update the settings. See this guide to learn more.' github_config_warning: 'The server is configured to allow signup and log in with GitHub (enable_github_logins), but the client id and secret values are not set. Go to the Site Settings and update the settings. See this guide to learn more.' failing_emails_warning: 'There are %{num_failed_jobs} email jobs that failed. Check your config/environments/production.rb file and ensure that the config.action_mailer settings are correct. See the failed jobs in Sidekiq.' default_logo_warning: "You haven't customized the logo images for your site. Update logo_url, logo_small_url, and favicon_url in the Site Settings." @@ -623,6 +623,8 @@ en: pop3s_polling_username: "The username for the POP3S account to poll for email" pop3s_polling_password: "The password for the POP3S account to poll for email" + minimum_topics_similar: "How many topics need to exist in the database before similar topics are presented." + notification_types: mentioned: "%{display_username} mentioned you in %{link}" diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index ba910a33fc2..d8d08de8100 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -161,18 +161,40 @@ describe TopicsController do -> { xhr :get, :similar_to, title: title, raw: raw }.should raise_error(Discourse::InvalidParameters) end - it "delegates to Topic.similar_to" do - Topic.expects(:similar_to).with(title, raw, nil).returns([Fabricate(:topic)]) - xhr :get, :similar_to, title: title, raw: raw - end + describe "minimum_topics_similar" do - context "logged in" do - let(:user) { log_in } + before do + SiteSetting.stubs(:minimum_topics_similar).returns(30) + end - it "passes a user throught if logged in" do - Topic.expects(:similar_to).with(title, raw, user).returns([Fabricate(:topic)]) + after do xhr :get, :similar_to, title: title, raw: raw end + + describe "With enough topics" do + before do + Topic.stubs(:count).returns(50) + end + + it "deletes to Topic.similar_to if there are more topics than `minimum_topics_similar`" do + Topic.expects(:similar_to).with(title, raw, nil).returns([Fabricate(:topic)]) + end + + describe "with a logged in user" do + let(:user) { log_in } + + it "passes a user through if logged in" do + Topic.expects(:similar_to).with(title, raw, user).returns([Fabricate(:topic)]) + end + end + + end + + it "does not call Topic.similar_to if there are fewer topics than `minimum_topics_similar`" do + Topic.stubs(:count).returns(10) + Topic.expects(:similar_to).never + end + end end