2022-11-17 11:05:09 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
module Admin
|
|
|
|
class ExportDomainBlocksController < BaseController
|
2023-11-30 15:39:41 +01:00
|
|
|
include Admin::ExportControllerConcern
|
2022-11-17 11:05:09 +01:00
|
|
|
|
|
|
|
before_action :set_dummy_import!, only: [:new]
|
|
|
|
|
|
|
|
def new
|
|
|
|
authorize :domain_block, :create?
|
|
|
|
end
|
|
|
|
|
|
|
|
def export
|
|
|
|
authorize :instance, :index?
|
|
|
|
send_export_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def import
|
|
|
|
authorize :domain_block, :create?
|
|
|
|
|
|
|
|
@import = Admin::Import.new(import_params)
|
|
|
|
return render :new unless @import.validate
|
|
|
|
|
|
|
|
@global_private_comment = I18n.t('admin.export_domain_blocks.import.private_comment_template', source: @import.data_file_name, date: I18n.l(Time.now.utc))
|
|
|
|
|
|
|
|
@form = Form::DomainBlockBatch.new
|
2023-01-18 16:20:52 +01:00
|
|
|
@domain_blocks = @import.csv_rows.filter_map do |row|
|
2022-11-17 11:05:09 +01:00
|
|
|
domain = row['#domain'].strip
|
|
|
|
next if DomainBlock.rule_for(domain).present?
|
|
|
|
|
|
|
|
domain_block = DomainBlock.new(domain: domain,
|
2023-01-18 16:20:52 +01:00
|
|
|
severity: row.fetch('#severity', :suspend),
|
|
|
|
reject_media: row.fetch('#reject_media', false),
|
|
|
|
reject_reports: row.fetch('#reject_reports', false),
|
2022-11-17 11:05:09 +01:00
|
|
|
private_comment: @global_private_comment,
|
2023-01-18 16:20:52 +01:00
|
|
|
public_comment: row['#public_comment'],
|
|
|
|
obfuscate: row.fetch('#obfuscate', false))
|
|
|
|
|
|
|
|
if domain_block.invalid?
|
|
|
|
flash.now[:alert] = I18n.t('admin.export_domain_blocks.invalid_domain_block', error: domain_block.errors.full_messages.join(', '))
|
|
|
|
next
|
|
|
|
end
|
2022-11-17 11:05:09 +01:00
|
|
|
|
2023-01-18 16:20:52 +01:00
|
|
|
domain_block
|
|
|
|
rescue ArgumentError => e
|
|
|
|
flash.now[:alert] = I18n.t('admin.export_domain_blocks.invalid_domain_block', error: e.message)
|
|
|
|
next
|
2022-11-17 11:05:09 +01:00
|
|
|
end
|
|
|
|
|
2024-01-25 16:28:27 +01:00
|
|
|
@warning_domains = instances_from_imported_blocks.pluck(:domain)
|
2022-11-17 11:05:09 +01:00
|
|
|
rescue ActionController::ParameterMissing
|
|
|
|
flash.now[:alert] = I18n.t('admin.export_domain_blocks.no_file')
|
|
|
|
set_dummy_import!
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-01-25 16:28:27 +01:00
|
|
|
def instances_from_imported_blocks
|
|
|
|
Instance.with_domain_follows(@domain_blocks.map(&:domain))
|
|
|
|
end
|
|
|
|
|
2022-11-17 11:05:09 +01:00
|
|
|
def export_filename
|
|
|
|
'domain_blocks.csv'
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_headers
|
|
|
|
%w(#domain #severity #reject_media #reject_reports #public_comment #obfuscate)
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_data
|
|
|
|
CSV.generate(headers: export_headers, write_headers: true) do |content|
|
2024-01-03 14:06:15 +01:00
|
|
|
DomainBlock.with_limitations.order(id: :asc).each do |instance|
|
2022-11-17 11:05:09 +01:00
|
|
|
content << [instance.domain, instance.severity, instance.reject_media, instance.reject_reports, instance.public_comment, instance.obfuscate]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|