Skip to content

Commit

Permalink
Fix processing of compacted single-item JSON-LD collections (mastodon…
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored Jan 19, 2024
1 parent 3593ee2 commit cf2a2ed
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/services/activitypub/fetch_featured_collection_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def collection_items(collection)

case collection['type']
when 'Collection', 'CollectionPage'
collection['items']
as_array(collection['items'])
when 'OrderedCollection', 'OrderedCollectionPage'
collection['orderedItems']
as_array(collection['orderedItems'])
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/services/activitypub/fetch_replies_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def collection_items(collection_or_uri)

case collection['type']
when 'Collection', 'CollectionPage'
collection['items']
as_array(collection['items'])
when 'OrderedCollection', 'OrderedCollectionPage'
collection['orderedItems']
as_array(collection['orderedItems'])
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/services/activitypub/synchronize_followers_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def collection_items(collection_or_uri)

case collection['type']
when 'Collection', 'CollectionPage'
collection['items']
as_array(collection['items'])
when 'OrderedCollection', 'OrderedCollectionPage'
collection['orderedItems']
as_array(collection['orderedItems'])
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/keys/query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def query_remote_devices!

return if json['items'].blank?

@devices = json['items'].map do |device|
@devices = as_array(json['items']).map do |device|
Device.new(device_id: device['id'], name: device['name'], identity_key: device.dig('identityKey', 'publicKeyBase64'), fingerprint_key: device.dig('fingerprintKey', 'publicKeyBase64'), claim_url: device['claim'])
end
rescue HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::Error => e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
end

let(:status_json_pinned_unknown_unreachable) do
let(:status_json_pinned_unknown_reachable) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Note',
Expand Down Expand Up @@ -75,7 +75,7 @@
stub_request(:get, 'https://example.com/account/pinned/known').to_return(status: 200, body: Oj.dump(status_json_pinned_known))
stub_request(:get, 'https://example.com/account/pinned/unknown-inlined').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_inlined))
stub_request(:get, 'https://example.com/account/pinned/unknown-unreachable').to_return(status: 404)
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_unreachable))
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_reachable))
stub_request(:get, 'https://example.com/account/collections/featured').to_return(status: 200, body: Oj.dump(featured_with_null))

subject.call(actor, note: true, hashtag: false)
Expand Down Expand Up @@ -115,6 +115,21 @@
end

it_behaves_like 'sets pinned posts'

context 'when there is a single item, with the array compacted away' do
let(:items) { 'https://example.com/account/pinned/unknown-reachable' }

before do
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_reachable))
subject.call(actor, note: true, hashtag: false)
end

it 'sets expected posts as pinned posts' do
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
'https://example.com/account/pinned/unknown-reachable'
)
end
end
end

context 'when the endpoint is a paginated Collection' do
Expand All @@ -136,6 +151,21 @@
end

it_behaves_like 'sets pinned posts'

context 'when there is a single item, with the array compacted away' do
let(:items) { 'https://example.com/account/pinned/unknown-reachable' }

before do
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_reachable))
subject.call(actor, note: true, hashtag: false)
end

it 'sets expected posts as pinned posts' do
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
'https://example.com/account/pinned/unknown-reachable'
)
end
end
end
end
end
12 changes: 12 additions & 0 deletions spec/services/activitypub/fetch_replies_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@

describe '#call' do
context 'when the payload is a Collection with inlined replies' do
context 'when there is a single reply, with the array compacted away' do
let(:items) { 'http://example.com/self-reply-1' }

it 'queues the expected worker' do
allow(FetchReplyWorker).to receive(:push_bulk)

subject.call(status, payload)

expect(FetchReplyWorker).to have_received(:push_bulk).with(['http://example.com/self-reply-1'])
end
end

context 'when passing the collection itself' do
it 'spawns workers for up to 5 replies on the same server' do
allow(FetchReplyWorker).to receive(:push_bulk)
Expand Down

0 comments on commit cf2a2ed

Please sign in to comment.