Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Admin::AccountAction
|
|
|
|
include ActiveModel::Model
|
|
|
|
include AccountableConcern
|
|
|
|
include Authorization
|
|
|
|
|
|
|
|
TYPES = %w(
|
|
|
|
none
|
|
|
|
disable
|
2020-11-04 20:45:01 +01:00
|
|
|
sensitive
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
silence
|
|
|
|
suspend
|
|
|
|
).freeze
|
|
|
|
|
|
|
|
attr_accessor :target_account,
|
|
|
|
:current_account,
|
|
|
|
:type,
|
|
|
|
:text,
|
|
|
|
:report_id,
|
2019-07-06 13:54:32 +02:00
|
|
|
:warning_preset_id
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
attr_reader :warning, :send_email_notification, :include_statuses
|
2019-07-06 13:54:32 +02:00
|
|
|
|
2022-03-15 04:11:13 +01:00
|
|
|
alias send_email_notification? send_email_notification
|
|
|
|
alias include_statuses? include_statuses
|
|
|
|
|
2022-10-30 02:44:32 +02:00
|
|
|
validates :type, :target_account, :current_account, presence: true
|
|
|
|
|
2022-03-15 04:11:13 +01:00
|
|
|
def initialize(attributes = {})
|
|
|
|
@send_email_notification = true
|
|
|
|
@include_statuses = true
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2019-07-06 13:54:32 +02:00
|
|
|
def send_email_notification=(value)
|
|
|
|
@send_email_notification = ActiveModel::Type::Boolean.new.cast(value)
|
|
|
|
end
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
def include_statuses=(value)
|
|
|
|
@include_statuses = ActiveModel::Type::Boolean.new.cast(value)
|
|
|
|
end
|
|
|
|
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
def save!
|
2022-10-30 02:44:32 +02:00
|
|
|
raise ActiveRecord::RecordInvalid, self unless valid?
|
|
|
|
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
ApplicationRecord.transaction do
|
|
|
|
process_action!
|
2022-01-17 09:41:33 +01:00
|
|
|
process_strike!
|
2022-10-30 02:44:32 +02:00
|
|
|
process_reports!
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
end
|
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
process_email!
|
|
|
|
process_queue!
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def report
|
|
|
|
@report ||= Report.find(report_id) if report_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_report?
|
|
|
|
!report.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def types_for_account(account)
|
|
|
|
if account.local?
|
|
|
|
TYPES
|
|
|
|
else
|
|
|
|
TYPES - %w(none disable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_action!
|
|
|
|
case type
|
|
|
|
when 'disable'
|
|
|
|
handle_disable!
|
2020-11-04 20:45:01 +01:00
|
|
|
when 'sensitive'
|
|
|
|
handle_sensitive!
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
when 'silence'
|
|
|
|
handle_silence!
|
|
|
|
when 'suspend'
|
|
|
|
handle_suspend!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-17 09:41:33 +01:00
|
|
|
def process_strike!
|
|
|
|
@warning = target_account.strikes.create!(
|
|
|
|
account: current_account,
|
|
|
|
report: report,
|
|
|
|
action: type,
|
|
|
|
text: text_for_warning,
|
|
|
|
status_ids: status_ids
|
|
|
|
)
|
2022-05-06 21:40:20 +02:00
|
|
|
|
|
|
|
# A log entry is only interesting if the warning contains
|
|
|
|
# custom text from someone. Otherwise it's just noise.
|
|
|
|
log_action(:create, @warning) if @warning.text.present? && type == 'none'
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_reports!
|
2019-09-11 16:32:44 +02:00
|
|
|
# If we're doing "mark as resolved" on a single report,
|
|
|
|
# then we want to keep other reports open in case they
|
|
|
|
# contain new actionable information.
|
|
|
|
#
|
|
|
|
# Otherwise, we will mark all unresolved reports about
|
|
|
|
# the account as resolved.
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
reports.each do |report|
|
2022-10-30 02:44:32 +02:00
|
|
|
authorize(report, :update?)
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
log_action(:resolve, report)
|
|
|
|
report.resolve!(current_account)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_disable!
|
|
|
|
authorize(target_account.user, :disable?)
|
|
|
|
log_action(:disable, target_account.user)
|
|
|
|
target_account.user&.disable!
|
|
|
|
end
|
|
|
|
|
2020-11-04 20:45:01 +01:00
|
|
|
def handle_sensitive!
|
|
|
|
authorize(target_account, :sensitive?)
|
|
|
|
log_action(:sensitive, target_account)
|
|
|
|
target_account.sensitize!
|
|
|
|
end
|
|
|
|
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
def handle_silence!
|
|
|
|
authorize(target_account, :silence?)
|
|
|
|
log_action(:silence, target_account)
|
|
|
|
target_account.silence!
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_suspend!
|
|
|
|
authorize(target_account, :suspend?)
|
|
|
|
log_action(:suspend, target_account)
|
2020-11-08 00:28:39 +01:00
|
|
|
target_account.suspend!(origin: :local)
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def text_for_warning
|
|
|
|
[warning_preset&.text, text].compact.join("\n\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_suspension_worker!
|
|
|
|
Admin::SuspensionWorker.perform_async(target_account.id)
|
|
|
|
end
|
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
def process_queue!
|
|
|
|
queue_suspension_worker! if type == 'suspend'
|
|
|
|
end
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
def process_email!
|
2022-01-17 09:41:33 +01:00
|
|
|
UserMailer.warning(target_account.user, warning).deliver_later! if warnable?
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def warnable?
|
2022-03-15 04:11:13 +01:00
|
|
|
send_email_notification? && target_account.local?
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
end
|
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
def status_ids
|
2022-03-15 04:11:13 +01:00
|
|
|
report.status_ids if with_report? && include_statuses?
|
2019-08-23 22:37:23 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
def reports
|
2023-02-18 23:09:40 +01:00
|
|
|
@reports ||= if type == 'none'
|
|
|
|
with_report? ? [report] : []
|
|
|
|
else
|
|
|
|
Report.where(target_account: target_account).unresolved
|
|
|
|
end
|
2019-09-11 16:32:44 +02:00
|
|
|
end
|
|
|
|
|
Add moderation warnings (#9519)
* Add moderation warnings
Replace individual routes for disabling, silencing, and suspending
a user, as well as the report update route, with a unified account
action controller that allows you to select an action (none,
disable, silence, suspend) as well as whether it should generate an
e-mail notification with optional custom text. That notification,
with the optional custom text, is saved as a warning.
Additionally, there are warning presets you can configure to save
time when performing the above.
* Use Account#local_username_and_domain
2018-12-22 20:02:09 +01:00
|
|
|
def warning_preset
|
|
|
|
@warning_preset ||= AccountWarningPreset.find(warning_preset_id) if warning_preset_id.present?
|
|
|
|
end
|
|
|
|
end
|