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.

146 lines
4.5 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. when 'bookmarks'
  18. import_bookmarks!
  19. end
  20. end
  21. private
  22. def import_follows!
  23. parse_import_data!(['Account address'])
  24. import_relationships!('follow', 'unfollow', @account.following, ROWS_PROCESSING_LIMIT, reblogs: { header: 'Show boosts', default: true })
  25. end
  26. def import_blocks!
  27. parse_import_data!(['Account address'])
  28. import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
  29. end
  30. def import_mutes!
  31. parse_import_data!(['Account address'])
  32. import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT, notifications: { header: 'Hide notifications', default: true })
  33. end
  34. def import_domain_blocks!
  35. parse_import_data!(['#domain'])
  36. items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row['#domain'].strip }
  37. if @import.overwrite?
  38. presence_hash = items.index_with(true)
  39. @account.domain_blocks.find_each do |domain_block|
  40. if presence_hash[domain_block.domain]
  41. items.delete(domain_block.domain)
  42. else
  43. @account.unblock_domain!(domain_block.domain)
  44. end
  45. end
  46. end
  47. items.each do |domain|
  48. @account.block_domain!(domain)
  49. end
  50. AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
  51. [@account.id, domain]
  52. end
  53. end
  54. def import_relationships!(action, undo_action, overwrite_scope, limit, extra_fields = {})
  55. local_domain_suffix = "@#{Rails.configuration.x.local_domain}"
  56. items = @data.take(limit).map { |row| [row['Account address']&.strip&.delete_suffix(local_domain_suffix), Hash[extra_fields.map { |key, field_settings| [key, row[field_settings[:header]]&.strip || field_settings[:default]] }]] }.reject { |(id, _)| id.blank? }
  57. if @import.overwrite?
  58. presence_hash = items.each_with_object({}) { |(id, extra), mapping| mapping[id] = [true, extra] }
  59. overwrite_scope.find_each do |target_account|
  60. if presence_hash[target_account.acct]
  61. items.delete(target_account.acct)
  62. extra = presence_hash[target_account.acct][1]
  63. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, extra.stringify_keys)
  64. else
  65. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
  66. end
  67. end
  68. end
  69. head_items = items.uniq { |acct, _| acct.split('@')[1] }
  70. tail_items = items - head_items
  71. Import::RelationshipWorker.push_bulk(head_items + tail_items) do |acct, extra|
  72. [@account.id, acct, action, extra.stringify_keys]
  73. end
  74. end
  75. def import_bookmarks!
  76. parse_import_data!(['#uri'])
  77. items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row['#uri'].strip }
  78. if @import.overwrite?
  79. presence_hash = items.index_with(true)
  80. @account.bookmarks.find_each do |bookmark|
  81. if presence_hash[bookmark.status.uri]
  82. items.delete(bookmark.status.uri)
  83. else
  84. bookmark.destroy!
  85. end
  86. end
  87. end
  88. statuses = items.filter_map do |uri|
  89. status = ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
  90. next if status.nil? && ActivityPub::TagManager.instance.local_uri?(uri)
  91. status || ActivityPub::FetchRemoteStatusService.new.call(uri)
  92. end
  93. account_ids = statuses.map(&:account_id)
  94. preloaded_relations = relations_map_for_account(@account, account_ids)
  95. statuses.keep_if { |status| StatusPolicy.new(@account, status, preloaded_relations).show? }
  96. statuses.each do |status|
  97. @account.bookmarks.find_or_create_by!(account: @account, status: status)
  98. end
  99. end
  100. def parse_import_data!(default_headers)
  101. data = CSV.parse(import_data, headers: true)
  102. data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
  103. @data = data.reject(&:blank?)
  104. end
  105. def import_data
  106. Paperclip.io_adapters.for(@import.data).read
  107. end
  108. def relations_map_for_account(account, account_ids)
  109. {
  110. blocking: {},
  111. blocked_by: Account.blocked_by_map(account_ids, account.id),
  112. muting: {},
  113. following: Account.following_map(account_ids, account.id),
  114. domain_blocking_by_domain: {},
  115. }
  116. end
  117. end