11# frozen_string_literal: true
22
33class BulkImportRowService
4+ STOPLIGHT_COOL_OFF_TIME = 5 . minutes . seconds
5+ STOPLIGHT_THRESHOLD = 1
6+
47 def call ( row )
58 @account = row . bulk_import . account
69 @data = row . data
@@ -10,15 +13,15 @@ def call(row)
1013 when :following , :blocking , :muting , :lists
1114 target_acct = @data [ 'acct' ]
1215 target_domain = domain ( target_acct )
13- @target_account = stoplight_wrapper ( target_domain ) . run { ResolveAccountService . new . call ( target_acct , { check_delivery_availability : true } ) }
16+ @target_account = stoplight_wrapper ( target_domain ) . run ( stoplight_fallback ) { ResolveAccountService . new . call ( target_acct , { check_delivery_availability : true } ) }
1417 return false if @target_account . nil?
1518 when :bookmarks
1619 target_uri = @data [ 'uri' ]
1720 target_domain = Addressable ::URI . parse ( target_uri ) . normalized_host
1821 @target_status = ActivityPub ::TagManager . instance . uri_to_resource ( target_uri , Status )
1922 return false if @target_status . nil? && ActivityPub ::TagManager . instance . local_uri? ( target_uri )
2023
21- @target_status ||= stoplight_wrapper ( target_domain ) . run { ActivityPub ::FetchRemoteStatusService . new . call ( target_uri ) }
24+ @target_status ||= stoplight_wrapper ( target_domain ) . run ( stoplight_fallback ) { ActivityPub ::FetchRemoteStatusService . new . call ( target_uri ) }
2225 return false if @target_status . nil?
2326 end
2427
@@ -51,13 +54,18 @@ def domain(uri)
5154 TagManager . instance . local_domain? ( domain ) ? nil : TagManager . instance . normalize_domain ( domain )
5255 end
5356
57+ def stoplight_fallback
58+ -> ( error ) { }
59+ end
60+
5461 def stoplight_wrapper ( domain )
5562 if domain . present?
56- Stoplight ( "source:#{ domain } " )
57- . with_fallback { nil }
58- . with_threshold ( 1 )
59- . with_cool_off_time ( 5 . minutes . seconds )
60- . with_error_handler { |error , handle | error . is_a? ( HTTP ::Error ) || error . is_a? ( OpenSSL ::SSL ::SSLError ) ? handle . call ( error ) : raise ( error ) }
63+ Stoplight (
64+ "source:#{ domain } " ,
65+ cool_off_time : STOPLIGHT_COOL_OFF_TIME ,
66+ threshold : STOPLIGHT_THRESHOLD ,
67+ tracked_errors : [ HTTP ::Error , OpenSSL ::SSL ::SSLError ]
68+ )
6169 else
6270 Stoplight ( 'domain-blank' )
6371 end
0 commit comments