about summary refs log tree commit diff
path: root/app/workers/import
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2020-06-09 13:00:24 +0200
committerGitHub <noreply@github.com>2020-06-09 13:00:24 +0200
commit3287a10fe9658a6c370431a925abc50d4f68ea08 (patch)
tree88a408a943aa421a6ba844d309f673150cd92434 /app/workers/import
parentf328f2faa3fbdb182921366c6a20e745c069b840 (diff)
parente9d7ca7645fc37534df1222e8d7ab8f3d533b852 (diff)
Merge pull request #1350 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/workers/import')
-rw-r--r--app/workers/import/relationship_worker.rb21
1 files changed, 20 insertions, 1 deletions
diff --git a/app/workers/import/relationship_worker.rb b/app/workers/import/relationship_worker.rb
index 616da6da9..4a455f3ae 100644
--- a/app/workers/import/relationship_worker.rb
+++ b/app/workers/import/relationship_worker.rb
@@ -7,7 +7,8 @@ class Import::RelationshipWorker
 
   def perform(account_id, target_account_uri, relationship, options = {})
     from_account   = Account.find(account_id)
-    target_account = ResolveAccountService.new.call(target_account_uri)
+    target_domain  = domain(target_account_uri)
+    target_account = stoplight_wrap_request(target_domain) { ResolveAccountService.new.call(target_account_uri, { check_delivery_availability: true }) }
     options.symbolize_keys!
 
     return if target_account.nil?
@@ -29,4 +30,22 @@ class Import::RelationshipWorker
   rescue ActiveRecord::RecordNotFound
     true
   end
+
+  def domain(uri)
+    domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
+    TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
+  end
+
+  def stoplight_wrap_request(domain, &block)
+    if domain.present?
+      Stoplight("source:#{domain}", &block)
+        .with_fallback { nil }
+        .with_threshold(1)
+        .with_cool_off_time(5.minutes.seconds)
+        .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
+        .run
+    else
+      block.call
+    end
+  end
 end