debian-mirror-gitlab/app/services/akismet_service.rb

71 lines
1.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
class AkismetService
attr_accessor :owner, :text, :options
def initialize(owner, text, options = {})
@owner = owner
@text = text
@options = options
end
2018-03-17 18:26:18 +05:30
def spam?
2016-09-13 17:45:13 +05:30
return false unless akismet_enabled?
params = {
type: 'comment',
text: text,
created_at: DateTime.now,
author: owner.name,
author_email: owner.email,
2017-09-10 17:25:29 +05:30
referrer: options[:referrer]
2016-09-13 17:45:13 +05:30
}
begin
is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
is_spam || is_blatant
rescue => e
2019-09-30 21:07:59 +05:30
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check") # rubocop:disable Gitlab/RailsLogger
2016-09-13 17:45:13 +05:30
false
end
end
def submit_ham
2016-11-03 12:29:30 +05:30
submit(:ham)
end
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
def submit_spam
submit(:spam)
end
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
private
def akismet_client
2018-03-17 18:26:18 +05:30
@akismet_client ||= ::Akismet::Client.new(Gitlab::CurrentSettings.akismet_api_key,
2016-11-03 12:29:30 +05:30
Gitlab.config.gitlab.url)
2016-09-13 17:45:13 +05:30
end
2016-11-03 12:29:30 +05:30
def akismet_enabled?
2018-03-17 18:26:18 +05:30
Gitlab::CurrentSettings.akismet_enabled
2016-11-03 12:29:30 +05:30
end
def submit(type)
2016-09-13 17:45:13 +05:30
return false unless akismet_enabled?
params = {
type: 'comment',
text: text,
author: owner.name,
author_email: owner.email
}
begin
2018-03-17 18:26:18 +05:30
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params) # rubocop:disable GitlabSecurity/PublicSend
2016-09-13 17:45:13 +05:30
true
rescue => e
2019-09-30 21:07:59 +05:30
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!") # rubocop:disable Gitlab/RailsLogger
2016-09-13 17:45:13 +05:30
false
end
end
end