diff --git a/app/models/post.rb b/app/models/post.rb
index df3d51ed246..981a0ba9244 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -229,7 +229,6 @@ class Post < ActiveRecord::Base
end
def whitelisted_spam_hosts
-
hosts = SiteSetting
.white_listed_spam_host_domains
.split('|')
@@ -255,7 +254,8 @@ class Post < ActiveRecord::Base
TopicLink.where(domain: hosts.keys, user_id: acting_user.id)
.group(:domain, :post_id)
- .count.each_key do |tuple|
+ .count
+ .each_key do |tuple|
domain = tuple[0]
hosts[domain] = (hosts[domain] || 0) + 1
end
@@ -265,13 +265,9 @@ 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.has_trust_level?(TrustLevel[1])
+ return false if acting_user.present? && (acting_user.staged? || acting_user.has_trust_level?(TrustLevel[1]))
- total_hosts_usage.each do |_, count|
- return true if count >= SiteSetting.newuser_spam_host_threshold
- end
-
- false
+ total_hosts_usage.values.any? { |count| count >= SiteSetting.newuser_spam_host_threshold }
end
def archetype
diff --git a/app/models/user.rb b/app/models/user.rb
index 0909d61760c..db2e267ae0d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -702,12 +702,16 @@ class User < ActiveRecord::Base
# Flag all posts from a user as spam
def flag_linked_posts_as_spam
- admin = Discourse.system_user
+ disagreed_flag_post_ids = PostAction.where(post_action_type_id: PostActionType.types[:spam])
+ .where.not(disagreed_at: nil)
+ .pluck(:post_id)
- disagreed_flag_post_ids = PostAction.where(post_action_type_id: PostActionType.types[:spam]).where.not(disagreed_at: nil).pluck(:post_id)
- topic_links.includes(:post).where.not(post_id: disagreed_flag_post_ids).each do |tl|
+ topic_links.includes(:post)
+ .where.not(post_id: disagreed_flag_post_ids)
+ .each do |tl|
begin
- PostAction.act(admin, tl.post, PostActionType.types[:spam], message: I18n.t('flag_reason.spam_hosts'))
+ message = I18n.t('flag_reason.spam_hosts', domain: tl.domain)
+ PostAction.act(Discourse.system_user, tl.post, PostActionType.types[:spam], message: message)
rescue PostAction::AlreadyActed
# If the user has already acted, just ignore it
end
diff --git a/app/services/spam_rule/flag_sockpuppets.rb b/app/services/spam_rule/flag_sockpuppets.rb
index 4c96a0687fc..b669e80a8a2 100644
--- a/app/services/spam_rule/flag_sockpuppets.rb
+++ b/app/services/spam_rule/flag_sockpuppets.rb
@@ -30,10 +30,11 @@ class SpamRule::FlagSockpuppets
end
def flag_sockpuppet_users
- system_user = Discourse.system_user
- PostAction.act(system_user, @post, PostActionType.types[:spam], message: I18n.t('flag_reason.sockpuppet')) rescue PostAction::AlreadyActed
+ message = I18n.t('flag_reason.sockpuppet', ip_address: @post.user.ip_address)
+ PostAction.act(Discourse.system_user, @post, PostActionType.types[:spam], message: message) rescue PostAction::AlreadyActed
+
if (first_post = @post.topic.posts.by_post_number.first).try(:user).try(:new_user?)
- PostAction.act(system_user, first_post, PostActionType.types[:spam], message: I18n.t('flag_reason.sockpuppet')) rescue PostAction::AlreadyActed
+ PostAction.act(Discourse.system_user, first_post, PostActionType.types[:spam], message: message) rescue PostAction::AlreadyActed
end
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 78c60a071fa..55aff601897 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1134,7 +1134,7 @@ en:
tos_url: "If you have a Terms of Service document hosted elsewhere that you want to use, provide the full URL here."
privacy_policy_url: "If you have a Privacy Policy document hosted elsewhere that you want to use, provide the full URL here."
- newuser_spam_host_threshold: "How many times a new user can post a link to the same host within their `newuser_spam_host_posts` posts before being considered spam."
+ newuser_spam_host_threshold: "How many times a new user can post a link to the same host within their `newuser_spam_host_threshold` posts before being considered spam."
white_listed_spam_host_domains: "A list of domains excluded from spam host testing. New users will never be restricted from creating posts with links to these domains."
staff_like_weight: "How much extra weighting factor to give staff likes."
@@ -2389,8 +2389,8 @@ en:
missing: "Sorry, we can't find any avatar associated with that email address. Can you try uploading it again?"
flag_reason:
- sockpuppet: "A new user created a topic, and another new user at the same IP address replied. See the flag_sockpuppets site setting."
- spam_hosts: "This new user tried to create multiple posts with links to the same domain. See the newuser_spam_host_threshold site setting."
+ sockpuppet: "A new user created a topic, and another new user at the same IP address (%{ip_address}) replied. See the `flag_sockpuppets` site setting."
+ spam_hosts: "This new user tried to create multiple posts with links to the same domain (%{domain}). See the `newuser_spam_host_threshold` site setting."
email_log:
no_user: "Can't find user with id %{user_id}"
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 4d08038deec..762c1c0eaa5 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -283,7 +283,7 @@ module Discourse
SYSTEM_USER_ID ||= -1
def self.system_user
- User.find_by(id: SYSTEM_USER_ID)
+ @system_user ||= User.find_by(id: SYSTEM_USER_ID)
end
def self.store
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 39d9711cc33..9f81977ec5a 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -102,6 +102,7 @@ class PostCreator
setup_post
return true if skip_validations?
+
if @post.has_host_spam?
@spam = true
errors[:base] << I18n.t(:spamming_host)