relationship_worker.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. # NOTE: This is a deprecated worker, only kept to not break ongoing imports
  3. # on upgrade. See `Import::RowWorker` for its replacement.
  4. class Import::RelationshipWorker
  5. include Sidekiq::Worker
  6. sidekiq_options queue: 'pull', retry: 8, dead: false
  7. def perform(account_id, target_account_uri, relationship, options)
  8. from_account = Account.find(account_id)
  9. target_domain = domain(target_account_uri)
  10. target_account = stoplight_wrap_request(target_domain) { ResolveAccountService.new.call(target_account_uri, { check_delivery_availability: true }) }
  11. options.symbolize_keys!
  12. return if target_account.nil?
  13. case relationship
  14. when 'follow'
  15. begin
  16. FollowService.new.call(from_account, target_account, **options)
  17. rescue ActiveRecord::RecordInvalid
  18. raise if FollowLimitValidator.limit_for_account(from_account) < from_account.following_count
  19. end
  20. when 'unfollow'
  21. UnfollowService.new.call(from_account, target_account)
  22. when 'block'
  23. BlockService.new.call(from_account, target_account)
  24. when 'unblock'
  25. UnblockService.new.call(from_account, target_account)
  26. when 'mute'
  27. MuteService.new.call(from_account, target_account, **options)
  28. when 'unmute'
  29. UnmuteService.new.call(from_account, target_account)
  30. end
  31. rescue ActiveRecord::RecordNotFound
  32. true
  33. end
  34. def domain(uri)
  35. domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
  36. TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
  37. end
  38. def stoplight_wrap_request(domain, &block)
  39. if domain.present?
  40. Stoplight("source:#{domain}", &block)
  41. .with_fallback { nil }
  42. .with_threshold(1)
  43. .with_cool_off_time(5.minutes.seconds)
  44. .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
  45. .run
  46. else
  47. yield
  48. end
  49. end
  50. end