bulk_import_row_service.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. class BulkImportRowService
  3. def call(row)
  4. @account = row.bulk_import.account
  5. @data = row.data
  6. @type = row.bulk_import.type.to_sym
  7. case @type
  8. when :following, :blocking, :muting, :lists
  9. target_acct = @data['acct']
  10. target_domain = domain(target_acct)
  11. @target_account = stoplight_wrapper(target_domain).run { ResolveAccountService.new.call(target_acct, { check_delivery_availability: true }) }
  12. return false if @target_account.nil?
  13. when :bookmarks
  14. target_uri = @data['uri']
  15. target_domain = Addressable::URI.parse(target_uri).normalized_host
  16. @target_status = ActivityPub::TagManager.instance.uri_to_resource(target_uri, Status)
  17. return false if @target_status.nil? && ActivityPub::TagManager.instance.local_uri?(target_uri)
  18. @target_status ||= stoplight_wrapper(target_domain).run { ActivityPub::FetchRemoteStatusService.new.call(target_uri) }
  19. return false if @target_status.nil?
  20. end
  21. case @type
  22. when :following
  23. FollowService.new.call(@account, @target_account, reblogs: @data['show_reblogs'], notify: @data['notify'], languages: @data['languages'])
  24. when :blocking
  25. BlockService.new.call(@account, @target_account)
  26. when :muting
  27. MuteService.new.call(@account, @target_account, notifications: @data['hide_notifications'])
  28. when :bookmarks
  29. return false unless StatusPolicy.new(@account, @target_status).show?
  30. @account.bookmarks.find_or_create_by!(status: @target_status)
  31. when :lists
  32. list = @account.owned_lists.find_or_create_by!(title: @data['list_name'])
  33. FollowService.new.call(@account, @target_account) unless @account.id == @target_account.id
  34. list.accounts << @target_account
  35. end
  36. true
  37. rescue ActiveRecord::RecordNotFound
  38. false
  39. end
  40. def domain(uri)
  41. domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
  42. TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
  43. end
  44. def stoplight_wrapper(domain)
  45. if domain.present?
  46. Stoplight("source:#{domain}")
  47. .with_fallback { nil }
  48. .with_threshold(1)
  49. .with_cool_off_time(5.minutes.seconds)
  50. .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
  51. else
  52. Stoplight('domain-blank')
  53. end
  54. end
  55. end