export_domain_blocks_controller.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # frozen_string_literal: true
  2. require 'csv'
  3. module Admin
  4. class ExportDomainBlocksController < BaseController
  5. include AdminExportControllerConcern
  6. before_action :set_dummy_import!, only: [:new]
  7. def new
  8. authorize :domain_block, :create?
  9. end
  10. def export
  11. authorize :instance, :index?
  12. send_export_file
  13. end
  14. def import
  15. authorize :domain_block, :create?
  16. @import = Admin::Import.new(import_params)
  17. return render :new unless @import.validate
  18. @global_private_comment = I18n.t('admin.export_domain_blocks.import.private_comment_template', source: @import.data_file_name, date: I18n.l(Time.now.utc))
  19. @form = Form::DomainBlockBatch.new
  20. @domain_blocks = @import.csv_rows.filter_map do |row|
  21. domain = row['#domain'].strip
  22. next if DomainBlock.rule_for(domain).present?
  23. domain_block = DomainBlock.new(domain: domain,
  24. severity: row.fetch('#severity', :suspend),
  25. reject_media: row.fetch('#reject_media', false),
  26. reject_reports: row.fetch('#reject_reports', false),
  27. private_comment: @global_private_comment,
  28. public_comment: row['#public_comment'],
  29. obfuscate: row.fetch('#obfuscate', false))
  30. if domain_block.invalid?
  31. flash.now[:alert] = I18n.t('admin.export_domain_blocks.invalid_domain_block', error: domain_block.errors.full_messages.join(', '))
  32. next
  33. end
  34. domain_block
  35. rescue ArgumentError => e
  36. flash.now[:alert] = I18n.t('admin.export_domain_blocks.invalid_domain_block', error: e.message)
  37. next
  38. end
  39. @warning_domains = Instance.where(domain: @domain_blocks.map(&:domain)).where('EXISTS (SELECT 1 FROM follows JOIN accounts ON follows.account_id = accounts.id OR follows.target_account_id = accounts.id WHERE accounts.domain = instances.domain)').pluck(:domain)
  40. rescue ActionController::ParameterMissing
  41. flash.now[:alert] = I18n.t('admin.export_domain_blocks.no_file')
  42. set_dummy_import!
  43. render :new
  44. end
  45. private
  46. def export_filename
  47. 'domain_blocks.csv'
  48. end
  49. def export_headers
  50. %w(#domain #severity #reject_media #reject_reports #public_comment #obfuscate)
  51. end
  52. def export_data
  53. CSV.generate(headers: export_headers, write_headers: true) do |content|
  54. DomainBlock.with_limitations.each do |instance|
  55. content << [instance.domain, instance.severity, instance.reject_media, instance.reject_reports, instance.public_comment, instance.obfuscate]
  56. end
  57. end
  58. end
  59. end
  60. end