imports_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. require 'csv'
  3. class Settings::ImportsController < Settings::BaseController
  4. before_action :set_bulk_import, only: [:show, :confirm, :destroy]
  5. before_action :set_recent_imports, only: [:index]
  6. TYPE_TO_FILENAME_MAP = {
  7. following: 'following_accounts_failures.csv',
  8. blocking: 'blocked_accounts_failures.csv',
  9. muting: 'muted_accounts_failures.csv',
  10. domain_blocking: 'blocked_domains_failures.csv',
  11. bookmarks: 'bookmarks_failures.csv',
  12. lists: 'lists_failures.csv',
  13. }.freeze
  14. TYPE_TO_HEADERS_MAP = {
  15. following: ['Account address', 'Show boosts', 'Notify on new posts', 'Languages'],
  16. blocking: false,
  17. muting: ['Account address', 'Hide notifications'],
  18. domain_blocking: false,
  19. bookmarks: false,
  20. lists: false,
  21. }.freeze
  22. RECENT_IMPORTS_LIMIT = 10
  23. def index
  24. @import = Form::Import.new(current_account: current_account)
  25. end
  26. def show; end
  27. def failures
  28. @bulk_import = current_account.bulk_imports.state_finished.find(params[:id])
  29. respond_to do |format|
  30. format.csv do
  31. filename = TYPE_TO_FILENAME_MAP[@bulk_import.type.to_sym]
  32. headers = TYPE_TO_HEADERS_MAP[@bulk_import.type.to_sym]
  33. export_data = CSV.generate(headers: headers, write_headers: true) do |csv|
  34. @bulk_import.rows.find_each do |row|
  35. case @bulk_import.type.to_sym
  36. when :following
  37. csv << [row.data['acct'], row.data.fetch('show_reblogs', true), row.data.fetch('notify', false), row.data['languages']&.join(', ')]
  38. when :blocking
  39. csv << [row.data['acct']]
  40. when :muting
  41. csv << [row.data['acct'], row.data.fetch('hide_notifications', true)]
  42. when :domain_blocking
  43. csv << [row.data['domain']]
  44. when :bookmarks
  45. csv << [row.data['uri']]
  46. when :lists
  47. csv << [row.data['list_name'], row.data['acct']]
  48. end
  49. end
  50. end
  51. send_data export_data, filename: filename
  52. end
  53. end
  54. end
  55. def confirm
  56. @bulk_import.update!(state: :scheduled)
  57. BulkImportWorker.perform_async(@bulk_import.id)
  58. redirect_to settings_imports_path, notice: I18n.t('imports.success')
  59. end
  60. def create
  61. @import = Form::Import.new(import_params.merge(current_account: current_account))
  62. if @import.save
  63. redirect_to settings_import_path(@import.bulk_import.id)
  64. else
  65. # We need to set recent imports as we are displaying the index again
  66. set_recent_imports
  67. render :index
  68. end
  69. end
  70. def destroy
  71. @bulk_import.destroy!
  72. redirect_to settings_imports_path
  73. end
  74. private
  75. def import_params
  76. params.require(:form_import).permit(:data, :type, :mode)
  77. end
  78. def set_bulk_import
  79. @bulk_import = current_account.bulk_imports.state_unconfirmed.find(params[:id])
  80. end
  81. def set_recent_imports
  82. @recent_imports = current_account.bulk_imports.reorder(id: :desc).limit(RECENT_IMPORTS_LIMIT)
  83. end
  84. end