闭社主体 forked from https://github.com/tootsuite/mastodon
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.

104 lines
3.1 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. 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. head_items = items.uniq { |acct, _| acct.split('@')[1] }
  68. tail_items = items - head_items
  69. Import::RelationshipWorker.push_bulk(head_items + tail_items) do |acct, extra|
  70. [@account.id, acct, action, extra]
  71. end
  72. end
  73. def parse_import_data!(default_headers)
  74. data = CSV.parse(import_data, headers: true)
  75. data = CSV.parse(import_data, headers: default_headers) unless data.headers&.first&.strip&.include?(' ')
  76. @data = data.reject(&:blank?)
  77. end
  78. def import_data
  79. Paperclip.io_adapters.for(@import.data).read
  80. end
  81. def follow_limit
  82. FollowLimitValidator.limit_for_account(@account)
  83. end
  84. end