Welcome to mirror list, hosted at ThFree Co, Russian Federation.

akismet_helper.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19e738203219832921bee9b84b0494b4d26f3515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module Gitlab
  module AkismetHelper
    def akismet_enabled?
      current_application_settings.akismet_enabled
    end

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

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

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

    def check_for_spam?(project)
      akismet_enabled? && project.public?
    end

    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

    def ham!(details, text, user)
      client = akismet_client

      params = {
        type: 'comment',
        text: text,
        author: user.name,
        author_email: user.email
      }

      begin
        client.submit_ham(details.ip_address, details.user_agent, params)
      rescue => e
        Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
      end
    end

    def spam!(details, text, user)
      client = akismet_client

      params = {
        type: 'comment',
        text: text,
        author: user.name,
        author_email: user.email
      }

      begin
        client.submit_spam(details.ip_address, details.user_agent, params)
      rescue => e
        Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
      end
    end
  end
end