You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.4 KiB

  1. # frozen_string_literal: true
  2. class Admin::AccountAction
  3. include ActiveModel::Model
  4. include AccountableConcern
  5. include Authorization
  6. TYPES = %w(
  7. none
  8. disable
  9. sensitive
  10. silence
  11. suspend
  12. ).freeze
  13. attr_accessor :target_account,
  14. :current_account,
  15. :type,
  16. :text,
  17. :report_id,
  18. :warning_preset_id
  19. attr_reader :warning, :send_email_notification, :include_statuses
  20. def send_email_notification=(value)
  21. @send_email_notification = ActiveModel::Type::Boolean.new.cast(value)
  22. end
  23. def include_statuses=(value)
  24. @include_statuses = ActiveModel::Type::Boolean.new.cast(value)
  25. end
  26. def save!
  27. ApplicationRecord.transaction do
  28. process_action!
  29. process_strike!
  30. end
  31. process_email!
  32. process_reports!
  33. process_queue!
  34. end
  35. def report
  36. @report ||= Report.find(report_id) if report_id.present?
  37. end
  38. def with_report?
  39. !report.nil?
  40. end
  41. class << self
  42. def types_for_account(account)
  43. if account.local?
  44. TYPES
  45. else
  46. TYPES - %w(none disable)
  47. end
  48. end
  49. end
  50. private
  51. def process_action!
  52. case type
  53. when 'disable'
  54. handle_disable!
  55. when 'sensitive'
  56. handle_sensitive!
  57. when 'silence'
  58. handle_silence!
  59. when 'suspend'
  60. handle_suspend!
  61. end
  62. end
  63. def process_strike!
  64. @warning = target_account.strikes.create!(
  65. account: current_account,
  66. report: report,
  67. action: type,
  68. text: text_for_warning,
  69. status_ids: status_ids
  70. )
  71. end
  72. def process_reports!
  73. # If we're doing "mark as resolved" on a single report,
  74. # then we want to keep other reports open in case they
  75. # contain new actionable information.
  76. #
  77. # Otherwise, we will mark all unresolved reports about
  78. # the account as resolved.
  79. reports.each { |report| authorize(report, :update?) }
  80. reports.each do |report|
  81. log_action(:resolve, report)
  82. report.resolve!(current_account)
  83. end
  84. end
  85. def handle_disable!
  86. authorize(target_account.user, :disable?)
  87. log_action(:disable, target_account.user)
  88. target_account.user&.disable!
  89. end
  90. def handle_sensitive!
  91. authorize(target_account, :sensitive?)
  92. log_action(:sensitive, target_account)
  93. target_account.sensitize!
  94. end
  95. def handle_silence!
  96. authorize(target_account, :silence?)
  97. log_action(:silence, target_account)
  98. target_account.silence!
  99. end
  100. def handle_suspend!
  101. authorize(target_account, :suspend?)
  102. log_action(:suspend, target_account)
  103. target_account.suspend!(origin: :local)
  104. end
  105. def text_for_warning
  106. [warning_preset&.text, text].compact.join("\n\n")
  107. end
  108. def queue_suspension_worker!
  109. Admin::SuspensionWorker.perform_async(target_account.id)
  110. end
  111. def process_queue!
  112. queue_suspension_worker! if type == 'suspend'
  113. end
  114. def process_email!
  115. UserMailer.warning(target_account.user, warning).deliver_later! if warnable?
  116. end
  117. def warnable?
  118. send_email_notification && target_account.local?
  119. end
  120. def status_ids
  121. report.status_ids if with_report? && include_statuses
  122. end
  123. def reports
  124. @reports ||= begin
  125. if type == 'none' && with_report?
  126. [report]
  127. else
  128. Report.where(target_account: target_account).unresolved
  129. end
  130. end
  131. end
  132. def warning_preset
  133. @warning_preset ||= AccountWarningPreset.find(warning_preset_id) if warning_preset_id.present?
  134. end
  135. end