gitlab-org--gitlab-foss/app/services/akismet_service.rb

69 lines
1.4 KiB
Ruby
Raw Normal View History

class AkismetService
2016-08-09 13:43:47 -04:00
attr_accessor :owner, :text, :options
2016-08-09 13:43:47 -04:00
def initialize(owner, text, options = {})
@owner = owner
@text = text
@options = options
end
2016-08-09 13:43:47 -04:00
def is_spam?
return false unless akismet_enabled?
params = {
type: 'comment',
2016-08-09 13:43:47 -04:00
text: text,
created_at: DateTime.now,
2016-08-09 13:43:47 -04:00
author: owner.name,
author_email: owner.email,
referrer: options[:referrer]
}
begin
2016-08-09 13:43:47 -04:00
is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
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-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)
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?
params = {
type: 'comment',
2016-08-09 13:43:47 -04:00
text: text,
author: owner.name,
author_email: owner.email
}
begin
2016-09-14 20:59:56 -04:00
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params)
true
rescue => e
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
false
end
end
end