闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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