1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00
mastodon/app/controllers/settings/imports_controller.rb

102 lines
2.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2023-05-02 03:08:48 -07:00
require 'csv'
class Settings::ImportsController < Settings::BaseController
2023-05-02 03:08:48 -07: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',
lists: 'lists_failures.csv',
2023-05-02 03:08:48 -07:00
}.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,
lists: false,
2023-05-02 03:08:48 -07:00
}.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']]
when :lists
csv << [row.data['list_name'], row.data['acct']]
2023-05-02 03:08:48 -07:00
end
end
end
2023-05-02 03:08:48 -07: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')
end
def create
2023-05-02 03:08:48 -07:00
@import = Form::Import.new(import_params.merge(current_account: current_account))
if @import.save
2023-05-02 03:08:48 -07:00
redirect_to settings_import_path(@import.bulk_import.id)
else
2023-05-02 03:08:48 -07:00
# We need to set recent imports as we are displaying the index again
set_recent_imports
render :index
end
end
2023-05-02 03:08:48 -07:00
def destroy
@bulk_import.destroy!
redirect_to settings_imports_path
end
private
2023-05-02 03:08:48 -07:00
def import_params
params.require(:form_import).permit(:data, :type, :mode)
end
2023-05-02 03:08:48 -07: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)
end
end