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.

137 lines
3.0 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. silence
  10. suspend
  11. ).freeze
  12. attr_accessor :target_account,
  13. :current_account,
  14. :type,
  15. :text,
  16. :report_id,
  17. :warning_preset_id
  18. attr_reader :warning, :send_email_notification
  19. def send_email_notification=(value)
  20. @send_email_notification = ActiveModel::Type::Boolean.new.cast(value)
  21. end
  22. def save!
  23. ApplicationRecord.transaction do
  24. process_action!
  25. process_warning!
  26. end
  27. queue_email!
  28. process_reports!
  29. end
  30. def report
  31. @report ||= Report.find(report_id) if report_id.present?
  32. end
  33. def with_report?
  34. !report.nil?
  35. end
  36. class << self
  37. def types_for_account(account)
  38. if account.local?
  39. TYPES
  40. else
  41. TYPES - %w(none disable)
  42. end
  43. end
  44. end
  45. private
  46. def process_action!
  47. case type
  48. when 'disable'
  49. handle_disable!
  50. when 'silence'
  51. handle_silence!
  52. when 'suspend'
  53. handle_suspend!
  54. end
  55. end
  56. def process_warning!
  57. return unless warnable?
  58. authorize(target_account, :warn?)
  59. @warning = AccountWarning.create!(target_account: target_account,
  60. account: current_account,
  61. action: type,
  62. text: text_for_warning)
  63. # A log entry is only interesting if the warning contains
  64. # custom text from someone. Otherwise it's just noise.
  65. log_action(:create, warning) if warning.text.present?
  66. end
  67. def process_reports!
  68. return if report_id.blank?
  69. authorize(report, :update?)
  70. if type == 'none'
  71. log_action(:resolve, report)
  72. report.resolve!(current_account)
  73. else
  74. Report.where(target_account: target_account).unresolved.update_all(action_taken: true, action_taken_by_account_id: current_account.id)
  75. end
  76. end
  77. def handle_disable!
  78. authorize(target_account.user, :disable?)
  79. log_action(:disable, target_account.user)
  80. target_account.user&.disable!
  81. end
  82. def handle_silence!
  83. authorize(target_account, :silence?)
  84. log_action(:silence, target_account)
  85. target_account.silence!
  86. end
  87. def handle_suspend!
  88. authorize(target_account, :suspend?)
  89. log_action(:suspend, target_account)
  90. target_account.suspend!
  91. queue_suspension_worker!
  92. end
  93. def text_for_warning
  94. [warning_preset&.text, text].compact.join("\n\n")
  95. end
  96. def queue_suspension_worker!
  97. Admin::SuspensionWorker.perform_async(target_account.id)
  98. end
  99. def queue_email!
  100. return unless warnable?
  101. UserMailer.warning(target_account.user, warning).deliver_later!
  102. end
  103. def warnable?
  104. send_email_notification && target_account.local?
  105. end
  106. def warning_preset
  107. @warning_preset ||= AccountWarningPreset.find(warning_preset_id) if warning_preset_id.present?
  108. end
  109. end