Module: Spammable

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::Utils::StrongMemoize
Included in:
Issue, MergeRequest, Milestone, Note, Snippet
Defined in:
app/models/concerns/spammable.rb

Instance Method Summary collapse

Instance Method Details

#allow_possible_spam?Boolean

Override in included class if you want to allow possible spam under specific circumstances

Returns:

  • (Boolean)


157
158
159
# File 'app/models/concerns/spammable.rb', line 157

def allow_possible_spam?(*)
  Gitlab::CurrentSettings.allow_possible_spam
end

#check_for_spam(user:, action:, extra_features: {}) ⇒ Object



150
151
152
153
154
# File 'app/models/concerns/spammable.rb', line 150

def check_for_spam(user:, action:, extra_features: {})
  strong_memoize_with(:check_for_spam, user, action, extra_features) do
    Spam::SpamActionService.new(spammable: self, user: user, action: action, extra_features: extra_features).execute
  end
end

#check_for_spam?Boolean

Override in included class if further checks are necessary

Returns:

  • (Boolean)


142
143
144
# File 'app/models/concerns/spammable.rb', line 142

def check_for_spam?(*)
  spammable_attribute_changed?
end

#clear_spam_flags!Object



77
78
79
80
# File 'app/models/concerns/spammable.rb', line 77

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

#invalidate_if_spamObject



82
83
84
85
86
87
88
# File 'app/models/concerns/spammable.rb', line 82

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

#needs_recaptcha!Object



49
50
51
52
53
54
55
# File 'app/models/concerns/spammable.rb', line 49

def needs_recaptcha!
  if self.supports_recaptcha?
    self.needs_recaptcha = true
  else
    self.spam!
  end
end

#recaptcha_error!Object



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

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)


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

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

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

  self.needs_recaptcha?
end

#spamObject



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

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

#spam!Object



73
74
75
# File 'app/models/concerns/spammable.rb', line 73

def spam!
  self.spam = true
end

#spam_descriptionObject



125
126
127
128
129
130
131
# File 'app/models/concerns/spammable.rb', line 125

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



117
118
119
120
121
122
123
# File 'app/models/concerns/spammable.rb', line 117

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_attribute_changed?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/models/concerns/spammable.rb', line 146

def spammable_attribute_changed?
  (changed & self.class.spammable_attrs.to_h.keys).any?
end

#spammable_entity_typeObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/concerns/spammable.rb', line 102

def spammable_entity_type
  case self
  when Issue
    _('issue')
  when MergeRequest
    _('merge request')
  when Note
    _('comment')
  when Snippet
    _('snippet')
  else
    self.class.model_name.human.downcase
  end
end

#spammable_textObject



133
134
135
136
137
138
139
# File 'app/models/concerns/spammable.rb', line 133

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)


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

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)


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

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

#supports_recaptcha?Boolean

Override in Spammable if recaptcha is supported

Returns:

  • (Boolean)


58
59
60
# File 'app/models/concerns/spammable.rb', line 58

def supports_recaptcha?
  false
end

#unrecoverable_spam_error!Object



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

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