Module: Gitlab::AkismetHelper

Defined in:
lib/gitlab/akismet_helper.rb

Instance Method Summary collapse

Instance Method Details

#akismet_clientObject


7
8
9
10
# File 'lib/gitlab/akismet_helper.rb', line 7

def akismet_client
  @akismet_client ||= ::Akismet::Client.new(current_application_settings.akismet_api_key,
    Gitlab.config.gitlab.url)
end

#akismet_enabled?Boolean

Returns:

  • (Boolean)

3
4
5
# File 'lib/gitlab/akismet_helper.rb', line 3

def akismet_enabled?
  current_application_settings.akismet_enabled
end

#check_for_spam?(project, user) ⇒ Boolean

Returns:

  • (Boolean)

20
21
22
# File 'lib/gitlab/akismet_helper.rb', line 20

def check_for_spam?(project, user)
  akismet_enabled? && !project.team.member?(user)
end

#client_ip(env) ⇒ Object


12
13
14
# File 'lib/gitlab/akismet_helper.rb', line 12

def client_ip(env)
  env['action_dispatch.remote_ip'].to_s
end

#is_spam?(environment, user, text) ⇒ Boolean

Returns:

  • (Boolean)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gitlab/akismet_helper.rb', line 24

def is_spam?(environment, user, text)
  client = akismet_client
  ip_address = client_ip(environment)
  user_agent = user_agent(environment)

  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

#user_agent(env) ⇒ Object


16
17
18
# File 'lib/gitlab/akismet_helper.rb', line 16

def user_agent(env)
  env['HTTP_USER_AGENT']
end