From 10014f29a5f93ba5a8e9401ba197b6b13d62a19c Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 28 Apr 2022 20:19:10 +0200 Subject: [PATCH] Fix temporary network/remote server error prevent from interactions with remote accounts (#18161) * Fix temporary network/remote server error prevent from interactions with remote accounts * Fix and add tests --- app/services/resolve_url_service.rb | 5 +++++ spec/services/resolve_url_service_spec.rb | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/services/resolve_url_service.rb b/app/services/resolve_url_service.rb index 5981e4d98a..e2c745673e 100644 --- a/app/services/resolve_url_service.rb +++ b/app/services/resolve_url_service.rb @@ -30,6 +30,11 @@ class ResolveURLService < BaseService end def process_url_from_db + if [500, 502, 503, 504, nil].include?(fetch_resource_service.response_code) + account = Account.find_by(uri: @url) + return account unless account.nil? + end + return unless @on_behalf_of.present? && [401, 403, 404].include?(fetch_resource_service.response_code) # It may happen that the resource is a private toot, and thus not fetchable, diff --git a/spec/services/resolve_url_service_spec.rb b/spec/services/resolve_url_service_spec.rb index a38b235900..1b639dea98 100644 --- a/spec/services/resolve_url_service_spec.rb +++ b/spec/services/resolve_url_service_spec.rb @@ -7,15 +7,29 @@ describe ResolveURLService, type: :service do describe '#call' do it 'returns nil when there is no resource url' do - url = 'http://example.com/missing-resource' + url = 'http://example.com/missing-resource' + known_account = Fabricate(:account, uri: url) service = double allow(FetchResourceService).to receive(:new).and_return service + allow(service).to receive(:response_code).and_return(404) allow(service).to receive(:call).with(url).and_return(nil) expect(subject.call(url)).to be_nil end + it 'returns known account on temporary error' do + url = 'http://example.com/missing-resource' + known_account = Fabricate(:account, uri: url) + service = double + + allow(FetchResourceService).to receive(:new).and_return service + allow(service).to receive(:response_code).and_return(500) + allow(service).to receive(:call).with(url).and_return(nil) + + expect(subject.call(url)).to eq known_account + end + context 'searching for a remote private status' do let(:account) { Fabricate(:account) } let(:poster) { Fabricate(:account, domain: 'example.com') }