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
|
|
|
|
|
2017-08-24 13:05:02 -04:00
|
|
|
def spam?
|
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,
|
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
|
2018-02-02 13:39:55 -05:00
|
|
|
@akismet_client ||= ::Akismet::Client.new(Gitlab::CurrentSettings.akismet_api_key,
|
2016-09-14 20:59:56 -04:00
|
|
|
Gitlab.config.gitlab.url)
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
|
|
|
|
2016-09-14 20:59:56 -04:00
|
|
|
def akismet_enabled?
|
2018-02-02 13:39:55 -05:00
|
|
|
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?
|
|
|
|
|
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
|
2017-08-10 12:39:26 -04:00
|
|
|
akismet_client.public_send(type, options[:ip_address], options[:user_agent], params) # rubocop:disable GitlabSecurity/PublicSend
|
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
|