You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

  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. begin
  14. FollowService.new.call(from_account, target_account, **options)
  15. rescue ActiveRecord::RecordInvalid
  16. raise if FollowLimitValidator.limit_for_account(from_account) < from_account.following_count
  17. end
  18. when 'unfollow'
  19. UnfollowService.new.call(from_account, target_account)
  20. when 'block'
  21. BlockService.new.call(from_account, target_account)
  22. when 'unblock'
  23. UnblockService.new.call(from_account, target_account)
  24. when 'mute'
  25. MuteService.new.call(from_account, target_account, **options)
  26. when 'unmute'
  27. UnmuteService.new.call(from_account, target_account)
  28. end
  29. rescue ActiveRecord::RecordNotFound
  30. true
  31. end
  32. def domain(uri)
  33. domain = uri.is_a?(Account) ? uri.domain : uri.split('@')[1]
  34. TagManager.instance.local_domain?(domain) ? nil : TagManager.instance.normalize_domain(domain)
  35. end
  36. def stoplight_wrap_request(domain, &block)
  37. if domain.present?
  38. Stoplight("source:#{domain}", &block)
  39. .with_fallback { nil }
  40. .with_threshold(1)
  41. .with_cool_off_time(5.minutes.seconds)
  42. .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
  43. .run
  44. else
  45. block.call
  46. end
  47. end
  48. end