relationship_worker.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_wrapper(target_domain).run { 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_wrapper(domain)
  39. if domain.present?
  40. Stoplight("source:#{domain}")
  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. else
  46. Stoplight('domain-blank')
  47. end
  48. end
  49. end