From d92fc6e526c90ffb2e2f4536f1e8f931bb450ee4 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 15 Nov 2023 16:30:24 +0300 Subject: [PATCH] Move common query limit method of changesets and notes to mixin --- app/controllers/api/changesets_controller.rb | 18 +++---------- app/controllers/api/notes_controller.rb | 28 +++++++------------- app/controllers/concerns/query_methods.rb | 27 +++++++++++++++++++ 3 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 app/controllers/concerns/query_methods.rb diff --git a/app/controllers/api/changesets_controller.rb b/app/controllers/api/changesets_controller.rb index 9111bb609..77d3d7824 100644 --- a/app/controllers/api/changesets_controller.rb +++ b/app/controllers/api/changesets_controller.rb @@ -2,6 +2,8 @@ module Api class ChangesetsController < ApiController + include QueryMethods + before_action :check_api_writable, :only => [:create, :update, :upload, :subscribe, :unsubscribe] before_action :setup_user_auth, :only => [:show] before_action :authorize, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe] @@ -43,7 +45,7 @@ module Api end # limit the result - changesets = changesets.limit(result_limit) + changesets = query_limit(changesets) # preload users, tags and comments, and render result @changesets = changesets.preload(:user, :changeset_tags, :comments) @@ -403,19 +405,5 @@ module Api changesets.where(:id => ids) end end - - ## - # Get the maximum number of results to return - def result_limit - if params[:limit] - if params[:limit].to_i.positive? && params[:limit].to_i <= Settings.max_changeset_query_limit - params[:limit].to_i - else - raise OSM::APIBadUserInput, "Changeset limit must be between 1 and #{Settings.max_changeset_query_limit}" - end - else - Settings.default_changeset_query_limit - end - end end end diff --git a/app/controllers/api/notes_controller.rb b/app/controllers/api/notes_controller.rb index a0095d954..6f4803191 100644 --- a/app/controllers/api/notes_controller.rb +++ b/app/controllers/api/notes_controller.rb @@ -1,5 +1,7 @@ module Api class NotesController < ApiController + include QueryMethods + before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy] before_action :setup_user_auth, :only => [:create, :show] before_action :authorize, :only => [:close, :reopen, :destroy, :comment] @@ -36,7 +38,9 @@ module Api @max_lat = bbox.max_lat # Find the notes we want to return - @notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments) + notes = notes.bbox(bbox).order("updated_at DESC") + notes = query_limit(notes) + @notes = notes.preload(:comments) # Render the result respond_to do |format| @@ -234,8 +238,9 @@ module Api # Find the comments we want to return @comments = NoteComment.where(:note => notes) - .order(:created_at => :desc).limit(result_limit) - .preload(:author, :note => { :comments => :author }) + .order(:created_at => :desc) + @comments = query_limit(@comments) + @comments = @comments.preload(:author, :note => { :comments => :author }) # Render the result respond_to do |format| @@ -311,7 +316,8 @@ module Api end # Find the notes we want to return - @notes = @notes.distinct.limit(result_limit).preload(:comments) + @notes = query_limit(@notes.distinct) + @notes = @notes.preload(:comments) # Render the result respond_to do |format| @@ -328,20 +334,6 @@ module Api # utility functions below. #------------------------------------------------------------ - ## - # Get the maximum number of results to return - def result_limit - if params[:limit] - if params[:limit].to_i.positive? && params[:limit].to_i <= Settings.max_note_query_limit - params[:limit].to_i - else - raise OSM::APIBadUserInput, "Note limit must be between 1 and #{Settings.max_note_query_limit}" - end - else - Settings.default_note_query_limit - end - end - ## # Generate a condition to choose which notes we want based # on their status and the user's request parameters diff --git a/app/controllers/concerns/query_methods.rb b/app/controllers/concerns/query_methods.rb new file mode 100644 index 000000000..2d0bfee53 --- /dev/null +++ b/app/controllers/concerns/query_methods.rb @@ -0,0 +1,27 @@ +module QueryMethods + extend ActiveSupport::Concern + + private + + ## + # Limit the result according to request parameters and settings + def query_limit(items) + items.limit(query_limit_value) + end + + ## + # Get query limit value from request parameters and settings + def query_limit_value + max_limit = Settings["max_#{controller_name.singularize}_query_limit"] + default_limit = Settings["default_#{controller_name.singularize}_query_limit"] + if params[:limit] + if params[:limit].to_i.positive? && params[:limit].to_i <= max_limit + params[:limit].to_i + else + raise OSM::APIBadUserInput, "#{controller_name.classify} limit must be between 1 and #{max_limit}" + end + else + default_limit + end + end +end -- 2.39.5