You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.9 KiB

  1. # frozen_string_literal: true
  2. require 'csv'
  3. class ImportService < BaseService
  4. ROWS_PROCESSING_LIMIT = 20_000
  5. def call(import)
  6. @import = import
  7. @account = @import.account
  8. case @import.type
  9. when 'following'
  10. import_follows!
  11. when 'blocking'
  12. import_blocks!
  13. when 'muting'
  14. import_mutes!
  15. when 'domain_blocking'
  16. import_domain_blocks!
  17. end
  18. end
  19. private
  20. def import_follows!
  21. parse_import_data!(['Account address'])
  22. import_relationships!('follow', 'unfollow', @account.following, follow_limit, reblogs: 'Show boosts')
  23. end
  24. def import_blocks!
  25. parse_import_data!(['Account address'])
  26. import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
  27. end
  28. def import_mutes!
  29. parse_import_data!(['Account address'])
  30. import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT, notifications: 'Hide notifications')
  31. end
  32. def import_domain_blocks!
  33. parse_import_data!(['#domain'])
  34. items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row['#domain'].strip }
  35. if @import.overwrite?
  36. presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
  37. @account.domain_blocks.find_each do |domain_block|
  38. if presence_hash[domain_block.domain]
  39. items.delete(domain_block.domain)
  40. else
  41. @account.unblock_domain!(domain_block.domain)
  42. end
  43. end
  44. end
  45. items.each do |domain|
  46. @account.block_domain!(domain)
  47. end
  48. AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
  49. [@account.id, domain]
  50. end
  51. end
  52. def import_relationships!(action, undo_action, overwrite_scope, limit, extra_fields = {})
  53. items = @data.take(limit).map { |row| [row['Account address']&.strip, Hash[extra_fields.map { |key, header| [key, row[header]&.strip] }]] }.reject { |(id, _)| id.blank? }
  54. if @import.overwrite?
  55. presence_hash = items.each_with_object({}) { |(id, extra), mapping| mapping[id] = [true, extra] }
  56. overwrite_scope.find_each do |target_account|
  57. if presence_hash[target_account.acct]
  58. items.delete(target_account.acct)
  59. extra = presence_hash[target_account.acct][1]
  60. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, extra)
  61. else
  62. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
  63. end
  64. end
  65. end
  66. Import::RelationshipWorker.push_bulk(items) do |acct, extra|
  67. [@account.id, acct, action, extra]
  68. end
  69. end
  70. def parse_import_data!(default_headers)
  71. data = CSV.parse(import_data, headers: true)
  72. data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
  73. @data = data.reject(&:blank?)
  74. end
  75. def import_data
  76. Paperclip.io_adapters.for(@import.data).read
  77. end
  78. def follow_limit
  79. FollowLimitValidator.limit_for_account(@account)
  80. end
  81. end