2017-03-31 04:42:33 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-05-02 20:08:48 +10:00
|
|
|
require 'csv'
|
|
|
|
|
2018-12-13 08:32:13 +11:00
|
|
|
class Settings::ImportsController < Settings::BaseController
|
2023-05-02 20:08:48 +10:00
|
|
|
before_action :set_bulk_import, only: [:show, :confirm, :destroy]
|
|
|
|
before_action :set_recent_imports, only: [:index]
|
|
|
|
|
|
|
|
TYPE_TO_FILENAME_MAP = {
|
|
|
|
following: 'following_accounts_failures.csv',
|
|
|
|
blocking: 'blocked_accounts_failures.csv',
|
|
|
|
muting: 'muted_accounts_failures.csv',
|
|
|
|
domain_blocking: 'blocked_domains_failures.csv',
|
|
|
|
bookmarks: 'bookmarks_failures.csv',
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
TYPE_TO_HEADERS_MAP = {
|
|
|
|
following: ['Account address', 'Show boosts', 'Notify on new posts', 'Languages'],
|
|
|
|
blocking: false,
|
|
|
|
muting: ['Account address', 'Hide notifications'],
|
|
|
|
domain_blocking: false,
|
|
|
|
bookmarks: false,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def index
|
|
|
|
@import = Form::Import.new(current_account: current_account)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show; end
|
|
|
|
|
|
|
|
def failures
|
|
|
|
@bulk_import = current_account.bulk_imports.where(state: :finished).find(params[:id])
|
|
|
|
|
|
|
|
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']]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-31 04:42:33 +11:00
|
|
|
|
2023-05-02 20:08:48 +10:00
|
|
|
send_data export_data, filename: filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm
|
|
|
|
@bulk_import.update!(state: :scheduled)
|
|
|
|
BulkImportWorker.perform_async(@bulk_import.id)
|
|
|
|
redirect_to settings_imports_path, notice: I18n.t('imports.success')
|
2017-03-31 04:42:33 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2023-05-02 20:08:48 +10:00
|
|
|
@import = Form::Import.new(import_params.merge(current_account: current_account))
|
2017-03-31 04:42:33 +11:00
|
|
|
|
|
|
|
if @import.save
|
2023-05-02 20:08:48 +10:00
|
|
|
redirect_to settings_import_path(@import.bulk_import.id)
|
2017-03-31 04:42:33 +11:00
|
|
|
else
|
2023-05-02 20:08:48 +10:00
|
|
|
# We need to set recent imports as we are displaying the index again
|
|
|
|
set_recent_imports
|
|
|
|
render :index
|
2017-03-31 04:42:33 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-02 20:08:48 +10:00
|
|
|
def destroy
|
|
|
|
@bulk_import.destroy!
|
|
|
|
redirect_to settings_imports_path
|
|
|
|
end
|
|
|
|
|
2017-03-31 04:42:33 +11:00
|
|
|
private
|
|
|
|
|
2023-05-02 20:08:48 +10:00
|
|
|
def import_params
|
|
|
|
params.require(:form_import).permit(:data, :type, :mode)
|
2017-03-31 04:42:33 +11:00
|
|
|
end
|
|
|
|
|
2023-05-02 20:08:48 +10:00
|
|
|
def set_bulk_import
|
|
|
|
@bulk_import = current_account.bulk_imports.where(state: :unconfirmed).find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_recent_imports
|
|
|
|
@recent_imports = current_account.bulk_imports.reorder(id: :desc).limit(10)
|
2017-03-31 04:42:33 +11:00
|
|
|
end
|
|
|
|
end
|