Admin accounts controller cleanup (#1664)
* Remove unused account_params method in admin/accounts controller * Introduce AccountFilter to find accounts * Use AccountFilter in admin/accounts controller * Use more restful routes admin silence and suspension area * Add admin/silences and admin/suspensions controllers
This commit is contained in:
@ -2,49 +2,29 @@
|
||||
|
||||
module Admin
|
||||
class AccountsController < BaseController
|
||||
before_action :set_account, except: :index
|
||||
|
||||
def index
|
||||
@accounts = Account.alphabetic.page(params[:page])
|
||||
|
||||
@accounts = @accounts.local if params[:local].present?
|
||||
@accounts = @accounts.remote if params[:remote].present?
|
||||
@accounts = @accounts.where(domain: params[:by_domain]) if params[:by_domain].present?
|
||||
@accounts = @accounts.silenced if params[:silenced].present?
|
||||
@accounts = @accounts.recent if params[:recent].present?
|
||||
@accounts = @accounts.suspended if params[:suspended].present?
|
||||
@accounts = filtered_accounts.page(params[:page])
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def suspend
|
||||
Admin::SuspensionWorker.perform_async(@account.id)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
def unsuspend
|
||||
@account.update(suspended: false)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
def silence
|
||||
@account.update(silenced: true)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
def unsilence
|
||||
@account.update(silenced: false)
|
||||
redirect_to admin_accounts_path
|
||||
def show
|
||||
@account = Account.find(params[:id])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_account
|
||||
@account = Account.find(params[:id])
|
||||
def filtered_accounts
|
||||
AccountFilter.new(filter_params).results
|
||||
end
|
||||
|
||||
def account_params
|
||||
params.require(:account).permit(:silenced, :suspended)
|
||||
def filter_params
|
||||
params.permit(
|
||||
:local,
|
||||
:remote,
|
||||
:by_domain,
|
||||
:silenced,
|
||||
:recent,
|
||||
:suspended
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
23
app/controllers/admin/silences_controller.rb
Normal file
23
app/controllers/admin/silences_controller.rb
Normal file
@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Admin
|
||||
class SilencesController < BaseController
|
||||
before_action :set_account
|
||||
|
||||
def create
|
||||
@account.update(silenced: true)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
def destroy
|
||||
@account.update(silenced: false)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_account
|
||||
@account = Account.find(params[:account_id])
|
||||
end
|
||||
end
|
||||
end
|
23
app/controllers/admin/suspensions_controller.rb
Normal file
23
app/controllers/admin/suspensions_controller.rb
Normal file
@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Admin
|
||||
class SuspensionsController < BaseController
|
||||
before_action :set_account
|
||||
|
||||
def create
|
||||
Admin::SuspensionWorker.perform_async(@account.id)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
def destroy
|
||||
@account.update(suspended: false)
|
||||
redirect_to admin_accounts_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_account
|
||||
@account = Account.find(params[:account_id])
|
||||
end
|
||||
end
|
||||
end
|
36
app/models/account_filter.rb
Normal file
36
app/models/account_filter.rb
Normal file
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AccountFilter
|
||||
attr_reader :params
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def results
|
||||
scope = Account.alphabetic
|
||||
params.each do |key, value|
|
||||
scope = scope.merge scope_for(key, value)
|
||||
end
|
||||
scope
|
||||
end
|
||||
|
||||
def scope_for(key, value)
|
||||
case key
|
||||
when /local/
|
||||
Account.local
|
||||
when /remote/
|
||||
Account.remote
|
||||
when /by_domain/
|
||||
Account.where(domain: value)
|
||||
when /silenced/
|
||||
Account.silenced
|
||||
when /recent/
|
||||
Account.recent
|
||||
when /suspended/
|
||||
Account.suspended
|
||||
else
|
||||
raise "Unknown filter: #{key}"
|
||||
end
|
||||
end
|
||||
end
|
@ -62,11 +62,11 @@
|
||||
= number_to_human_size @account.media_attachments.sum('file_file_size')
|
||||
|
||||
- if @account.silenced?
|
||||
= link_to 'Undo silence', unsilence_admin_account_path(@account.id), method: :post, class: 'button'
|
||||
= link_to 'Undo silence', admin_account_silence_path(@account.id), method: :delete, class: 'button'
|
||||
- else
|
||||
= link_to 'Silence', silence_admin_account_path(@account.id), method: :post, class: 'button'
|
||||
= link_to 'Silence', admin_account_silence_path(@account.id), method: :post, class: 'button'
|
||||
|
||||
- if @account.suspended?
|
||||
= link_to 'Undo suspension', unsuspend_admin_account_path(@account.id), method: :post, class: 'button'
|
||||
= link_to 'Undo suspension', admin_account_suspension_path(@account.id), method: :delete, class: 'button'
|
||||
- else
|
||||
= link_to 'Perform full suspension', suspend_admin_account_path(@account.id), method: :post, data: { confirm: 'Are you sure?' }, class: 'button'
|
||||
= link_to 'Perform full suspension', admin_account_suspension_path(@account.id), method: :post, data: { confirm: 'Are you sure?' }, class: 'button'
|
||||
|
Reference in New Issue
Block a user