import_service.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # frozen_string_literal: true
  2. require 'csv'
  3. # NOTE: This is a deprecated service, only kept to not break ongoing imports
  4. # on upgrade. See `BulkImportService` for its replacement.
  5. class ImportService < BaseService
  6. ROWS_PROCESSING_LIMIT = 20_000
  7. def call(import)
  8. @import = import
  9. @account = @import.account
  10. case @import.type
  11. when 'following'
  12. import_follows!
  13. when 'blocking'
  14. import_blocks!
  15. when 'muting'
  16. import_mutes!
  17. when 'domain_blocking'
  18. import_domain_blocks!
  19. when 'bookmarks'
  20. import_bookmarks!
  21. end
  22. end
  23. private
  24. def import_follows!
  25. parse_import_data!(['Account address'])
  26. import_relationships!('follow', 'unfollow', @account.following, ROWS_PROCESSING_LIMIT, reblogs: { header: 'Show boosts', default: true }, notify: { header: 'Notify on new posts', default: false }, languages: { header: 'Languages', default: nil })
  27. end
  28. def import_blocks!
  29. parse_import_data!(['Account address'])
  30. import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
  31. end
  32. def import_mutes!
  33. parse_import_data!(['Account address'])
  34. import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT, notifications: { header: 'Hide notifications', default: true })
  35. end
  36. def import_domain_blocks!
  37. parse_import_data!(['#domain'])
  38. items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row['#domain'].strip }
  39. if @import.overwrite?
  40. presence_hash = items.index_with(true)
  41. @account.domain_blocks.find_each do |domain_block|
  42. if presence_hash[domain_block.domain]
  43. items.delete(domain_block.domain)
  44. else
  45. @account.unblock_domain!(domain_block.domain)
  46. end
  47. end
  48. end
  49. items.each do |domain|
  50. @account.block_domain!(domain)
  51. end
  52. AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
  53. [@account.id, domain]
  54. end
  55. end
  56. def import_relationships!(action, undo_action, overwrite_scope, limit, extra_fields = {})
  57. local_domain_suffix = "@#{Rails.configuration.x.local_domain}"
  58. items = @data.take(limit).map { |row| [row['Account address']&.strip&.delete_suffix(local_domain_suffix), extra_fields.to_h { |key, field_settings| [key, row[field_settings[:header]]&.strip || field_settings[:default]] }] }.reject { |(id, _)| id.blank? }
  59. if @import.overwrite?
  60. presence_hash = items.each_with_object({}) { |(id, extra), mapping| mapping[id] = [true, extra] }
  61. overwrite_scope.reorder(nil).find_each do |target_account|
  62. if presence_hash[target_account.acct]
  63. items.delete(target_account.acct)
  64. extra = presence_hash[target_account.acct][1]
  65. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, action, extra.stringify_keys)
  66. else
  67. Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
  68. end
  69. end
  70. end
  71. head_items = items.uniq { |acct, _| acct.split('@')[1] }
  72. tail_items = items - head_items
  73. Import::RelationshipWorker.push_bulk(head_items + tail_items) do |acct, extra|
  74. [@account.id, acct, action, extra.stringify_keys]
  75. end
  76. end
  77. def import_bookmarks!
  78. parse_import_data!(['#uri'])
  79. items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row['#uri'].strip }
  80. if @import.overwrite?
  81. presence_hash = items.index_with(true)
  82. @account.bookmarks.find_each do |bookmark|
  83. if presence_hash[bookmark.status.uri]
  84. items.delete(bookmark.status.uri)
  85. else
  86. bookmark.destroy!
  87. end
  88. end
  89. end
  90. statuses = items.filter_map do |uri|
  91. status = ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
  92. next if status.nil? && ActivityPub::TagManager.instance.local_uri?(uri)
  93. status || ActivityPub::FetchRemoteStatusService.new.call(uri)
  94. rescue *Mastodon::HTTP_CONNECTION_ERRORS, Mastodon::UnexpectedResponseError
  95. nil
  96. rescue => e
  97. Rails.logger.warn "Unexpected error when importing bookmark: #{e}"
  98. nil
  99. end
  100. account_ids = statuses.map(&:account_id)
  101. preloaded_relations = @account.relations_map(account_ids, skip_blocking_and_muting: true)
  102. statuses.keep_if { |status| StatusPolicy.new(@account, status, preloaded_relations).show? }
  103. statuses.each do |status|
  104. @account.bookmarks.find_or_create_by!(account: @account, status: status)
  105. end
  106. end
  107. def parse_import_data!(default_headers)
  108. data = CSV.parse(import_data, headers: true)
  109. data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
  110. @data = data.compact_blank
  111. end
  112. def import_data
  113. Paperclip.io_adapters.for(@import.data).read.force_encoding(Encoding::UTF_8)
  114. end
  115. end