diff --git a/app/controllers/settings/exports/blocked_accounts_controller.rb b/app/controllers/settings/exports/blocked_accounts_controller.rb new file mode 100644 index 00000000000..0bf8848b49c --- /dev/null +++ b/app/controllers/settings/exports/blocked_accounts_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Settings + module Exports + class BlockedAccountsController < ApplicationController + before_action :authenticate_user! + + def index + export_data = Export.new(current_account.blocking).to_csv + + respond_to do |format| + format.csv { send_data export_data, filename: 'blocking.csv' } + end + end + end + end +end diff --git a/app/controllers/settings/exports/following_accounts_controller.rb b/app/controllers/settings/exports/following_accounts_controller.rb new file mode 100644 index 00000000000..a7f4344ca78 --- /dev/null +++ b/app/controllers/settings/exports/following_accounts_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Settings + module Exports + class FollowingAccountsController < ApplicationController + before_action :authenticate_user! + + def index + export_data = Export.new(current_account.following).to_csv + + respond_to do |format| + format.csv { send_data export_data, filename: 'following.csv' } + end + end + end + end +end diff --git a/app/controllers/settings/exports_controller.rb b/app/controllers/settings/exports_controller.rb index ff688978cc3..e060f03d3c6 100644 --- a/app/controllers/settings/exports_controller.rb +++ b/app/controllers/settings/exports_controller.rb @@ -1,46 +1,13 @@ # frozen_string_literal: true -require 'csv' - class Settings::ExportsController < ApplicationController layout 'admin' before_action :authenticate_user! - before_action :set_account def show @total_storage = current_account.media_attachments.sum(:file_file_size) @total_follows = current_account.following.count @total_blocks = current_account.blocking.count end - - def download_following_list - @accounts = current_account.following - - respond_to do |format| - format.csv { render text: accounts_list_to_csv(@accounts) } - end - end - - def download_blocking_list - @accounts = current_account.blocking - - respond_to do |format| - format.csv { render text: accounts_list_to_csv(@accounts) } - end - end - - private - - def set_account - @account = current_user.account - end - - def accounts_list_to_csv(list) - CSV.generate do |csv| - list.each do |account| - csv << [(account.local? ? account.local_username_and_domain : account.acct)] - end - end - end end diff --git a/app/models/export.rb b/app/models/export.rb new file mode 100644 index 00000000000..cd1a58eb6dd --- /dev/null +++ b/app/models/export.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true +require 'csv' + +class Export + attr_reader :accounts + + def initialize(accounts) + @accounts = accounts + end + + def to_csv + CSV.generate do |csv| + accounts.each do |account| + csv << [(account.local? ? account.local_username_and_domain : account.acct)] + end + end + end +end diff --git a/app/views/settings/exports/show.html.haml b/app/views/settings/exports/show.html.haml index 0a0ff863325..432a61b4a43 100644 --- a/app/views/settings/exports/show.html.haml +++ b/app/views/settings/exports/show.html.haml @@ -10,8 +10,8 @@ %tr %th= t('exports.follows') %td= @total_follows - %td= table_link_to 'download', t('exports.csv'), follows_settings_export_path(format: :csv) + %td= table_link_to 'download', t('exports.csv'), settings_exports_follows_path(format: :csv) %tr %th= t('exports.blocks') %td= @total_blocks - %td= table_link_to 'download', t('exports.csv'), blocks_settings_export_path(format: :csv) + %td= table_link_to 'download', t('exports.csv'), settings_exports_blocks_path(format: :csv) diff --git a/config/routes.rb b/config/routes.rb index 9adcdb862b7..69f8887b2fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,11 +53,10 @@ Rails.application.routes.draw do resource :preferences, only: [:show, :update] resource :import, only: [:show, :create] - resource :export, only: [:show] do - collection do - get :follows, to: 'exports#download_following_list' - get :blocks, to: 'exports#download_blocking_list' - end + resource :export, only: [:show] + namespace :exports, constraints: { format: :csv } do + resources :follows, only: :index, controller: :following_accounts + resources :blocks, only: :index, controller: :blocked_accounts end resource :two_factor_auth, only: [:show, :new, :create] do diff --git a/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb b/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb new file mode 100644 index 00000000000..574d4d87586 --- /dev/null +++ b/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe Settings::Exports::BlockedAccountsController do + before do + sign_in Fabricate(:user), scope: :user + end + + describe 'GET #index' do + it 'returns a csv of the blocking accounts' do + get :index, format: :csv + + expect(response).to have_http_status(:success) + expect(response.content_type).to eq 'text/csv' + expect(response.headers['Content-Disposition']).to eq 'attachment; filename="blocking.csv"' + end + end +end diff --git a/spec/controllers/settings/exports/following_accounts_controller_spec.rb b/spec/controllers/settings/exports/following_accounts_controller_spec.rb new file mode 100644 index 00000000000..bf768052324 --- /dev/null +++ b/spec/controllers/settings/exports/following_accounts_controller_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe Settings::Exports::FollowingAccountsController do + before do + sign_in Fabricate(:user), scope: :user + end + + describe 'GET #index' do + it 'returns a csv of the following accounts' do + get :index, format: :csv + + expect(response).to have_http_status(:success) + expect(response.content_type).to eq 'text/csv' + expect(response.headers['Content-Disposition']).to eq 'attachment; filename="following.csv"' + end + end +end diff --git a/spec/controllers/settings/exports_controller_spec.rb b/spec/controllers/settings/exports_controller_spec.rb new file mode 100644 index 00000000000..ff98f3ad945 --- /dev/null +++ b/spec/controllers/settings/exports_controller_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +describe Settings::ExportsController do + before do + sign_in Fabricate(:user), scope: :user + end + + describe 'GET #show' do + it 'returns http success' do + get :show + expect(response).to have_http_status(:success) + end + end +end diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb new file mode 100644 index 00000000000..5cc62c26624 --- /dev/null +++ b/spec/models/export_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe Export do + describe 'to_csv' do + it 'returns a csv of the accounts' do + one = Account.new(username: 'one', domain: 'local.host') + two = Account.new(username: 'two', domain: 'local.host') + accounts = [one, two] + + export = Export.new(accounts).to_csv + results = export.strip.split + + expect(results.size).to eq 2 + expect(results.first).to eq 'one@local.host' + end + end +end