Class: Spam::AkismetService

Inherits:
Object
  • Object
show all
Defined in:
app/services/spam/akismet_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_name, owner_email, text, options = {}) ⇒ AkismetService

Returns a new instance of AkismetService.


7
8
9
10
11
12
# File 'app/services/spam/akismet_service.rb', line 7

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.


5
6
7
# File 'app/services/spam/akismet_service.rb', line 5

def options
  @options
end

#textObject

Returns the value of attribute text.


5
6
7
# File 'app/services/spam/akismet_service.rb', line 5

def text
  @text
end

Instance Method Details

#spam?Boolean

Returns:

  • (Boolean)

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
# File 'app/services/spam/akismet_service.rb', line 14

def spam?
  return false unless akismet_enabled?

  params = {
    type: 'comment',
    text: text,
    created_at: DateTime.current,
    author: owner_name,
    author_email: owner_email,
    # NOTE: The akismet_client needs the option to be named `:referrer`, not `:referer`
    referrer: options[:referer]
  }

  begin
    is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
    is_spam || is_blatant
  rescue ArgumentError => e
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
    false
  rescue StandardError => e
    Gitlab::ErrorTracking.track_exception(e)
    Gitlab::AppLogger.error("Error during Akismet spam check, flagging as not spam: #{e}")
    false
  end
end

#submit_hamObject


40
41
42
# File 'app/services/spam/akismet_service.rb', line 40

def submit_ham
  submit(:ham)
end

#submit_spamObject


44
45
46
# File 'app/services/spam/akismet_service.rb', line 44

def submit_spam
  submit(:spam)
end