Class: Spam::SpamActionService

Inherits:
Object
  • Object
show all
Includes:
SpamConstants
Defined in:
app/services/spam/spam_action_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

Constructor Details

#initialize(spammable:, spam_params:, user:, action:) ⇒ SpamActionService

Returns a new instance of SpamActionService.


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

def initialize(spammable:, spam_params:, user:, action:)
  @target = spammable
  @spam_params = spam_params
  @user = user
  @action = action
end

Instance Method Details

#executeObject

rubocop:disable Metrics/AbcSize


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/spam/spam_action_service.rb', line 15

def execute
  # If spam_params is passed as `nil`, no check will be performed. This is the easiest way to allow
  # composed services which may not need to do spam checking to "opt out".  For example, when
  # MoveService is calling CreateService, spam checking is not necessary, as no new content is
  # being created.
  return ServiceResponse.success(message: 'Skipped spam check because spam_params was not present') unless spam_params

  recaptcha_verified = Captcha::CaptchaVerificationService.new(spam_params: spam_params).execute

  if recaptcha_verified
    # If it's a request which is already verified through CAPTCHA,
    # update the spam log accordingly.
    SpamLog.verify_recaptcha!(user_id: user.id, id: spam_params.spam_log_id)
    ServiceResponse.success(message: "CAPTCHA successfully verified")
  else
    return ServiceResponse.success(message: 'Skipped spam check because user was allowlisted') if allowlisted?(user)
    return ServiceResponse.success(message: 'Skipped spam check because it was not required') unless check_for_spam?(user: user)

    perform_spam_service_check
    ServiceResponse.success(message: "Spam check performed. Check #{target.class.name} spammable model for any errors or CAPTCHA requirement")
  end
end