relationship_worker.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # frozen_string_literal: true
  2. class Import::RelationshipWorker
  3. include Sidekiq::Worker
  4. sidekiq_options queue: 'pull', retry: 8, dead: false
  5. def perform(account_id, target_account_uri, relationship, options = {})
  6. from_account = Account.find(account_id)
  7. target_domain = domain(target_account_uri)
  8. target_account = stoplight_wrap_request(target_domain) { ResolveAccountService.new.call(target_account_uri, { check_delivery_availability: true }) }
  9. options.symbolize_keys!
  10. return if target_account.nil?
  11. case relationship
  12. when 'follow'
  13. FollowService.new.call(from_account, target_account, options)
  14. when 'unfollow'
  15. UnfollowService.new.call(from_account, target_account)
  16. when 'block'
  17. BlockService.new.call(from_account, target_account)
  18. when 'unblock'
  19. UnblockService.new.call(from_account, target_account)
  20. when 'mute'
  21. MuteService.new.call(from_account, target_account, options)
  22. when 'unmute'
  23. UnmuteService.new.call(from_account, target_account)
  24. end
  25. rescue ActiveRecord::RecordNotFound
  26. true
  27. end
  28. def domain(uri)
  29. domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
  30. TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
  31. end
  32. def stoplight_wrap_request(domain, &block)
  33. if domain.present?
  34. Stoplight("source:#{domain}", &block)
  35. .with_fallback { nil }
  36. .with_threshold(1)
  37. .with_cool_off_time(5.minutes.seconds)
  38. .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
  39. .run
  40. else
  41. block.call
  42. end
  43. end
  44. end