2016-07-21 19:11:53 -04:00
|
|
|
module Spammable
|
|
|
|
extend ActiveSupport::Concern
|
2016-07-28 20:02:56 -04:00
|
|
|
|
|
|
|
module ClassMethods
|
2016-08-05 18:10:08 -04:00
|
|
|
def attr_spammable(attr, options = {})
|
|
|
|
spammable_attrs << [attr.to_s, options]
|
2016-07-28 20:02:56 -04:00
|
|
|
end
|
|
|
|
end
|
2016-07-21 19:11:53 -04:00
|
|
|
|
|
|
|
included do
|
2016-07-28 20:02:56 -04:00
|
|
|
has_one :user_agent_detail, as: :subject, dependent: :destroy
|
2016-08-09 13:43:47 -04:00
|
|
|
|
2016-07-21 19:11:53 -04:00
|
|
|
attr_accessor :spam
|
2017-01-27 11:25:39 -05:00
|
|
|
attr_accessor :spam_log
|
2016-08-09 13:43:47 -04:00
|
|
|
|
2017-02-14 14:07:11 -05:00
|
|
|
after_validation :check_for_spam, on: [:create, :update]
|
2016-07-28 20:02:56 -04:00
|
|
|
|
|
|
|
cattr_accessor :spammable_attrs, instance_accessor: false do
|
|
|
|
[]
|
|
|
|
end
|
2016-08-09 13:43:47 -04:00
|
|
|
|
|
|
|
delegate :ip_address, :user_agent, to: :user_agent_detail, allow_nil: true
|
2016-07-28 20:02:56 -04:00
|
|
|
end
|
|
|
|
|
2017-02-10 05:41:22 -05:00
|
|
|
def submittable_as_spam_by?(current_user)
|
|
|
|
current_user && current_user.admin? && submittable_as_spam?
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def submittable_as_spam?
|
2016-07-28 20:02:56 -04:00
|
|
|
if user_agent_detail
|
2016-08-22 22:36:04 -04:00
|
|
|
user_agent_detail.submittable? && current_application_settings.akismet_enabled
|
2016-07-28 20:02:56 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-30 00:18:32 -04:00
|
|
|
def spam?
|
2016-07-21 19:11:53 -04:00
|
|
|
@spam
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
def check_for_spam
|
2017-01-27 11:25:39 -05:00
|
|
|
error_msg = if Gitlab::Recaptcha.enabled?
|
|
|
|
"Your #{spammable_entity_type} has been recognized as spam. "\
|
2017-03-20 22:36:53 -04:00
|
|
|
"Please, change the content or solve the reCAPTCHA to proceed."
|
2017-01-27 11:25:39 -05:00
|
|
|
else
|
|
|
|
"Your #{spammable_entity_type} has been recognized as spam and has been discarded."
|
|
|
|
end
|
|
|
|
|
|
|
|
self.errors.add(:base, error_msg) if spam?
|
2017-02-01 13:15:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def spammable_entity_type
|
|
|
|
self.class.name.underscore
|
2016-07-30 00:18:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def spam_title
|
2016-08-09 13:43:47 -04:00
|
|
|
attr = self.class.spammable_attrs.find do |_, options|
|
2016-08-05 18:10:08 -04:00
|
|
|
options.fetch(:spam_title, false)
|
|
|
|
end
|
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
public_send(attr.first) if attr && respond_to?(attr.first.to_sym)
|
2016-07-30 00:18:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def spam_description
|
2016-08-09 13:43:47 -04:00
|
|
|
attr = self.class.spammable_attrs.find do |_, options|
|
2016-08-05 18:10:08 -04:00
|
|
|
options.fetch(:spam_description, false)
|
|
|
|
end
|
2016-07-28 20:02:56 -04:00
|
|
|
|
2016-08-09 13:43:47 -04:00
|
|
|
public_send(attr.first) if attr && respond_to?(attr.first.to_sym)
|
2016-08-05 18:10:08 -04:00
|
|
|
end
|
2016-07-28 20:02:56 -04:00
|
|
|
|
|
|
|
def spammable_text
|
2016-08-09 13:43:47 -04:00
|
|
|
result = self.class.spammable_attrs.map do |attr|
|
|
|
|
public_send(attr.first)
|
2016-07-28 20:02:56 -04:00
|
|
|
end
|
2016-08-05 18:10:08 -04:00
|
|
|
|
2016-07-28 20:02:56 -04:00
|
|
|
result.reject(&:blank?).join("\n")
|
|
|
|
end
|
|
|
|
|
2016-08-05 18:10:08 -04:00
|
|
|
# Override in Spammable if further checks are necessary
|
|
|
|
def check_for_spam?
|
2016-08-09 13:43:47 -04:00
|
|
|
true
|
2016-07-21 19:11:53 -04:00
|
|
|
end
|
|
|
|
end
|