The code powering m.abunchtell.com https://m.abunchtell.com
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

148 lignes
3.2 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. return if report_id.blank?
  73. authorize(report, :update?)
  74. if type == 'none'
  75. log_action(:resolve, report)
  76. report.resolve!(current_account)
  77. else
  78. Report.where(target_account: target_account).unresolved.update_all(action_taken: true, action_taken_by_account_id: current_account.id)
  79. end
  80. end
  81. def handle_disable!
  82. authorize(target_account.user, :disable?)
  83. log_action(:disable, target_account.user)
  84. target_account.user&.disable!
  85. end
  86. def handle_silence!
  87. authorize(target_account, :silence?)
  88. log_action(:silence, target_account)
  89. target_account.silence!
  90. end
  91. def handle_suspend!
  92. authorize(target_account, :suspend?)
  93. log_action(:suspend, target_account)
  94. target_account.suspend!
  95. end
  96. def text_for_warning
  97. [warning_preset&.text, text].compact.join("\n\n")
  98. end
  99. def queue_suspension_worker!
  100. Admin::SuspensionWorker.perform_async(target_account.id)
  101. end
  102. def process_queue!
  103. queue_suspension_worker! if type == 'suspend'
  104. end
  105. def process_email!
  106. UserMailer.warning(target_account.user, warning, status_ids).deliver_now! if warnable?
  107. end
  108. def warnable?
  109. send_email_notification && target_account.local?
  110. end
  111. def status_ids
  112. @report.status_ids if @report && include_statuses
  113. end
  114. def warning_preset
  115. @warning_preset ||= AccountWarningPreset.find(warning_preset_id) if warning_preset_id.present?
  116. end
  117. end