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.

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