imports_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. def index
  23. @import = Form::Import.new(current_account: current_account)
  24. end
  25. def show; end
  26. def failures
  27. @bulk_import = current_account.bulk_imports.where(state: :finished).find(params[:id])
  28. respond_to do |format|
  29. format.csv do
  30. filename = TYPE_TO_FILENAME_MAP[@bulk_import.type.to_sym]
  31. headers = TYPE_TO_HEADERS_MAP[@bulk_import.type.to_sym]
  32. export_data = CSV.generate(headers: headers, write_headers: true) do |csv|
  33. @bulk_import.rows.find_each do |row|
  34. case @bulk_import.type.to_sym
  35. when :following
  36. csv << [row.data['acct'], row.data.fetch('show_reblogs', true), row.data.fetch('notify', false), row.data['languages']&.join(', ')]
  37. when :blocking
  38. csv << [row.data['acct']]
  39. when :muting
  40. csv << [row.data['acct'], row.data.fetch('hide_notifications', true)]
  41. when :domain_blocking
  42. csv << [row.data['domain']]
  43. when :bookmarks
  44. csv << [row.data['uri']]
  45. when :lists
  46. csv << [row.data['list_name'], row.data['acct']]
  47. end
  48. end
  49. end
  50. send_data export_data, filename: filename
  51. end
  52. end
  53. end
  54. def confirm
  55. @bulk_import.update!(state: :scheduled)
  56. BulkImportWorker.perform_async(@bulk_import.id)
  57. redirect_to settings_imports_path, notice: I18n.t('imports.success')
  58. end
  59. def create
  60. @import = Form::Import.new(import_params.merge(current_account: current_account))
  61. if @import.save
  62. redirect_to settings_import_path(@import.bulk_import.id)
  63. else
  64. # We need to set recent imports as we are displaying the index again
  65. set_recent_imports
  66. render :index
  67. end
  68. end
  69. def destroy
  70. @bulk_import.destroy!
  71. redirect_to settings_imports_path
  72. end
  73. private
  74. def import_params
  75. params.require(:form_import).permit(:data, :type, :mode)
  76. end
  77. def set_bulk_import
  78. @bulk_import = current_account.bulk_imports.where(state: :unconfirmed).find(params[:id])
  79. end
  80. def set_recent_imports
  81. @recent_imports = current_account.bulk_imports.reorder(id: :desc).limit(10)
  82. end
  83. end