mirror of
https://github.com/mastodon/mastodon.git
synced 2025-09-05 17:31:12 +00:00

Related to - https://github.com/mastodon/mastodon/pull/35458 - which added a batch base class, and to - https://github.com/mastodon/mastodon/pull/35872 - which envisions a world where form objects tell the view what to do. Background here... - The `Form::AccountBatch` form object is used in a few different places, for somewhat varied purposes - The user relationships view uses it on a show page, which does a PUT to update, and uses the `follow`, `unfollow`, `remove_from_followers`, and `remove_domains_from_followers` "actions" of the form model - The admin accounts view shows a form on index view, does POST to batch action, uses the `suspend`, `approve` and `reject` actions. - The admin relationships view (for admins only) also has a form on index and does a POST back to that same spot - The admin/follow_recommendations page has a form on show and does PUT to update, using the `suppress_follow_recommendation` and `unsuppress_follow_recommendation` actions So, while these are all related in that they supply an `accounts_ids` array, the actual things that they "bulk update" are Follow (from relationships), FollowRecommendationSuppression (from follow recs) and Account (from accounts). This is a little inconsistent with other of the "batch form model" classes, which tend to update the same domain concept (ie, CustomEmojiBlock, IpBlockBatch, EmailDomainBlockBatch, etc) that they are named after (and in some cases that a "filter" class is named after). All that said, the changes here: - Pull out `Form::FollowRecommendationBatch` and `Form::RelationshipBatch` classes, which more closely align with the domain concept they are bulk updating (open to naming suggestions here) - Mark both of these as `persisted?` true so that their form views will infer correct HTTP action - Add some missing coverage to existing class, and coverage for new classes as well It would be pretty straightforward to chop this up into smaller pieces, if any of "just the coverage first", "just the boolean attribute", "one new batch class at a time", etc - were more appealing. Possible follow-up: - In the existing `Form::AccountBatch`, use attributes API for boolean `select_all_matching` value, remove query method - Convert the relationships controller spec to mix of request/system specs, round out coverage - Most (all maybe?) of the "batch actions" are doing a `POST` to a `batch` action on the controller of whatever they are related to. Might be worth looking at what the diff would look like to move these all to nested controllers where they did a `PUT to */batches#update` with restful routing, etc.
92 lines
2.0 KiB
Ruby
92 lines
2.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Form::AccountBatch < Form::BaseBatch
|
|
include Payloadable
|
|
|
|
attr_accessor :account_ids,
|
|
:query,
|
|
:select_all_matching
|
|
|
|
def save
|
|
case action
|
|
when 'approve'
|
|
approve!
|
|
when 'reject'
|
|
reject!
|
|
when 'suspend'
|
|
suspend!
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def accounts
|
|
if select_all_matching?
|
|
query
|
|
else
|
|
Account.where(id: account_ids)
|
|
end
|
|
end
|
|
|
|
def approve!
|
|
accounts.includes(:user).find_each do |account|
|
|
approve_account(account)
|
|
end
|
|
end
|
|
|
|
def reject!
|
|
accounts.includes(:user).find_each do |account|
|
|
reject_account(account)
|
|
end
|
|
end
|
|
|
|
def suspend!
|
|
accounts.find_each do |account|
|
|
if account.user_pending?
|
|
reject_account(account)
|
|
else
|
|
suspend_account(account)
|
|
end
|
|
end
|
|
end
|
|
|
|
def reject_account(account)
|
|
authorize(account.user, :reject?)
|
|
log_action(:reject, account.user)
|
|
account.suspend!(origin: :local)
|
|
AccountDeletionWorker.perform_async(account.id, { 'reserve_username' => false })
|
|
end
|
|
|
|
def suspend_account(account)
|
|
authorize(account, :suspend?)
|
|
log_action(:suspend, account)
|
|
account.suspend!(origin: :local)
|
|
account.strikes.create!(
|
|
account: current_account,
|
|
action: :suspend
|
|
)
|
|
|
|
Admin::SuspensionWorker.perform_async(account.id)
|
|
|
|
# Suspending a single account closes their associated reports, so
|
|
# mass-suspending would be consistent.
|
|
account.targeted_reports.unresolved.find_each do |report|
|
|
authorize(report, :update?)
|
|
log_action(:resolve, report)
|
|
report.resolve!(current_account)
|
|
rescue Mastodon::NotPermittedError
|
|
# This should not happen, but just in case, do not fail early
|
|
end
|
|
end
|
|
|
|
def approve_account(account)
|
|
authorize(account.user, :approve?)
|
|
log_action(:approve, account.user)
|
|
account.user.approve!
|
|
end
|
|
|
|
def select_all_matching?
|
|
select_all_matching == '1'
|
|
end
|
|
end
|