Module: Spammable

Extended by:
ActiveSupport::Concern
Included in:
Issue, Snippet
Defined in:
app/models/concerns/spammable.rb

Instance Method Summary collapse

Instance Method Details

#allow_possible_spam?Boolean

Override in Spammable if differs

Returns:

  • (Boolean)

125
126
127
# File 'app/models/concerns/spammable.rb', line 125

def allow_possible_spam?
  Feature.enabled?(:allow_possible_spam, project)
end

#check_for_spam?(user:) ⇒ Boolean

Override in Spammable if further checks are necessary

Returns:

  • (Boolean)

120
121
122
# File 'app/models/concerns/spammable.rb', line 120

def check_for_spam?(user:)
  true
end

#clear_spam_flags!Object


67
68
69
70
# File 'app/models/concerns/spammable.rb', line 67

def clear_spam_flags!
  self.spam = false
  self.needs_recaptcha = false
end

#invalidate_if_spamObject


72
73
74
75
76
77
78
# File 'app/models/concerns/spammable.rb', line 72

def invalidate_if_spam
  if needs_recaptcha? && Gitlab::Recaptcha.enabled?
    recaptcha_error!
  elsif needs_recaptcha? || spam?
    unrecoverable_spam_error!
  end
end

#needs_recaptcha!Object


48
49
50
# File 'app/models/concerns/spammable.rb', line 48

def needs_recaptcha!
  self.needs_recaptcha = true
end

#recaptcha_error!Object


80
81
82
83
84
# File 'app/models/concerns/spammable.rb', line 80

def recaptcha_error!
  self.errors.add(:base, _("Your %{spammable_entity_type} has been recognized as spam. "\
                  "Please, change the content or solve the reCAPTCHA to proceed.") \
                  % { spammable_entity_type: spammable_entity_type })
end

#render_recaptcha?Boolean

Indicates if a recaptcha should be rendered before allowing this model to be saved.

Returns:

  • (Boolean)

55
56
57
58
59
60
61
# File 'app/models/concerns/spammable.rb', line 55

def render_recaptcha?
  return false unless Gitlab::Recaptcha.enabled?

  return false if self.errors.count > 1 # captcha should not be rendered if are still other errors

  self.needs_recaptcha?
end

#spamObject


32
33
34
# File 'app/models/concerns/spammable.rb', line 32

def spam
  !!@spam # rubocop:disable Gitlab/ModuleWithInstanceVariables
end

#spam!Object


63
64
65
# File 'app/models/concerns/spammable.rb', line 63

def spam!
  self.spam = true
end

#spam_descriptionObject


103
104
105
106
107
108
109
# File 'app/models/concerns/spammable.rb', line 103

def spam_description
  attr = self.class.spammable_attrs.find do |_, options|
    options.fetch(:spam_description, false)
  end

  public_send(attr.first) if attr && respond_to?(attr.first.to_sym) # rubocop:disable GitlabSecurity/PublicSend
end

#spam_titleObject


95
96
97
98
99
100
101
# File 'app/models/concerns/spammable.rb', line 95

def spam_title
  attr = self.class.spammable_attrs.find do |_, options|
    options.fetch(:spam_title, false)
  end

  public_send(attr.first) if attr && respond_to?(attr.first.to_sym) # rubocop:disable GitlabSecurity/PublicSend
end

#spammable_entity_typeObject


91
92
93
# File 'app/models/concerns/spammable.rb', line 91

def spammable_entity_type
  self.class.name.underscore
end

#spammable_textObject


111
112
113
114
115
116
117
# File 'app/models/concerns/spammable.rb', line 111

def spammable_text
  result = self.class.spammable_attrs.map do |attr|
    public_send(attr.first) # rubocop:disable GitlabSecurity/PublicSend
  end

  result.reject(&:blank?).join("\n")
end

#submittable_as_spam?Boolean

Returns:

  • (Boolean)

40
41
42
43
44
45
46
# File 'app/models/concerns/spammable.rb', line 40

def submittable_as_spam?
  if user_agent_detail
    user_agent_detail.submittable? && Gitlab::CurrentSettings.current_application_settings.akismet_enabled
  else
    false
  end
end

#submittable_as_spam_by?(current_user) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
# File 'app/models/concerns/spammable.rb', line 36

def submittable_as_spam_by?(current_user)
  current_user && current_user.admin? && submittable_as_spam?
end

#unrecoverable_spam_error!Object


86
87
88
89
# File 'app/models/concerns/spammable.rb', line 86

def unrecoverable_spam_error!
  self.errors.add(:base, _("Your %{spammable_entity_type} has been recognized as spam and has been discarded.") \
                  % { spammable_entity_type: spammable_entity_type })
end