Class: Spam::SpamVerdictService

Inherits:
Object
  • Object
show all
Includes:
AkismetMethods, SpamConstants
Defined in:
app/services/spam/spam_verdict_service.rb

Constant Summary

Constants included from SpamConstants

Spam::SpamConstants::ALLOW, Spam::SpamConstants::BLOCK_USER, Spam::SpamConstants::CONDITIONAL_ALLOW, Spam::SpamConstants::DISALLOW, Spam::SpamConstants::NOOP, Spam::SpamConstants::OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM, Spam::SpamConstants::SUPPORTED_VERDICTS

Instance Method Summary collapse

Methods included from AkismetMethods

#akismet, #target_owner

Constructor Details

#initialize(user:, target:, options:, context: {}) ⇒ SpamVerdictService

Returns a new instance of SpamVerdictService.


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

def initialize(user:, target:, options:, context: {})
  @target = target
  @user = user
  @options = options
  @context = context
end

Instance Method Details

#executeObject


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

def execute
  spamcheck_result = nil
  spamcheck_attribs = {}
  spamcheck_error = false

  external_spam_check_round_trip_time = Benchmark.realtime do
    spamcheck_result, spamcheck_attribs, spamcheck_error = spamcheck_verdict
  end

  label = spamcheck_error ? 'ERROR' : spamcheck_result.to_s.upcase

  histogram.observe( { result: label }, external_spam_check_round_trip_time )

  # assign result to a var for logging it before reassigning to nil when monitorMode is true
  original_spamcheck_result = spamcheck_result

  spamcheck_result = nil if spamcheck_attribs&.fetch("monitorMode", "false") == "true"

  akismet_result = akismet_verdict

  # filter out anything we don't recognise, including nils.
  valid_results = [spamcheck_result, akismet_result].compact.select { |r| SUPPORTED_VERDICTS.key?(r) }

  # Treat nils - such as service unavailable - as ALLOW
  return ALLOW unless valid_results.any?

  # Favour the most restrictive result.
  verdict = valid_results.min_by { |v| SUPPORTED_VERDICTS[v][:priority] }

  # The target can override the verdict via the `allow_possible_spam` feature flag
  verdict = OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM if override_via_allow_possible_spam?(verdict: verdict)

  logger.info(class: self.class.name,
              akismet_verdict: akismet_verdict,
              spam_check_verdict: original_spamcheck_result,
              extra_attributes: spamcheck_attribs,
              spam_check_rtt: external_spam_check_round_trip_time.real,
              final_verdict: verdict,
              username: user.username,
              user_id: user.id,
              target_type: target.class.to_s,
              project_id: target.project_id
             )

  verdict
end