diff --git a/app/models/post.rb b/app/models/post.rb index a4df5df7787..e967f46bd43 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -303,6 +303,7 @@ class Post < ActiveRecord::Base # Prevent new users from posting the same hosts too many times. def has_host_spam? return false if acting_user.present? && (acting_user.staged? || acting_user.has_trust_level?(TrustLevel[1])) + return false if topic&.private_message? total_hosts_usage.values.any? { |count| count >= SiteSetting.newuser_spam_host_threshold } end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index aff50f205d0..b6128472cc9 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -835,8 +835,10 @@ describe Post do end describe "has_host_spam" do + let(:raw) { "hello from my site http://www.somesite.com http://#{GlobalSetting.hostname} http://#{RailsMultisite::ConnectionManagement.current_hostname}" } + it "correctly detects host spam" do - post = Fabricate(:post, raw: "hello from my site http://www.somesite.com http://#{GlobalSetting.hostname} http://#{RailsMultisite::ConnectionManagement.current_hostname}") + post = Fabricate(:post, raw: raw) expect(post.total_hosts_usage).to eq("www.somesite.com" => 1) post.acting_user.trust_level = 0 @@ -850,6 +852,20 @@ describe Post do SiteSetting.white_listed_spam_host_domains = "bla.com|boo.com | somesite.com " expect(post.has_host_spam?).to eq(false) end + + it "doesn't punish staged users" do + SiteSetting.newuser_spam_host_threshold = 1 + user = Fabricate(:user, staged: true, trust_level: 0) + post = Fabricate(:post, raw: raw, user: user) + expect(post.has_host_spam?).to eq(false) + end + + it "ignores private messages" do + SiteSetting.newuser_spam_host_threshold = 1 + user = Fabricate(:user, trust_level: 0) + post = Fabricate(:post, raw: raw, user: user, topic: Fabricate(:private_message_topic, user: user)) + expect(post.has_host_spam?).to eq(false) + end end it "has custom fields" do