This commit is contained in:
Matt Jankowski 2025-09-05 09:07:27 +00:00 committed by GitHub
commit e7ca12a533
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 89 additions and 23 deletions

View File

@ -37,29 +37,7 @@ class Settings::ImportsController < Settings::BaseController
respond_to do |format|
format.csv do
filename = TYPE_TO_FILENAME_MAP[@bulk_import.type.to_sym]
headers = TYPE_TO_HEADERS_MAP[@bulk_import.type.to_sym]
export_data = CSV.generate(headers: headers, write_headers: true) do |csv|
@bulk_import.rows.find_each do |row|
case @bulk_import.type.to_sym
when :following
csv << [row.data['acct'], row.data.fetch('show_reblogs', true), row.data.fetch('notify', false), row.data['languages']&.join(', ')]
when :blocking
csv << [row.data['acct']]
when :muting
csv << [row.data['acct'], row.data.fetch('hide_notifications', true)]
when :domain_blocking
csv << [row.data['domain']]
when :bookmarks
csv << [row.data['uri']]
when :lists
csv << [row.data['list_name'], row.data['acct']]
end
end
end
send_data export_data, filename: filename
send_data export_data, filename: filename_for_type
end
end
end
@ -89,6 +67,22 @@ class Settings::ImportsController < Settings::BaseController
private
def export_data
CSV.generate(headers: headers_for_type, write_headers: true) do |csv|
@bulk_import.rows.find_each do |row|
csv << row.to_csv
end
end
end
def filename_for_type
TYPE_TO_FILENAME_MAP[@bulk_import.type.to_sym]
end
def headers_for_type
TYPE_TO_HEADERS_MAP[@bulk_import.type.to_sym]
end
def import_params
params.expect(form_import: [:data, :type, :mode])
end

View File

@ -12,4 +12,27 @@
#
class BulkImportRow < ApplicationRecord
belongs_to :bulk_import
def to_csv
case bulk_import.type.to_sym
when :following
[data['acct'], data.fetch('show_reblogs', true), data.fetch('notify', false), language_list]
when :blocking
[data['acct']]
when :muting
[data['acct'], data.fetch('hide_notifications', true)]
when :domain_blocking
[data['domain']]
when :bookmarks
[data['uri']]
when :lists
[data['list_name'], data['acct']]
end
end
private
def language_list
data['languages']&.join(', ')
end
end

View File

@ -0,0 +1,49 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe BulkImportRow do
describe 'Associations' do
it { is_expected.to belong_to(:bulk_import).required }
end
describe '#to_csv' do
subject { described_class.new(bulk_import: Fabricate.build(:bulk_import, type:), data: {}).to_csv }
context 'when bulk import is following type' do
let(:type) { :following }
it { is_expected.to be_a(Array) }
end
context 'when bulk import is blocking type' do
let(:type) { :blocking }
it { is_expected.to be_a(Array) }
end
context 'when bulk import is muting type' do
let(:type) { :muting }
it { is_expected.to be_a(Array) }
end
context 'when bulk import is domain_blocking type' do
let(:type) { :domain_blocking }
it { is_expected.to be_a(Array) }
end
context 'when bulk import is bookmarks type' do
let(:type) { :bookmarks }
it { is_expected.to be_a(Array) }
end
context 'when bulk import is lists type' do
let(:type) { :lists }
it { is_expected.to be_a(Array) }
end
end
end