import_service.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. local_domain_suffix = "@#{Rails.configuration.x.local_domain}"
  54. items = @data.take(limit).map { |row| [row['Account address']&.strip&.delete_suffix(local_domain_suffix), Hash[extra_fields.map { |key, header| [key, row[header]&.strip] }]] }.reject { |(id, _)| id.blank? }
  55. if @import.overwrite?
  56. presence_hash = items.each_with_object({}) { |(id, extra), mapping| mapping[id] = [true, extra] }
  57. overwrite_scope.find_each do |target_account|
  58. if presence_hash[target_account.acct]
  59. items.delete(target_account.acct)
  60. extra = presence_hash[target_account.acct][1]
  61. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, extra)
  62. else
  63. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
  64. end
  65. end
  66. end
  67. Import::RelationshipWorker.push_bulk(items) do |acct, extra|
  68. [@account.id, acct, action, extra]
  69. end
  70. end
  71. def parse_import_data!(default_headers)
  72. data = CSV.parse(import_data, headers: true)
  73. data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
  74. @data = data.reject(&:blank?)
  75. end
  76. def import_data
  77. Paperclip.io_adapters.for(@import.data).read
  78. end
  79. def follow_limit
  80. FollowLimitValidator.limit_for_account(@account)
  81. end
  82. end