]> git.openstreetmap.org Git - rails.git/blob - app/controllers/changesets_controller.rb
Validate the bbox parameter when listing changesets
[rails.git] / app / controllers / changesets_controller.rb
1 # frozen_string_literal: true
2
3 # The ChangesetController is the RESTful interface to Changeset objects
4
5 class ChangesetsController < ApplicationController
6   include UserMethods
7   include PaginationMethods
8
9   layout :site_layout
10
11   before_action :authorize_web
12   before_action :set_locale
13   before_action -> { check_database_readable(:need_api => true) }
14   before_action :require_oauth, :only => :show
15
16   authorize_resource
17
18   around_action :web_timeout
19
20   ELEMENTS_PER_PAGE = 20
21
22   ##
23   # list non-empty changesets in reverse chronological order
24   def index
25     param! :before, Integer, :min => 1
26     param! :after, Integer, :min => 1
27     param! :bbox, String, :custom => lambda { |bbox|
28       raise RailsParam::InvalidParameterError, "bbox must be of the form min_lon,min_lat,max_lon,max_lat" unless bbox.count(",") == 3
29     }
30
31     @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
32
33     if request.format == :atom && (@params[:before] || @params[:after])
34       redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
35       return
36     end
37
38     if @params[:display_name]
39       user = User.find_by(:display_name => @params[:display_name])
40       if !user || !user.active?
41         render_unknown_user @params[:display_name]
42         return
43       end
44     end
45
46     if (@params[:friends] || @params[:nearby]) && !current_user
47       require_user
48       return
49     end
50
51     if request.format == :html && !@params[:list]
52       require_oauth
53       render :action => :history, :layout => map_layout
54     else
55       changesets = conditions_nonempty(Changeset.all)
56
57       if @params[:display_name]
58         changesets = if user.data_public? || user == current_user
59                        changesets.where(:user => user)
60                      else
61                        changesets.where("false")
62                      end
63       elsif @params[:bbox]
64         bbox_array = @params[:bbox].split(",").map(&:to_f)
65         changesets = conditions_bbox(changesets, *bbox_array)
66       elsif @params[:friends] && current_user
67         changesets = changesets.where(:user => current_user.followings.identifiable)
68       elsif @params[:nearby] && current_user
69         changesets = changesets.where(:user => current_user.nearby)
70       end
71
72       @changesets = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
73
74       render :action => :index, :layout => false
75     end
76   end
77
78   ##
79   # list edits as an atom feed
80   def feed
81     index
82   end
83
84   def show
85     @type = "changeset"
86     @changeset = Changeset.find(params.expect(:id))
87     case turbo_frame_request_id
88     when "changeset_nodes"
89       load_nodes
90       render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :elements_count => @nodes_count, :current_page => @current_node_page }
91     when "changeset_ways"
92       load_ways
93       render :partial => "elements", :locals => { :type => "way", :elements => @ways, :elements_count => @ways_count, :current_page => @current_way_page }
94     when "changeset_relations"
95       load_relations
96       render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :elements_count => @relations_count, :current_page => @current_relation_page }
97     else
98       @comments = if current_user&.moderator?
99                     @changeset.comments.unscope(:where => :visible).includes(:author)
100                   else
101                     @changeset.comments.includes(:author)
102                   end
103       load_nodes
104       load_ways
105       load_relations
106       if @changeset.user.active? && @changeset.user.data_public?
107         changesets = conditions_nonempty(@changeset.user.changesets)
108         @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
109         @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
110       end
111       render :layout => map_layout
112     end
113   rescue ActiveRecord::RecordNotFound
114     render :template => "browse/not_found", :status => :not_found, :layout => map_layout
115   end
116
117   private
118
119   #------------------------------------------------------------
120   # utility functions below.
121   #------------------------------------------------------------
122
123   ##
124   # restrict changesets to those enclosed by a bounding box
125   def conditions_bbox(changesets, min_lon, min_lat, max_lon, max_lat)
126     db_min_lat = (min_lat * GeoRecord::SCALE).to_i
127     db_max_lat = (max_lat * GeoRecord::SCALE).to_i
128     db_min_lon = (wrap_lon(min_lon) * GeoRecord::SCALE).to_i
129     db_max_lon = (wrap_lon(max_lon) * GeoRecord::SCALE).to_i
130
131     changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
132
133     if max_lon - min_lon >= 360
134       # the query bbox spans the entire world, therefore no lon checks are necessary
135       changesets
136     elsif db_min_lon <= db_max_lon
137       # the normal case when the query bbox doesn't include the antimeridian
138       changesets.where("min_lon < ? and max_lon > ?", db_max_lon, db_min_lon)
139     else
140       # the query bbox includes the antimeridian
141       # this case works as if there are two query bboxes:
142       #   [-180*SCALE .. db_max_lon], [db_min_lon .. 180*SCALE]
143       # it would be necessary to check if changeset bboxes intersect with either of the query bboxes:
144       #   (changesets.min_lon < db_max_lon and changesets.max_lon > -180*SCALE) or (changesets.min_lon < 180*SCALE and changesets.max_lon > db_min_lon)
145       # but the comparisons with -180*SCALE and 180*SCALE are unnecessary:
146       #   (changesets.min_lon < db_max_lon) or (changesets.max_lon > db_min_lon)
147       changesets.where("min_lon < ? or max_lon > ?", db_max_lon, db_min_lon)
148     end
149   end
150
151   def wrap_lon(lon)
152     ((lon + 180) % 360) - 180
153   end
154
155   ##
156   # eliminate empty changesets (where the bbox has not been set)
157   # this should be applied to all changeset list displays
158   def conditions_nonempty(changesets)
159     changesets.where("num_changes > 0")
160   end
161
162   def load_nodes
163     @nodes_count = @changeset.actual_num_changed_nodes
164     @current_node_page = params.fetch(:node_page, "1").to_i.clamp(1, element_pages_count(@nodes_count))
165     @nodes = @changeset.old_nodes
166                        .order(:node_id, :version)
167                        .offset(ELEMENTS_PER_PAGE * (@current_node_page - 1))
168                        .limit(ELEMENTS_PER_PAGE)
169   end
170
171   def load_ways
172     @ways_count = @changeset.actual_num_changed_ways
173     @current_way_page = params.fetch(:way_page, "1").to_i.clamp(1, element_pages_count(@ways_count))
174     @ways = @changeset.old_ways
175                       .order(:way_id, :version)
176                       .offset(ELEMENTS_PER_PAGE * (@current_way_page - 1))
177                       .limit(ELEMENTS_PER_PAGE)
178   end
179
180   def load_relations
181     @relations_count = @changeset.actual_num_changed_relations
182     @current_relation_page = params.fetch(:relation_page, "1").to_i.clamp(1, element_pages_count(@relations_count))
183     @relations = @changeset.old_relations
184                            .order(:relation_id, :version)
185                            .offset(ELEMENTS_PER_PAGE * (@current_relation_page - 1))
186                            .limit(ELEMENTS_PER_PAGE)
187   end
188
189   helper_method def element_pages_count(elements_count)
190     [1, 1 + ((elements_count - 1) / ELEMENTS_PER_PAGE)].max
191   end
192
193   helper_method def element_range_values(elements_count, page)
194     { :x => (ELEMENTS_PER_PAGE * (page - 1)) + 1,
195       :y => [ELEMENTS_PER_PAGE * page, elements_count].min,
196       :count => elements_count }
197   end
198 end