|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class Api::V1::Admin::AccountsController < Api::BaseController |
| 4 | + include Authorization |
| 5 | + include AccountableConcern |
| 6 | + |
| 7 | + LIMIT = 100 |
| 8 | + |
| 9 | + before_action -> { doorkeeper_authorize! :'admin:read', :'admin:read:accounts' }, only: [:index, :show] |
| 10 | + before_action -> { doorkeeper_authorize! :'admin:write', :'admin:write:accounts' }, except: [:index, :show] |
| 11 | + before_action :require_staff! |
| 12 | + before_action :set_accounts, only: :index |
| 13 | + before_action :set_account, except: :index |
| 14 | + before_action :require_local_account!, only: [:enable, :approve, :reject] |
| 15 | + |
| 16 | + after_action :insert_pagination_headers, only: :index |
| 17 | + |
| 18 | + FILTER_PARAMS = %i( |
| 19 | + local |
| 20 | + remote |
| 21 | + by_domain |
| 22 | + active |
| 23 | + pending |
| 24 | + disabled |
| 25 | + silenced |
| 26 | + suspended |
| 27 | + username |
| 28 | + display_name |
| 29 | + email |
| 30 | + ip |
| 31 | + staff |
| 32 | + ).freeze |
| 33 | + |
| 34 | + PAGINATION_PARAMS = (%i(limit) + FILTER_PARAMS).freeze |
| 35 | + |
| 36 | + def index |
| 37 | + authorize :account, :index? |
| 38 | + render json: @accounts, each_serializer: REST::Admin::AccountSerializer |
| 39 | + end |
| 40 | + |
| 41 | + def show |
| 42 | + authorize @account, :show? |
| 43 | + render json: @account, serializer: REST::Admin::AccountSerializer |
| 44 | + end |
| 45 | + |
| 46 | + def enable |
| 47 | + authorize @account.user, :enable? |
| 48 | + @account.user.enable! |
| 49 | + log_action :enable, @account.user |
| 50 | + render json: @account, serializer: REST::Admin::AccountSerializer |
| 51 | + end |
| 52 | + |
| 53 | + def approve |
| 54 | + authorize @account.user, :approve? |
| 55 | + @account.user.approve! |
| 56 | + render json: @account, serializer: REST::Admin::AccountSerializer |
| 57 | + end |
| 58 | + |
| 59 | + def reject |
| 60 | + authorize @account.user, :reject? |
| 61 | + SuspendAccountService.new.call(@account, including_user: true, destroy: true, skip_distribution: true) |
| 62 | + render json: @account, serializer: REST::Admin::AccountSerializer |
| 63 | + end |
| 64 | + |
| 65 | + def unsilence |
| 66 | + authorize @account, :unsilence? |
| 67 | + @account.unsilence! |
| 68 | + log_action :unsilence, @account |
| 69 | + render json: @account, serializer: REST::Admin::AccountSerializer |
| 70 | + end |
| 71 | + |
| 72 | + def unsuspend |
| 73 | + authorize @account, :unsuspend? |
| 74 | + @account.unsuspend! |
| 75 | + log_action :unsuspend, @account |
| 76 | + render json: @account, serializer: REST::Admin::AccountSerializer |
| 77 | + end |
| 78 | + |
| 79 | + private |
| 80 | + |
| 81 | + def set_accounts |
| 82 | + @accounts = filtered_accounts.order(id: :desc).includes(user: [:invite_request, :invite]).paginate_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id)) |
| 83 | + end |
| 84 | + |
| 85 | + def set_account |
| 86 | + @account = Account.find(params[:id]) |
| 87 | + end |
| 88 | + |
| 89 | + def filtered_accounts |
| 90 | + AccountFilter.new(filter_params).results |
| 91 | + end |
| 92 | + |
| 93 | + def filter_params |
| 94 | + params.permit(*FILTER_PARAMS) |
| 95 | + end |
| 96 | + |
| 97 | + def insert_pagination_headers |
| 98 | + set_pagination_headers(next_path, prev_path) |
| 99 | + end |
| 100 | + |
| 101 | + def next_path |
| 102 | + api_v1_admin_accounts_url(pagination_params(max_id: pagination_max_id)) if records_continue? |
| 103 | + end |
| 104 | + |
| 105 | + def prev_path |
| 106 | + api_v1_admin_accounts_url(pagination_params(min_id: pagination_since_id)) unless @accounts.empty? |
| 107 | + end |
| 108 | + |
| 109 | + def pagination_max_id |
| 110 | + @accounts.last.id |
| 111 | + end |
| 112 | + |
| 113 | + def pagination_since_id |
| 114 | + @accounts.first.id |
| 115 | + end |
| 116 | + |
| 117 | + def records_continue? |
| 118 | + @accounts.size == limit_param(LIMIT) |
| 119 | + end |
| 120 | + |
| 121 | + def pagination_params(core_params) |
| 122 | + params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params) |
| 123 | + end |
| 124 | + |
| 125 | + def require_local_account! |
| 126 | + forbidden unless @account.local? && @account.user.present? |
| 127 | + end |
| 128 | +end |
0 commit comments