Class: Decidim::SpamSignal::Cops::CopHandler

Inherits:
ApplicationHandler show all
Defined in:
app/commands/decidim/spam_signal/cops/cop_handler.rb

Direct Known Subclasses

LockCopCommand, SinalizeCopCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationHandler

form, handler_name, #handler_name

Constructor Details

#initialize(errors:, error_key: :base, suspicious_user:, config:, reportable: nil, justification: nil, admin_reporter: nil) ⇒ CopHandler

Returns a new instance of CopHandler.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 9

def initialize(
  errors:,
  error_key: :base,
  suspicious_user:,
  config:,
  reportable: nil,
  justification: nil,
  admin_reporter: nil
)
  @errors = errors
  @error_key = error_key
  @reportable = reportable || suspicious_user
  @suspicious_user = suspicious_user
  @current_organization = suspicious_user.organization

  @config = config
  @justification = justification
  @admin_reporter = admin_reporter || CopBot.get(suspicious_user.organization)
end

Instance Attribute Details

#admin_reporterObject (readonly)

Returns the value of attribute admin_reporter.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def admin_reporter
  @admin_reporter
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def config
  @config
end

#current_organizationObject (readonly)

Returns the value of attribute current_organization.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def current_organization
  @current_organization
end

#error_keyObject (readonly)

Returns the value of attribute error_key.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def error_key
  @error_key
end

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def errors
  @errors
end

#justificationObject (readonly)

Returns the value of attribute justification.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def justification
  @justification
end

#reportableObject (readonly)

Returns the value of attribute reportable.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def reportable
  @reportable
end

#suspicious_userObject (readonly)

Returns the value of attribute suspicious_user.



7
8
9
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 7

def suspicious_user
  @suspicious_user
end

Class Method Details

.i18n_keyObject



29
30
31
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 29

def self.i18n_key
  "decidim.spam_signal.cops.#{handler_name}"
end

Instance Method Details

#now_tagObject



32
33
34
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 32

def now_tag
  "\n[#{DateTime.now.strftime("%d/%m/%Y %H:%M")}]"
end

#sinalize!(send_emails = true) ⇒ Object



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
61
62
63
64
65
66
67
# File 'app/commands/decidim/spam_signal/cops/cop_handler.rb', line 35

def sinalize!(send_emails=true)
  is_user_reported = reportable == suspicious_user
  if is_user_reported
    moderation = Decidim::UserModeration.find_or_create_by!(user: suspicious_user) do |moderation|
      moderation.report_count = 0
    end
  else
    moderation = Decidim::Moderation.find_or_create_by!(reportable: reportable, participatory_space: reportable.participatory_space) do |moderation|
      moderation.report_count = 0
    end
  end
  is_new = moderation.report_count == 0
  moderation.update(report_count: moderation.report_count + 1)
  if is_user_reported
    user_report = Decidim::UserReport.find_or_create_by!(moderation: moderation)  do |report|
      report.moderation = moderation
      report.user = admin_reporter
      report.reason = "spam"
      report.details = "#{now_tag}#{justification}"
    end
  else
    user_report = Decidim::Report.find_or_create_by!(moderation: moderation)  do |report|
      report.moderation = moderation
      report.user = admin_reporter
      report.reason = "spam"
      report.details = "#{now_tag}#{justification}"
    end
  end
  # append the new bad things (to have a log).
  user_report.update(details: "#{user_report.details}#{now_tag} #{justification}") unless is_new
  admin_accountable = Decidim::User.where(admin: true, email: ENV.fetch("ANTISPAM_ADMIN", "[email protected]")).first
  Decidim::UserReportJob.perform_later(admin, admin_reporter, "spam", reportable)
end