2017-10-03 15:39:32 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Import::RelationshipWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
sidekiq_options queue: 'pull', retry: 8, dead: false
|
|
|
|
|
2021-05-06 05:22:54 -07:00
|
|
|
def perform(account_id, target_account_uri, relationship, options)
|
2017-10-03 15:39:32 -07:00
|
|
|
from_account = Account.find(account_id)
|
2020-06-09 01:26:58 -07:00
|
|
|
target_domain = domain(target_account_uri)
|
|
|
|
target_account = stoplight_wrap_request(target_domain) { ResolveAccountService.new.call(target_account_uri, { check_delivery_availability: true }) }
|
2019-04-07 22:28:27 -07:00
|
|
|
options.symbolize_keys!
|
2017-10-03 15:39:32 -07:00
|
|
|
|
|
|
|
return if target_account.nil?
|
|
|
|
|
|
|
|
case relationship
|
|
|
|
when 'follow'
|
2020-12-26 14:52:46 -08:00
|
|
|
begin
|
2021-05-06 05:22:54 -07:00
|
|
|
FollowService.new.call(from_account, target_account, **options)
|
2020-12-26 14:52:46 -08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
raise if FollowLimitValidator.limit_for_account(from_account) < from_account.following_count
|
|
|
|
end
|
2019-02-02 18:59:51 -08:00
|
|
|
when 'unfollow'
|
|
|
|
UnfollowService.new.call(from_account, target_account)
|
2017-10-03 15:39:32 -07:00
|
|
|
when 'block'
|
|
|
|
BlockService.new.call(from_account, target_account)
|
2019-02-02 18:59:51 -08:00
|
|
|
when 'unblock'
|
|
|
|
UnblockService.new.call(from_account, target_account)
|
2017-10-03 15:39:32 -07:00
|
|
|
when 'mute'
|
2021-05-06 05:22:54 -07:00
|
|
|
MuteService.new.call(from_account, target_account, **options)
|
2019-02-02 18:59:51 -08:00
|
|
|
when 'unmute'
|
|
|
|
UnmuteService.new.call(from_account, target_account)
|
2017-10-03 15:39:32 -07:00
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
true
|
|
|
|
end
|
2020-06-09 01:26:58 -07:00
|
|
|
|
|
|
|
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
|
2023-02-07 15:58:18 -08:00
|
|
|
yield
|
2020-06-09 01:26:58 -07:00
|
|
|
end
|
|
|
|
end
|
2017-10-03 15:39:32 -07:00
|
|
|
end
|