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