summaryrefslogtreecommitdiff
path: root/app/services/akismet_service.rb
blob: 5c60addbe7c4d120e15c62d8f33654d38ec38081 (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
class AkismetService
  attr_accessor :owner, :text, :options

  def initialize(owner, text, options = {})
    @owner = owner
    @text = text
    @options = options
  end

  def is_spam?
    return false unless akismet_enabled?

    params = {
      type: 'comment',
      text: text,
      created_at: DateTime.now,
      author: owner.name,
      author_email: owner.email,
      referrer: options[:referrer],
    }

    begin
      is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
      is_spam || is_blatant
    rescue => e
      Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check")
      false
    end
  end

  def submit_ham
    return false unless akismet_enabled?

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

    begin
      akismet_client.submit_ham(options[:ip_address], options[:user_agent], params)
      true
    rescue => e
      Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
      false
    end
  end

  def submit_spam
    return false unless akismet_enabled?

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

    begin
      akismet_client.submit_spam(options[:ip_address], options[:user_agent], params)
      true
    rescue => e
      Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
      false
    end
  end

  private

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

  def akismet_enabled?
    current_application_settings.akismet_enabled
  end
end