import_service.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 }, notify: { header: 'Notify on new posts', default: false }, languages: { header: 'Languages', default: nil })
  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. rescue HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::UnexpectedResponseError
  93. nil
  94. rescue StandardError => e
  95. Rails.logger.warn "Unexpected error when importing bookmark: #{e}"
  96. nil
  97. end
  98. account_ids = statuses.map(&:account_id)
  99. preloaded_relations = relations_map_for_account(@account, account_ids)
  100. statuses.keep_if { |status| StatusPolicy.new(@account, status, preloaded_relations).show? }
  101. statuses.each do |status|
  102. @account.bookmarks.find_or_create_by!(account: @account, status: status)
  103. end
  104. end
  105. def parse_import_data!(default_headers)
  106. data = CSV.parse(import_data, headers: true)
  107. data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
  108. @data = data.reject(&:blank?)
  109. end
  110. def import_data
  111. Paperclip.io_adapters.for(@import.data).read.force_encoding(Encoding::UTF_8)
  112. end
  113. def relations_map_for_account(account, account_ids)
  114. {
  115. blocking: {},
  116. blocked_by: Account.blocked_by_map(account_ids, account.id),
  117. muting: {},
  118. following: Account.following_map(account_ids, account.id),
  119. domain_blocking_by_domain: {},
  120. }
  121. end
  122. end