Fix #4125: Detect forum and comment spam.
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
class DanbooruLogger
|
||||
def self.info(message, params = {})
|
||||
Rails.logger.info(message)
|
||||
|
||||
if defined?(::NewRelic)
|
||||
::NewRelic::Agent.record_custom_event(:spam, message: message, **params)
|
||||
end
|
||||
end
|
||||
|
||||
def self.log(exception, expected: false, **params)
|
||||
if expected
|
||||
Rails.logger.info("#{exception.class}: #{exception.message}")
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
class SpamDetector
|
||||
include Rakismet::Model
|
||||
|
||||
attr_accessor :user, :user_ip, :content, :comment_type
|
||||
attr_accessor :record, :user, :user_ip, :content, :comment_type
|
||||
rakismet_attrs author: proc { user.name },
|
||||
author_email: proc { user.email },
|
||||
blog_lang: "en",
|
||||
@@ -24,13 +24,26 @@ class SpamDetector
|
||||
false
|
||||
end
|
||||
|
||||
def initialize(record)
|
||||
def initialize(record, user_ip: nil)
|
||||
case record
|
||||
when Dmail
|
||||
@record = record
|
||||
@user = record.from
|
||||
@content = record.body
|
||||
@comment_type = "message"
|
||||
@user_ip = record.creator_ip_addr.to_s
|
||||
@user_ip = user_ip || record.creator_ip_addr.to_s
|
||||
when ForumPost
|
||||
@record = record
|
||||
@user = record.creator
|
||||
@content = record.body
|
||||
@comment_type = record.is_original_post? ? "forum-post" : "reply"
|
||||
@user_ip = user_ip
|
||||
when Comment
|
||||
@record = record
|
||||
@user = record.creator
|
||||
@content = record.body
|
||||
@comment_type = "comment"
|
||||
@user_ip = user_ip || record.creator_ip_addr.to_s
|
||||
else
|
||||
raise ArgumentError
|
||||
end
|
||||
@@ -39,6 +52,16 @@ class SpamDetector
|
||||
def spam?
|
||||
return false if !SpamDetector.enabled?
|
||||
return false if user.is_gold?
|
||||
super
|
||||
|
||||
is_spam = super
|
||||
|
||||
if is_spam
|
||||
DanbooruLogger.info("Spam detected: user_name=#{user.name} comment_type=#{comment_type} content=#{content.dump}", record.as_json)
|
||||
end
|
||||
|
||||
is_spam
|
||||
rescue => exception
|
||||
DanbooruLogger.log(exception)
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,6 +2,7 @@ class Comment < ApplicationRecord
|
||||
include Mentionable
|
||||
|
||||
validate :validate_creator_is_not_limited, :on => :create
|
||||
validate :validate_comment_is_not_spam, on: :create
|
||||
validates_presence_of :body, :message => "has no content"
|
||||
belongs_to :post
|
||||
belongs_to_creator
|
||||
@@ -124,6 +125,10 @@ class Comment < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def validate_comment_is_not_spam
|
||||
errors[:base] << "Failed to create comment" if SpamDetector.new(self).spam?
|
||||
end
|
||||
|
||||
def update_last_commented_at_on_create
|
||||
Post.where(:id => post_id).update_all(:last_commented_at => created_at)
|
||||
if Comment.where("post_id = ?", post_id).count <= Danbooru.config.comment_threshold && !do_not_bump_post?
|
||||
|
||||
@@ -15,6 +15,7 @@ class ForumPost < ApplicationRecord
|
||||
after_destroy :update_topic_updated_at_on_destroy
|
||||
validates_presence_of :body
|
||||
validate :validate_topic_is_unlocked
|
||||
validate :validate_post_is_not_spam, on: :create
|
||||
validate :topic_is_not_restricted, :on => :create
|
||||
before_destroy :validate_topic_is_unlocked
|
||||
after_save :delete_topic_if_original_post
|
||||
@@ -133,6 +134,10 @@ class ForumPost < ApplicationRecord
|
||||
votes.where(creator_id: user.id, score: score).exists?
|
||||
end
|
||||
|
||||
def validate_post_is_not_spam
|
||||
errors[:base] << "Failed to create forum post" if SpamDetector.new(self, user_ip: CurrentUser.ip_addr).spam?
|
||||
end
|
||||
|
||||
def validate_topic_is_unlocked
|
||||
return if CurrentUser.is_moderator?
|
||||
return if topic.nil?
|
||||
|
||||
Reference in New Issue
Block a user