2016-01-09 14:30:34 -05:00
|
|
|
module Gitlab
|
|
|
|
module AkismetHelper
|
|
|
|
def akismet_enabled?
|
|
|
|
current_application_settings.akismet_enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def akismet_client
|
2016-02-01 19:05:56 -05:00
|
|
|
@akismet_client ||= ::Akismet::Client.new(current_application_settings.akismet_api_key,
|
|
|
|
Gitlab.config.gitlab.url)
|
2016-01-09 14:30:34 -05:00
|
|
|
end
|
|
|
|
|
2016-04-28 01:04:40 -04:00
|
|
|
def client_ip(env)
|
|
|
|
env['action_dispatch.remote_ip'].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_agent(env)
|
|
|
|
env['HTTP_USER_AGENT']
|
|
|
|
end
|
|
|
|
|
2016-07-18 19:17:43 -04:00
|
|
|
def check_for_spam?(project)
|
|
|
|
akismet_enabled? && project.public?
|
2016-01-09 14:30:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_spam?(environment, user, text)
|
|
|
|
client = akismet_client
|
2016-04-28 01:04:40 -04:00
|
|
|
ip_address = client_ip(environment)
|
|
|
|
user_agent = user_agent(environment)
|
2016-01-09 14:30:34 -05:00
|
|
|
|
|
|
|
params = {
|
|
|
|
type: 'comment',
|
|
|
|
text: text,
|
|
|
|
created_at: DateTime.now,
|
|
|
|
author: user.name,
|
|
|
|
author_email: user.email,
|
|
|
|
referrer: environment['HTTP_REFERER'],
|
|
|
|
}
|
|
|
|
|
|
|
|
begin
|
|
|
|
is_spam, is_blatant = client.check(ip_address, user_agent, params)
|
|
|
|
is_spam || is_blatant
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check")
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|