account_action.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. alias send_email_notification? send_email_notification
  21. alias include_statuses? include_statuses
  22. validates :type, :target_account, :current_account, presence: true
  23. validates :type, inclusion: { in: TYPES }
  24. def initialize(attributes = {})
  25. @send_email_notification = true
  26. @include_statuses = true
  27. super
  28. end
  29. def send_email_notification=(value)
  30. @send_email_notification = ActiveModel::Type::Boolean.new.cast(value)
  31. end
  32. def include_statuses=(value)
  33. @include_statuses = ActiveModel::Type::Boolean.new.cast(value)
  34. end
  35. def save!
  36. raise ActiveRecord::RecordInvalid, self unless valid?
  37. ApplicationRecord.transaction do
  38. process_action!
  39. process_strike!
  40. process_reports!
  41. end
  42. process_email!
  43. process_queue!
  44. end
  45. def report
  46. @report ||= Report.find(report_id) if report_id.present?
  47. end
  48. def with_report?
  49. !report.nil?
  50. end
  51. class << self
  52. def types_for_account(account)
  53. if account.local?
  54. TYPES
  55. else
  56. TYPES - %w(none disable)
  57. end
  58. end
  59. def i18n_scope
  60. :activerecord
  61. end
  62. end
  63. private
  64. def process_action!
  65. case type
  66. when 'disable'
  67. handle_disable!
  68. when 'sensitive'
  69. handle_sensitive!
  70. when 'silence'
  71. handle_silence!
  72. when 'suspend'
  73. handle_suspend!
  74. end
  75. end
  76. def process_strike!
  77. @warning = target_account.strikes.create!(
  78. account: current_account,
  79. report: report,
  80. action: type,
  81. text: text_for_warning,
  82. status_ids: status_ids
  83. )
  84. # A log entry is only interesting if the warning contains
  85. # custom text from someone. Otherwise it's just noise.
  86. log_action(:create, @warning) if @warning.text.present? && type == 'none'
  87. end
  88. def process_reports!
  89. # If we're doing "mark as resolved" on a single report,
  90. # then we want to keep other reports open in case they
  91. # contain new actionable information.
  92. #
  93. # Otherwise, we will mark all unresolved reports about
  94. # the account as resolved.
  95. reports.each do |report|
  96. authorize(report, :update?)
  97. log_action(:resolve, report)
  98. report.resolve!(current_account)
  99. end
  100. end
  101. def handle_disable!
  102. authorize(target_account.user, :disable?)
  103. log_action(:disable, target_account.user)
  104. target_account.user&.disable!
  105. end
  106. def handle_sensitive!
  107. authorize(target_account, :sensitive?)
  108. log_action(:sensitive, target_account)
  109. target_account.sensitize!
  110. end
  111. def handle_silence!
  112. authorize(target_account, :silence?)
  113. log_action(:silence, target_account)
  114. target_account.silence!
  115. end
  116. def handle_suspend!
  117. authorize(target_account, :suspend?)
  118. log_action(:suspend, target_account)
  119. target_account.suspend!(origin: :local)
  120. end
  121. def text_for_warning
  122. [warning_preset&.text, text].compact.join("\n\n")
  123. end
  124. def queue_suspension_worker!
  125. Admin::SuspensionWorker.perform_async(target_account.id)
  126. end
  127. def process_queue!
  128. queue_suspension_worker! if type == 'suspend'
  129. end
  130. def process_email!
  131. UserMailer.warning(target_account.user, warning).deliver_later! if warnable?
  132. end
  133. def warnable?
  134. send_email_notification? && target_account.local?
  135. end
  136. def status_ids
  137. report.status_ids if with_report? && include_statuses?
  138. end
  139. def reports
  140. @reports ||= if type == 'none'
  141. with_report? ? [report] : []
  142. else
  143. Report.where(target_account: target_account).unresolved
  144. end
  145. end
  146. def warning_preset
  147. @warning_preset ||= AccountWarningPreset.find(warning_preset_id) if warning_preset_id.present?
  148. end
  149. end