2016-08-05 18:10:08 -04:00
|
|
|
class SpamService
|
2016-08-09 13:43:47 -04:00
|
|
|
attr_accessor :spammable, :request, :options
|
2017-01-27 11:25:39 -05:00
|
|
|
attr_reader :spam_log
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def initialize(spammable, request = nil)
|
2016-08-05 18:10:08 -04:00
|
|
|
@spammable = spammable
|
2016-08-09 13:43:47 -04:00
|
|
|
@request = request
|
|
|
|
@options = {}
|
|
|
|
|
|
|
|
if @request
|
|
|
|
@options[:ip_address] = @request.env['action_dispatch.remote_ip'].to_s
|
|
|
|
@options[:user_agent] = @request.env['HTTP_USER_AGENT']
|
|
|
|
@options[:referrer] = @request.env['HTTP_REFERRER']
|
|
|
|
else
|
|
|
|
@options[:ip_address] = @spammable.ip_address
|
|
|
|
@options[:user_agent] = @spammable.user_agent
|
|
|
|
end
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def check(api = false)
|
|
|
|
return false unless request && check_for_spam?
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
return false unless akismet.is_spam?
|
|
|
|
|
|
|
|
create_spam_log(api)
|
2016-08-05 18:10:08 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def mark_as_spam!
|
|
|
|
return false unless spammable.submittable_as_spam?
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
if akismet.submit_spam
|
|
|
|
spammable.user_agent_detail.update_attribute(:submitted, true)
|
2016-08-05 18:10:08 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
private
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def akismet
|
|
|
|
@akismet ||= AkismetService.new(
|
|
|
|
spammable_owner,
|
|
|
|
spammable.spammable_text,
|
|
|
|
options
|
|
|
|
)
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def spammable_owner
|
|
|
|
@user ||= User.find(spammable_owner_id)
|
|
|
|
end
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def spammable_owner_id
|
|
|
|
@owner_id ||=
|
|
|
|
if spammable.respond_to?(:author_id)
|
|
|
|
spammable.author_id
|
|
|
|
elsif spammable.respond_to?(:creator_id)
|
|
|
|
spammable.creator_id
|
|
|
|
end
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def check_for_spam?
|
|
|
|
spammable.check_for_spam?
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def create_spam_log(api)
|
2017-01-27 11:25:39 -05:00
|
|
|
@spam_log = SpamLog.create!(
|
2016-08-05 18:10:08 -04:00
|
|
|
{
|
2016-08-09 13:43:47 -04:00
|
|
|
user_id: spammable_owner_id,
|
2016-08-05 18:10:08 -04:00
|
|
|
title: spammable.spam_title,
|
|
|
|
description: spammable.spam_description,
|
2016-08-09 13:43:47 -04:00
|
|
|
source_ip: options[:ip_address],
|
|
|
|
user_agent: options[:user_agent],
|
2016-08-05 18:10:08 -04:00
|
|
|
noteable_type: spammable.class.to_s,
|
|
|
|
via_api: api
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|