From d95f6f4410c28b2f7b9f736c9477936587bd700b Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 3 Oct 2024 09:09:58 -0400 Subject: [PATCH] Extract `ExportSummary` class for account object counts (#32227) --- .../settings/exports_controller.rb | 13 ++- app/models/export.rb | 36 -------- app/presenters/export_summary.rb | 70 +++++++++++++++ app/views/settings/exports/show.html.haml | 18 ++-- spec/models/export_spec.rb | 71 --------------- spec/presenters/export_summary_spec.rb | 86 +++++++++++++++++++ 6 files changed, 177 insertions(+), 117 deletions(-) create mode 100644 app/presenters/export_summary.rb create mode 100644 spec/presenters/export_summary_spec.rb diff --git a/app/controllers/settings/exports_controller.rb b/app/controllers/settings/exports_controller.rb index 076ed5dadb..263d20eaea 100644 --- a/app/controllers/settings/exports_controller.rb +++ b/app/controllers/settings/exports_controller.rb @@ -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 diff --git a/app/models/export.rb b/app/models/export.rb index 2457dcc156..6ed9f60c7c 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -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) diff --git a/app/presenters/export_summary.rb b/app/presenters/export_summary.rb new file mode 100644 index 0000000000..8e45aadf67 --- /dev/null +++ b/app/presenters/export_summary.rb @@ -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 diff --git a/app/views/settings/exports/show.html.haml b/app/views/settings/exports/show.html.haml index 61d55350c2..b36e395e35 100644 --- a/app/views/settings/exports/show.html.haml +++ b/app/views/settings/exports/show.html.haml @@ -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/ diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb index 48e78830dd..81aaf88585 100644 --- a/spec/models/export_spec.rb +++ b/spec/models/export_spec.rb @@ -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 diff --git a/spec/presenters/export_summary_spec.rb b/spec/presenters/export_summary_spec.rb new file mode 100644 index 0000000000..0ed46c857d --- /dev/null +++ b/spec/presenters/export_summary_spec.rb @@ -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