]> git.openstreetmap.org Git - rails.git/blob - app/controllers/concerns/query_methods.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / app / controllers / concerns / query_methods.rb
1 # frozen_string_literal: true
2
3 module QueryMethods
4   extend ActiveSupport::Concern
5
6   private
7
8   ##
9   # Filter the resulting items by user
10   def query_conditions_user(items, filter_property)
11     user = query_conditions_user_value
12     items = items.where(filter_property => user) if user
13     items
14   end
15
16   ##
17   # Get user value for query filtering by user
18   # Raises OSM::APIBadUserInput if user not found like notes api does, changesets api raises OSM::APINotFoundError instead
19   def query_conditions_user_value
20     if params[:display_name] || params[:user]
21       if params[:display_name]
22         user = User.find_by(:display_name => params[:display_name])
23
24         raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless user
25       else
26         user = User.find_by(:id => params[:user])
27
28         raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless user
29       end
30
31       user
32     end
33   end
34
35   ##
36   # Restrict the resulting items to those created during a particular time period
37   # Using 'to' requires specifying 'from' as well for historical reasons
38   def query_conditions_time(items, filter_property = :created_at)
39     interval = query_conditions_time_value
40
41     if interval
42       items.where(filter_property => interval)
43     else
44       items
45     end
46   end
47
48   ##
49   # Get query time interval from request parameters or nil
50   def query_conditions_time_value
51     if params[:from]
52       begin
53         from = Time.parse(params[:from]).utc
54       rescue ArgumentError
55         raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
56       end
57
58       begin
59         to = if params[:to]
60                Time.parse(params[:to]).utc
61              else
62                Time.now.utc
63              end
64       rescue ArgumentError
65         raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format"
66       end
67
68       from..to
69     end
70   end
71
72   ##
73   # Limit the result according to request parameters and settings
74   def query_limit(items)
75     items.limit(query_limit_value)
76   end
77
78   ##
79   # Get query limit value from request parameters and settings
80   def query_limit_value
81     name = controller_path.sub(%r{^api/}, "").tr("/", "_").singularize
82     max_limit = Settings["max_#{name}_query_limit"]
83     default_limit = Settings["default_#{name}_query_limit"]
84     if params[:limit]
85       if params[:limit].to_i.positive? && params[:limit].to_i <= max_limit
86         params[:limit].to_i
87       else
88         raise OSM::APIBadUserInput, "#{controller_name.classify} limit must be between 1 and #{max_limit}"
89       end
90     else
91       default_limit
92     end
93   end
94 end