bulk_import_row_service.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_wrap_request(target_domain) { 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_wrap_request(target_domain) { 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_wrap_request(domain, &block)
  45. if domain.present?
  46. Stoplight("source:#{domain}", &block)
  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. .run
  52. else
  53. yield
  54. end
  55. end
  56. end