From 1d3ecd3fbaf15450890c1ece7955846fba82a58d Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 17 Apr 2024 05:22:45 -0400 Subject: [PATCH] Add `API::Pagination` concern (#28826) --- app/controllers/api/base_controller.rb | 28 +---------------- app/controllers/concerns/api/pagination.rb | 36 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 app/controllers/concerns/api/pagination.rb diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index f87d596ce3..c1a5e43f88 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -9,6 +9,7 @@ class Api::BaseController < ApplicationController include Api::CachingConcern include Api::ContentSecurityPolicy include Api::ErrorHandling + include Api::Pagination skip_before_action :require_functional!, unless: :limited_federation_mode? @@ -29,21 +30,6 @@ class Api::BaseController < ApplicationController protected - def pagination_max_id - pagination_collection.last.id - end - - def pagination_since_id - pagination_collection.first.id - end - - def set_pagination_headers(next_path = nil, prev_path = nil) - links = [] - links << [next_path, [%w(rel next)]] if next_path - links << [prev_path, [%w(rel prev)]] if prev_path - response.headers['Link'] = LinkHeader.new(links) unless links.empty? - end - def limit_param(default_limit) return default_limit unless params[:limit] @@ -72,10 +58,6 @@ class Api::BaseController < ApplicationController render json: { error: 'Your login is currently disabled' }, status: 403 if current_user&.account&.unavailable? end - def require_valid_pagination_options! - render json: { error: 'Pagination values for `offset` and `limit` must be positive' }, status: 400 if pagination_options_invalid? - end - def require_user! if !current_user render json: { error: 'This method requires an authenticated user' }, status: 422 @@ -104,14 +86,6 @@ class Api::BaseController < ApplicationController private - def insert_pagination_headers - set_pagination_headers(next_path, prev_path) - end - - def pagination_options_invalid? - params.slice(:limit, :offset).values.map(&:to_i).any?(&:negative?) - end - def respond_with_error(code) render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code end diff --git a/app/controllers/concerns/api/pagination.rb b/app/controllers/concerns/api/pagination.rb new file mode 100644 index 0000000000..d84a1d99f7 --- /dev/null +++ b/app/controllers/concerns/api/pagination.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Api::Pagination + extend ActiveSupport::Concern + + protected + + def pagination_max_id + pagination_collection.last.id + end + + def pagination_since_id + pagination_collection.first.id + end + + def set_pagination_headers(next_path = nil, prev_path = nil) + links = [] + links << [next_path, [%w(rel next)]] if next_path + links << [prev_path, [%w(rel prev)]] if prev_path + response.headers['Link'] = LinkHeader.new(links) unless links.empty? + end + + def require_valid_pagination_options! + render json: { error: 'Pagination values for `offset` and `limit` must be positive' }, status: 400 if pagination_options_invalid? + end + + private + + def insert_pagination_headers + set_pagination_headers(next_path, prev_path) + end + + def pagination_options_invalid? + params.slice(:limit, :offset).values.map(&:to_i).any?(&:negative?) + end +end