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.

90 lines
2.2 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. @data = CSV.new(import_data).reject(&:blank?)
  9. case @import.type
  10. when 'following'
  11. import_follows!
  12. when 'blocking'
  13. import_blocks!
  14. when 'muting'
  15. import_mutes!
  16. when 'domain_blocking'
  17. import_domain_blocks!
  18. end
  19. end
  20. private
  21. def import_follows!
  22. import_relationships!('follow', 'unfollow', @account.following, follow_limit)
  23. end
  24. def import_blocks!
  25. import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
  26. end
  27. def import_mutes!
  28. import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT)
  29. end
  30. def import_domain_blocks!
  31. items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row.first.strip }
  32. if @import.overwrite?
  33. presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
  34. @account.domain_blocks.find_each do |domain_block|
  35. if presence_hash[domain_block.domain]
  36. items.delete(domain_block.domain)
  37. else
  38. @account.unblock_domain!(domain_block.domain)
  39. end
  40. end
  41. end
  42. items.each do |domain|
  43. @account.block_domain!(domain)
  44. end
  45. AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
  46. [@account.id, domain]
  47. end
  48. end
  49. def import_relationships!(action, undo_action, overwrite_scope, limit)
  50. items = @data.take(limit).map { |row| row.first.strip }
  51. if @import.overwrite?
  52. presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
  53. overwrite_scope.find_each do |target_account|
  54. if presence_hash[target_account.acct]
  55. items.delete(target_account.acct)
  56. else
  57. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
  58. end
  59. end
  60. end
  61. Import::RelationshipWorker.push_bulk(items) do |acct|
  62. [@account.id, acct, action]
  63. end
  64. end
  65. def import_data
  66. Paperclip.io_adapters.for(@import.data).read
  67. end
  68. def follow_limit
  69. FollowLimitValidator.limit_for_account(@account)
  70. end
  71. end