Skip to content

Commit

Permalink
Allow pagination Link headers on API accounts/statuses when pinned …
Browse files Browse the repository at this point in the history
…true (mastodon#29442)
  • Loading branch information
mjankowski authored Feb 29, 2024
1 parent edd6aa7 commit eb1b8f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/accounts/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
before_action -> { authorize_if_got_token! :read, :'read:statuses' }
before_action :set_account

after_action :insert_pagination_headers, unless: -> { truthy_param?(:pinned) }
after_action :insert_pagination_headers

def index
cache_if_unauthenticated!
Expand Down
48 changes: 46 additions & 2 deletions spec/controllers/api/v1/accounts/statuses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
get :index, params: { account_id: user.account.id, limit: 1 }

expect(response).to have_http_status(200)
expect(response.headers['Link'].links.size).to eq(2)
expect(links_from_header.size)
.to eq(2)
end

context 'with only media' do
Expand Down Expand Up @@ -55,10 +56,45 @@
Fabricate(:status_pin, account: user.account, status: Fabricate(:status, account: user.account))
end

it 'returns http success' do
it 'returns http success and includes a header link' do
get :index, params: { account_id: user.account.id, pinned: true }

expect(response).to have_http_status(200)
expect(links_from_header.size)
.to eq(1)
expect(links_from_header)
.to contain_exactly(
have_attributes(
href: /pinned=true/,
attr_pairs: contain_exactly(['rel', 'prev'])
)
)
end
end

context 'with enough pinned statuses to paginate' do
before do
stub_const 'Api::BaseController::DEFAULT_STATUSES_LIMIT', 1
2.times { Fabricate(:status_pin, account: user.account) }
end

it 'returns http success and header pagination links to prev and next' do
get :index, params: { account_id: user.account.id, pinned: true }

expect(response).to have_http_status(200)
expect(links_from_header.size)
.to eq(2)
expect(links_from_header)
.to contain_exactly(
have_attributes(
href: /pinned=true/,
attr_pairs: contain_exactly(['rel', 'next'])
),
have_attributes(
href: /pinned=true/,
attr_pairs: contain_exactly(['rel', 'prev'])
)
)
end
end

Expand Down Expand Up @@ -98,4 +134,12 @@
end
end
end

private

def links_from_header
response
.headers['Link']
.links
end
end

0 comments on commit eb1b8f6

Please sign in to comment.