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
|
|
|
|
|
|
|
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!
|
|
|
|
ApplicationRecord.transaction do
|
|
|
|
process_action!
|
|
|
|
process_warning!
|
|
|
|
end
|
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
process_email!
|
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
|
|
|
process_reports!
|
2019-08-23 22:37:23 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
def process_warning!
|
|
|
|
return unless warnable?
|
|
|
|
|
|
|
|
authorize(target_account, :warn?)
|
|
|
|
|
|
|
|
@warning = AccountWarning.create!(target_account: target_account,
|
|
|
|
account: current_account,
|
|
|
|
action: type,
|
|
|
|
text: text_for_warning)
|
|
|
|
|
|
|
|
# A log entry is only interesting if the warning contains
|
|
|
|
# custom text from someone. Otherwise it's just noise.
|
2019-09-11 16:32:44 +02:00
|
|
|
|
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(:create, warning) if warning.text.present?
|
|
|
|
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 { |report| 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
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
reports.each do |report|
|
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)
|
|
|
|
target_account.suspend!
|
|
|
|
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!
|
2020-09-15 14:37:58 +02:00
|
|
|
UserMailer.warning(target_account.user, warning, status_ids).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?
|
|
|
|
send_email_notification && target_account.local?
|
|
|
|
end
|
|
|
|
|
2019-08-23 22:37:23 +02:00
|
|
|
def status_ids
|
2020-09-13 12:52:17 +02:00
|
|
|
report.status_ids if report && include_statuses
|
2019-08-23 22:37:23 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 16:32:44 +02:00
|
|
|
def reports
|
|
|
|
@reports ||= begin
|
|
|
|
if type == 'none' && with_report?
|
|
|
|
[report]
|
|
|
|
else
|
|
|
|
Report.where(target_account: target_account).unresolved
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|