Extract ExportSummary class for account object counts (#32227)

This commit is contained in:
Matt Jankowski 2024-10-03 09:09:58 -04:00 committed by GitHub
parent 52afa94f1c
commit d95f6f4410
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 177 additions and 117 deletions

View File

@ -9,7 +9,7 @@ class Settings::ExportsController < Settings::BaseController
skip_before_action :require_functional!
def show
@export = Export.new(current_account)
@export_summary = ExportSummary.new(preloaded_account)
@backups = current_user.backups
end
@ -25,4 +25,15 @@ class Settings::ExportsController < Settings::BaseController
redirect_to settings_export_path
end
private
def preloaded_account
current_account.tap do |account|
ActiveRecord::Associations::Preloader.new(
records: [account],
associations: :account_stat
).call
end
end
end

View File

@ -55,42 +55,6 @@ class Export
end
end
def total_storage
account.media_attachments.sum(:file_file_size)
end
def total_statuses
account.statuses_count
end
def total_bookmarks
account.bookmarks.count
end
def total_follows
account.following_count
end
def total_lists
account.owned_lists.count
end
def total_followers
account.followers_count
end
def total_blocks
account.blocking.count
end
def total_mutes
account.muting.count
end
def total_domain_blocks
account.domain_blocks.count
end
private
def to_csv(accounts)

View File

@ -0,0 +1,70 @@
# frozen_string_literal: true
class ExportSummary
attr_reader :account, :counts
delegate(
:blocking,
:bookmarks,
:domain_blocks,
:owned_lists,
:media_attachments,
:muting,
to: :account,
prefix: true
)
def initialize(account)
@account = account
@counts = populate_counts
end
def total_blocks
counts[:blocks].value
end
def total_bookmarks
counts[:bookmarks].value
end
def total_domain_blocks
counts[:domain_blocks].value
end
def total_followers
account.followers_count
end
def total_follows
account.following_count
end
def total_lists
counts[:owned_lists].value
end
def total_mutes
counts[:muting].value
end
def total_statuses
account.statuses_count
end
def total_storage
counts[:storage].value
end
private
def populate_counts
{
blocks: account_blocking.async_count,
bookmarks: account_bookmarks.async_count,
domain_blocks: account_domain_blocks.async_count,
owned_lists: account_owned_lists.async_count,
muting: account_muting.async_count,
storage: account_media_attachments.async_sum(:file_file_size),
}
end
end

View File

@ -6,39 +6,39 @@
%tbody
%tr
%th= t('exports.storage')
%td= number_to_human_size @export.total_storage
%td= number_to_human_size @export_summary.total_storage
%td
%tr
%th= t('accounts.posts_tab_heading')
%td= number_with_delimiter @export.total_statuses
%td= number_with_delimiter @export_summary.total_statuses
%td
%tr
%th= t('admin.accounts.follows')
%td= number_with_delimiter @export.total_follows
%td= number_with_delimiter @export_summary.total_follows
%td= table_link_to 'download', t('exports.csv'), settings_exports_follows_path(format: :csv)
%tr
%th= t('exports.lists')
%td= number_with_delimiter @export.total_lists
%td= number_with_delimiter @export_summary.total_lists
%td= table_link_to 'download', t('exports.csv'), settings_exports_lists_path(format: :csv)
%tr
%th= t('admin.accounts.followers')
%td= number_with_delimiter @export.total_followers
%td= number_with_delimiter @export_summary.total_followers
%td
%tr
%th= t('exports.mutes')
%td= number_with_delimiter @export.total_mutes
%td= number_with_delimiter @export_summary.total_mutes
%td= table_link_to 'download', t('exports.csv'), settings_exports_mutes_path(format: :csv)
%tr
%th= t('exports.blocks')
%td= number_with_delimiter @export.total_blocks
%td= number_with_delimiter @export_summary.total_blocks
%td= table_link_to 'download', t('exports.csv'), settings_exports_blocks_path(format: :csv)
%tr
%th= t('exports.domain_blocks')
%td= number_with_delimiter @export.total_domain_blocks
%td= number_with_delimiter @export_summary.total_domain_blocks
%td= table_link_to 'download', t('exports.csv'), settings_exports_domain_blocks_path(format: :csv)
%tr
%th= t('exports.bookmarks')
%td= number_with_delimiter @export.total_bookmarks
%td= number_with_delimiter @export_summary.total_bookmarks
%td= table_link_to 'download', t('exports.csv'), settings_exports_bookmarks_path(format: :csv)
%hr.spacer/

View File

@ -103,75 +103,4 @@ RSpec.describe Export do
)
end
end
describe '#total_storage' do
it 'returns the total size of the media attachments' do
media_attachment = Fabricate(:media_attachment, account: account)
expect(subject.total_storage).to eq media_attachment.file_file_size || 0
end
end
describe '#total_statuses' do
before { Fabricate.times(2, :status, account: account) }
it 'returns the total number of statuses' do
expect(subject.total_statuses).to eq(2)
end
end
describe '#total_bookmarks' do
before { Fabricate.times(2, :bookmark, account: account) }
it 'returns the total number of bookmarks' do
expect(subject.total_bookmarks).to eq(2)
end
end
describe '#total_follows' do
before { target_accounts.each { |target_account| account.follow!(target_account) } }
it 'returns the total number of the followed accounts' do
expect(subject.total_follows).to eq(2)
end
end
describe '#total_lists' do
before { Fabricate.times(2, :list, account: account) }
it 'returns the total number of lists' do
expect(subject.total_lists).to eq(2)
end
end
describe '#total_followers' do
before { target_accounts.each { |target_account| target_account.follow!(account) } }
it 'returns the total number of the follower accounts' do
expect(subject.total_followers).to eq(2)
end
end
describe '#total_blocks' do
before { target_accounts.each { |target_account| account.block!(target_account) } }
it 'returns the total number of the blocked accounts' do
expect(subject.total_blocks).to eq(2)
end
end
describe '#total_mutes' do
before { target_accounts.each { |target_account| account.mute!(target_account) } }
it 'returns the total number of the muted accounts' do
expect(subject.total_mutes).to eq(2)
end
end
describe '#total_domain_blocks' do
before { Fabricate.times(2, :account_domain_block, account: account) }
it 'returns the total number of account domain blocks' do
expect(subject.total_domain_blocks).to eq(2)
end
end
end

