diff --git a/app/lib/status_cache_hydrator.rb b/app/lib/status_cache_hydrator.rb index a06e070c3d..674945c403 100644 --- a/app/lib/status_cache_hydrator.rb +++ b/app/lib/status_cache_hydrator.rb @@ -75,25 +75,29 @@ class StatusCacheHydrator end def hydrate_quote_payload(empty_payload, quote, account_id, nested: false) - empty_payload.tap do |payload| - # Nothing to do if we're in the shallow (depth limit) case - next unless payload.key?(:quoted_status) + return unless quote&.acceptable? + empty_payload.tap do |payload| payload.delete(:quoted_status) if nested # TODO: performance improvements - if quote&.quoted_status.nil? - payload[nested ? :quoted_status_id : :quoted_status] = nil - payload[:state] = 'deleted' - elsif StatusFilter.new(quote.quoted_status, Account.find_by(id: account_id)).filtered? - payload[nested ? :quoted_status_id : :quoted_status] = nil - payload[:state] = 'unauthorized' - elsif payload[:state] == 'accepted' - if nested - payload[:quoted_status_id] = quote.quoted_status_id&.to_s + if quote.accepted? + if quote.quoted_status.nil? + payload[nested ? :quoted_status_id : :quoted_status] = nil + payload[:state] = 'deleted' + elsif StatusFilter.new(quote.quoted_status, Account.find_by(id: account_id)).filtered? + payload[nested ? :quoted_status_id : :quoted_status] = nil + payload[:state] = 'unauthorized' else - payload[:quoted_status] = StatusCacheHydrator.new(quote.quoted_status).hydrate(account_id, nested: true) + payload[:state] = 'accepted' + if nested + payload[:quoted_status_id] = quote.quoted_status_id&.to_s + else + payload[:quoted_status] = StatusCacheHydrator.new(quote.quoted_status).hydrate(account_id, nested: true) + end end + else + payload[nested ? :quoted_status_id : :quoted_status] = nil end end end diff --git a/spec/lib/status_cache_hydrator_spec.rb b/spec/lib/status_cache_hydrator_spec.rb index 073caec0fc..e56393da1d 100644 --- a/spec/lib/status_cache_hydrator_spec.rb +++ b/spec/lib/status_cache_hydrator_spec.rb @@ -56,9 +56,10 @@ RSpec.describe StatusCacheHydrator do context 'when handling an approved quote' do let(:quoted_status) { Fabricate(:status) } + let(:legacy) { false } before do - Fabricate(:quote, status: status, quoted_status: quoted_status, state: :accepted) + Fabricate(:quote, status: status, quoted_status: quoted_status, state: :accepted, legacy: legacy) end it 'renders the same attributes as full render' do @@ -75,13 +76,46 @@ RSpec.describe StatusCacheHydrator do end end + context 'when the quote post is a legacy quote' do + let(:legacy) { true } + + it 'renders the same attributes as full render' do + expect(subject).to eql(compare_to_hash) + expect(subject[:quote]).to_not be_nil + end + end + + context 'when the quoted post is a private post the viewer is not authorized to see' do + let(:quoted_status) { Fabricate(:status, account: status.account, visibility: :private) } + + it 'renders the same attributes as full render' do + expect(subject).to eql(compare_to_hash) + expect(subject[:quote]).to_not be_nil + expect(subject[:quote][:quoted_status]).to be_nil + end + end + + context 'when the quoted post is a private post the viewer is authorized to see' do + let(:quoted_status) { Fabricate(:status, account: status.account, visibility: :private) } + + before do + account.follow!(quoted_status.account) + end + + it 'renders the same attributes as full render' do + expect(subject).to eql(compare_to_hash) + expect(subject[:quote]).to_not be_nil + expect(subject[:quote][:quoted_status]).to_not be_nil + end + end + context 'when the quoted post has been deleted' do let(:quoted_status) { nil } it 'returns the same attributes as full render' do expect(subject).to eql(compare_to_hash) expect(subject[:quote]).to_not be_nil - expect(subject[:quote_status]).to be_nil + expect(subject[:quote][:quoted_status]).to be_nil end end @@ -93,7 +127,7 @@ RSpec.describe StatusCacheHydrator do it 'returns the same attributes as full render' do expect(subject).to eql(compare_to_hash) expect(subject[:quote]).to_not be_nil - expect(subject[:quote_status]).to be_nil + expect(subject[:quote][:quoted_status]).to be_nil end end