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