View File

@ -0,0 +1,86 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ExportSummary do
subject { described_class.new(account) }
let(:account) { Fabricate(:account) }
let(:target_accounts) do
[
Fabricate(:account),
Fabricate(:account, username: 'one', domain: 'local.host'),
]
end
describe '#total_storage' do
it 'returns the total size of the media attachments' do
media_attachment = Fabricate(:media_attachment, account: account)
expect(subject.total_storage).to eq media_attachment.file_file_size || 0
end
end
describe '#total_statuses' do
before { Fabricate.times(2, :status, account: account) }
it 'returns the total number of statuses' do
expect(subject.total_statuses).to eq(2)
end
end
describe '#total_bookmarks' do
before { Fabricate.times(2, :bookmark, account: account) }
it 'returns the total number of bookmarks' do
expect(subject.total_bookmarks).to eq(2)
end
end
describe '#total_follows' do
before { target_accounts.each { |target_account| account.follow!(target_account) } }
it 'returns the total number of the followed accounts' do
expect(subject.total_follows).to eq(2)
end
end
describe '#total_lists' do
before { Fabricate.times(2, :list, account: account) }
it 'returns the total number of lists' do
expect(subject.total_lists).to eq(2)
end
end
describe '#total_followers' do
before { target_accounts.each { |target_account| target_account.follow!(account) } }
it 'returns the total number of the follower accounts' do
expect(subject.total_followers).to eq(2)
end
end
describe '#total_blocks' do
before { target_accounts.each { |target_account| account.block!(target_account) } }
it 'returns the total number of the blocked accounts' do
expect(subject.total_blocks).to eq(2)
end
end
describe '#total_mutes' do
before { target_accounts.each { |target_account| account.mute!(target_account) } }
it 'returns the total number of the muted accounts' do
expect(subject.total_mutes).to eq(2)
end
end
describe '#total_domain_blocks' do
before { Fabricate.times(2, :account_domain_block, account: account) }
it 'returns the total number of account domain blocks' do
expect(subject.total_domain_blocks).to eq(2)
end
end
end