2020-03-09 13:42:32 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Spam
|
|
|
|
class SpamCheckService
|
|
|
|
include AkismetMethods
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
attr_accessor :target, :request, :options
|
2020-03-09 13:42:32 +05:30
|
|
|
attr_reader :spam_log
|
|
|
|
|
|
|
|
def initialize(spammable:, request:)
|
2020-04-08 14:13:33 +05:30
|
|
|
@target = spammable
|
2020-03-09 13:42:32 +05:30
|
|
|
@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
|
2020-04-08 14:13:33 +05:30
|
|
|
@options[:ip_address] = @target.ip_address
|
|
|
|
@options[:user_agent] = @target.user_agent
|
2020-03-09 13:42:32 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(api: false, recaptcha_verified:, spam_log_id:, user_id:)
|
|
|
|
if recaptcha_verified
|
|
|
|
# If it's a request which is already verified through recaptcha,
|
|
|
|
# update the spam log accordingly.
|
|
|
|
SpamLog.verify_recaptcha!(user_id: user_id, id: spam_log_id)
|
|
|
|
else
|
|
|
|
# Otherwise, it goes to Akismet for spam check.
|
|
|
|
# If so, it assigns spammable object as "spam" and creates a SpamLog record.
|
|
|
|
possible_spam = check(api)
|
2020-04-08 14:13:33 +05:30
|
|
|
target.spam = possible_spam unless target.allow_possible_spam?
|
|
|
|
target.spam_log = spam_log
|
2020-03-09 13:42:32 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check(api)
|
|
|
|
return unless request
|
|
|
|
return unless check_for_spam?
|
|
|
|
return unless akismet.spam?
|
|
|
|
|
|
|
|
create_spam_log(api)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_for_spam?
|
2020-04-08 14:13:33 +05:30
|
|
|
target.check_for_spam?
|
2020-03-09 13:42:32 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_spam_log(api)
|
|
|
|
@spam_log = SpamLog.create!(
|
|
|
|
{
|
2020-04-08 14:13:33 +05:30
|
|
|
user_id: target.author_id,
|
|
|
|
title: target.spam_title,
|
|
|
|
description: target.spam_description,
|
2020-03-09 13:42:32 +05:30
|
|
|
source_ip: options[:ip_address],
|
|
|
|
user_agent: options[:user_agent],
|
2020-04-08 14:13:33 +05:30
|
|
|
noteable_type: target.class.to_s,
|
2020-03-09 13:42:32 +05:30
|
|
|
via_api: api
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|