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

69 lines
1.5 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
2017-08-24 13:05:02 -04:00
def spam?
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,
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(Gitlab::CurrentSettings.akismet_api_key,
2016-09-14 20:59:56 -04:00
Gitlab.config.gitlab.url)
end
2016-09-14 20:59:56 -04:00
def akismet_enabled?
Gitlab::CurrentSettings.akismet_enabled
2016-09-14 20:59:56 -04:00
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
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params) # rubocop:disable GitlabSecurity/PublicSend
true
rescue => e
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
false
end
end
end