Instances list in admin (#2095)

* Add admin/instances index action

* Add link to instances admin page

* View lists instances

* Instances, grouped by domain, ordered by count

* Use Account.remote scope

* Extract method: Account.by_domain_accounts
This commit is contained in:
Matt Jankowski 2017-04-18 15:09:07 -04:00 committed by Eugen
parent 66d8f99a30
commit 55e1503522
8 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
module Admin
class InstancesController < BaseController
def index
@instances = ordered_instances.page(params[:page])
end
private
def ordered_instances
Account.remote.by_domain_accounts
end
end
end

View file

@ -68,6 +68,7 @@ class Account < ApplicationRecord
scope :suspended, -> { where(suspended: true) } scope :suspended, -> { where(suspended: true) }
scope :recent, -> { reorder(id: :desc) } scope :recent, -> { reorder(id: :desc) }
scope :alphabetic, -> { order(domain: :asc, username: :asc) } scope :alphabetic, -> { order(domain: :asc, username: :asc) }
scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') }
def follow!(other_account) def follow!(other_account)
active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account) active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)

View file

@ -0,0 +1,15 @@
- content_for :page_title do
= t('admin.instances.title')
%table.table
%thead
%tr
%th= t('admin.instances.domain_name')
%th= t('admin.instances.account_count')
%tbody
- @instances.each do |instance|
%tr
%td= instance.domain
%td= instance.accounts_count
= paginate @instances

View file

@ -114,6 +114,10 @@ en:
undo: Undo undo: Undo
title: Domain Blocks title: Domain Blocks
undo: Undo undo: Undo
instances:
account_count: Accounts
domain_name: Domain name
title: Instances
pubsubhubbub: pubsubhubbub:
callback_url: Callback URL callback_url: Callback URL
confirmed: Confirmed confirmed: Confirmed

View file

@ -17,6 +17,7 @@ SimpleNavigation::Configuration.run do |navigation|
primary.item :admin, safe_join([fa_icon('cogs fw'), t('admin.title')]), admin_reports_url, if: proc { current_user.admin? } do |admin| primary.item :admin, safe_join([fa_icon('cogs fw'), t('admin.title')]), admin_reports_url, if: proc { current_user.admin? } do |admin|
admin.item :reports, safe_join([fa_icon('flag fw'), t('admin.reports.title')]), admin_reports_url, highlights_on: %r{/admin/reports} admin.item :reports, safe_join([fa_icon('flag fw'), t('admin.reports.title')]), admin_reports_url, highlights_on: %r{/admin/reports}
admin.item :accounts, safe_join([fa_icon('users fw'), t('admin.accounts.title')]), admin_accounts_url, highlights_on: %r{/admin/accounts} admin.item :accounts, safe_join([fa_icon('users fw'), t('admin.accounts.title')]), admin_accounts_url, highlights_on: %r{/admin/accounts}
admin.item :instances, safe_join([fa_icon('cloud fw'), t('admin.instances.title')]), admin_instances_url, highlights_on: %r{/admin/instances}
admin.item :pubsubhubbubs, safe_join([fa_icon('paper-plane-o fw'), t('admin.pubsubhubbub.title')]), admin_pubsubhubbub_index_url admin.item :pubsubhubbubs, safe_join([fa_icon('paper-plane-o fw'), t('admin.pubsubhubbub.title')]), admin_pubsubhubbub_index_url
admin.item :domain_blocks, safe_join([fa_icon('lock fw'), t('admin.domain_blocks.title')]), admin_domain_blocks_url, highlights_on: %r{/admin/domain_blocks} admin.item :domain_blocks, safe_join([fa_icon('lock fw'), t('admin.domain_blocks.title')]), admin_domain_blocks_url, highlights_on: %r{/admin/domain_blocks}
admin.item :sidekiq, safe_join([fa_icon('diamond fw'), 'Sidekiq']), sidekiq_url, link_html: { target: 'sidekiq' } admin.item :sidekiq, safe_join([fa_icon('diamond fw'), 'Sidekiq']), sidekiq_url, link_html: { target: 'sidekiq' }

View file

@ -80,6 +80,7 @@ Rails.application.routes.draw do
resources :pubsubhubbub, only: [:index] resources :pubsubhubbub, only: [:index]
resources :domain_blocks, only: [:index, :new, :create, :show, :destroy] resources :domain_blocks, only: [:index, :new, :create, :show, :destroy]
resources :settings, only: [:index, :update] resources :settings, only: [:index, :update]
resources :instances, only: [:index]
resources :reports, only: [:index, :show, :update] do resources :reports, only: [:index, :show, :update] do
resources :reported_statuses, only: :destroy resources :reported_statuses, only: :destroy

View file

@ -0,0 +1,15 @@
require 'rails_helper'
RSpec.describe Admin::InstancesController, type: :controller do
before do
sign_in Fabricate(:user, admin: true), scope: :user
end
describe 'GET #index' do
it 'returns http success' do
get :index
expect(response).to have_http_status(:success)
end
end
end

View file

@ -410,6 +410,20 @@ RSpec.describe Account, type: :model do
end end
end end
describe 'by_domain_accounts' do
it 'returns accounts grouped by domain sorted by accounts' do
2.times { Fabricate(:account, domain: 'example.com') }
Fabricate(:account, domain: 'example2.com')
results = Account.by_domain_accounts
expect(results.length).to eq 2
expect(results.first.domain).to eq 'example.com'
expect(results.first.accounts_count).to eq 2
expect(results.last.domain).to eq 'example2.com'
expect(results.last.accounts_count).to eq 1
end
end
describe 'local' do describe 'local' do
it 'returns an array of accounts who do not have a domain' do it 'returns an array of accounts who do not have a domain' do
account_1 = Fabricate(:account, domain: nil) account_1 = Fabricate(:account, domain: nil)