2016-08-05 18:10:08 -04:00
|
|
|
class AkismetService
|
2016-08-09 13:43:47 -04:00
|
|
|
attr_accessor :owner, :text, :options
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def initialize(owner, text, options = {})
|
|
|
|
@owner = owner
|
|
|
|
@text = text
|
|
|
|
@options = options
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def is_spam?
|
|
|
|
return false unless akismet_enabled?
|
2016-08-05 18:10:08 -04:00
|
|
|
|
|
|
|
params = {
|
|
|
|
type: 'comment',
|
2016-08-09 13:43:47 -04:00
|
|
|
text: text,
|
2016-08-05 18:10:08 -04:00
|
|
|
created_at: DateTime.now,
|
2016-08-09 13:43:47 -04:00
|
|
|
author: owner.name,
|
|
|
|
author_email: owner.email,
|
2017-05-03 07:22:03 -04:00
|
|
|
referrer: options[:referrer]
|
2016-08-05 18:10:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
begin
|
2016-08-09 13:43:47 -04:00
|
|
|
is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
|
2016-08-05 18:10:08 -04:00
|
|
|
is_spam || is_blatant
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def submit_ham
|
2016-09-14 20:59:56 -04:00
|
|
|
submit(:ham)
|
|
|
|
end
|
2016-08-09 13:43:47 -04:00
|
|
|
|
2016-09-14 20:59:56 -04:00
|
|
|
def submit_spam
|
|
|
|
submit(:spam)
|
|
|
|
end
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-09-14 20:59:56 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def akismet_client
|
|
|
|
@akismet_client ||= ::Akismet::Client.new(current_application_settings.akismet_api_key,
|
|
|
|
Gitlab.config.gitlab.url)
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-09-14 20:59:56 -04:00
|
|
|
def akismet_enabled?
|
|
|
|
current_application_settings.akismet_enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def submit(type)
|
2016-08-09 13:43:47 -04:00
|
|
|
return false unless akismet_enabled?
|
|
|
|
|
2016-08-05 18:10:08 -04:00
|
|
|
params = {
|
|
|
|
type: 'comment',
|
2016-08-09 13:43:47 -04:00
|
|
|
text: text,
|
|
|
|
author: owner.name,
|
|
|
|
author_email: owner.email
|
2016-08-05 18:10:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
begin
|
2016-09-14 20:59:56 -04:00
|
|
|
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params)
|
2016-08-05 18:10:08 -04:00
|
|
|
true
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|