Replace the spam observer with simple after_save callbacks

This commit is contained in:
Tom Hughes 2013-06-25 21:54:02 +01:00
parent 2dc7c505fd
commit ac1f210736
5 changed files with 19 additions and 20 deletions

View file

@ -8,6 +8,7 @@ class DiaryComment < ActiveRecord::Base
attr_accessible :body attr_accessible :body
after_initialize :set_defaults after_initialize :set_defaults
after_save :spam_check
def body def body
RichText.new(read_attribute(:body_format), read_attribute(:body)) RichText.new(read_attribute(:body_format), read_attribute(:body))
@ -27,4 +28,8 @@ private
def set_defaults def set_defaults
self.body_format = "markdown" unless self.attribute_present?(:body_format) self.body_format = "markdown" unless self.attribute_present?(:body_format)
end end
def spam_check
user.spam_check
end
end end

View file

@ -27,6 +27,7 @@ class DiaryEntry < ActiveRecord::Base
attr_accessible :title, :body, :language_code, :latitude, :longitude attr_accessible :title, :body, :language_code, :latitude, :longitude
after_initialize :set_defaults after_initialize :set_defaults
after_save :spam_check
def body def body
RichText.new(read_attribute(:body_format), read_attribute(:body)) RichText.new(read_attribute(:body_format), read_attribute(:body))
@ -37,4 +38,8 @@ private
def set_defaults def set_defaults
self.body_format = "markdown" unless self.attribute_present?(:body_format) self.body_format = "markdown" unless self.attribute_present?(:body_format)
end end
def spam_check
user.spam_check
end
end end

View file

@ -1,15 +0,0 @@
class SpamObserver < ActiveRecord::Observer
observe User, DiaryEntry, DiaryComment
def after_save(record)
case
when record.is_a?(User) then user = record
when record.is_a?(DiaryEntry) then user = record.user
when record.is_a?(DiaryComment) then user = record.user
end
if user.status == "active" and user.spam_score > SPAM_THRESHOLD
user.update_column(:status, "suspended")
end
end
end

View file

@ -52,6 +52,7 @@ class User < ActiveRecord::Base
after_initialize :set_defaults after_initialize :set_defaults
before_save :encrypt_password before_save :encrypt_password
after_save :spam_check
has_attached_file :image, has_attached_file :image,
:default_url => "/assets/:class/:attachment/:style.png", :default_url => "/assets/:class/:attachment/:style.png",
@ -216,6 +217,14 @@ class User < ActiveRecord::Base
return score.to_i return score.to_i
end end
##
# perform a spam check on a user
def spam_check
if status == "active" and spam_score > SPAM_THRESHOLD
update_column(:status, "suspended")
end
end
## ##
# return an oauth access token for a specified application # return an oauth access token for a specified application
def access_token(application_key) def access_token(application_key)

View file

@ -32,11 +32,6 @@ module OpenStreetMap
# :all can be used as a placeholder for all plugins not explicitly named. # :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
unless STATUS == :database_offline
config.active_record.observers = :spam_observer
end
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)' # config.time_zone = 'Central Time (US & Canada)'