Add API::Pagination concern (#28826)
				
					
				
			This commit is contained in:
		
							parent
							
								
									828299e71c
								
							
						
					
					
						commit
						1d3ecd3fba
					
				| @ -9,6 +9,7 @@ class Api::BaseController < ApplicationController | |||||||
|   include Api::CachingConcern |   include Api::CachingConcern | ||||||
|   include Api::ContentSecurityPolicy |   include Api::ContentSecurityPolicy | ||||||
|   include Api::ErrorHandling |   include Api::ErrorHandling | ||||||
|  |   include Api::Pagination | ||||||
| 
 | 
 | ||||||
|   skip_before_action :require_functional!, unless: :limited_federation_mode? |   skip_before_action :require_functional!, unless: :limited_federation_mode? | ||||||
| 
 | 
 | ||||||
| @ -29,21 +30,6 @@ class Api::BaseController < ApplicationController | |||||||
| 
 | 
 | ||||||
|   protected |   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) |   def limit_param(default_limit) | ||||||
|     return default_limit unless params[: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? |     render json: { error: 'Your login is currently disabled' }, status: 403 if current_user&.account&.unavailable? | ||||||
|   end |   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! |   def require_user! | ||||||
|     if !current_user |     if !current_user | ||||||
|       render json: { error: 'This method requires an authenticated user' }, status: 422 |       render json: { error: 'This method requires an authenticated user' }, status: 422 | ||||||
| @ -104,14 +86,6 @@ class Api::BaseController < ApplicationController | |||||||
| 
 | 
 | ||||||
|   private |   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) |   def respond_with_error(code) | ||||||
|     render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code |     render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code | ||||||
|   end |   end | ||||||
|  | |||||||
							
								
								
									
										36
									
								
								app/controllers/concerns/api/pagination.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								app/controllers/concerns/api/pagination.rb
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user