1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetsController < ApplicationController
5 include PaginationMethods
9 before_action :authorize_web
10 before_action :set_locale
11 before_action -> { check_database_readable(:need_api => true) }
12 before_action :require_oauth, :only => :show
16 around_action :web_timeout
18 ELEMENTS_PER_PAGE = 20
21 # list non-empty changesets in reverse chronological order
23 param! :before, Integer, :min => 1
24 param! :after, Integer, :min => 1
26 @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
28 if request.format == :atom && (@params[:before] || @params[:after])
29 redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
33 if @params[:display_name]
34 user = User.find_by(:display_name => @params[:display_name])
35 if !user || !user.active?
36 render_unknown_user @params[:display_name]
41 if (@params[:friends] || @params[:nearby]) && !current_user
46 if request.format == :html && !@params[:list]
48 render :action => :history, :layout => map_layout
50 changesets = conditions_nonempty(Changeset.all)
52 if @params[:display_name]
53 changesets = if user.data_public? || user == current_user
54 changesets.where(:user => user)
56 changesets.where("false")
59 bbox_array = @params[:bbox].split(",").map(&:to_f)
60 raise OSM::APIBadUserInput, "The parameter bbox must be of the form min_lon,min_lat,max_lon,max_lat" unless bbox_array.count == 4
62 changesets = conditions_bbox(changesets, *bbox_array)
63 elsif @params[:friends] && current_user
64 changesets = changesets.where(:user => current_user.followings.identifiable)
65 elsif @params[:nearby] && current_user
66 changesets = changesets.where(:user => current_user.nearby)
69 @changesets, @newer_changesets_id, @older_changesets_id = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
71 render :action => :index, :layout => false
76 # list edits as an atom feed
83 @changeset = Changeset.find(params[:id])
84 case turbo_frame_request_id
85 when "changeset_nodes"
87 render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :elements_count => @nodes_count, :current_page => @current_node_page }
90 render :partial => "elements", :locals => { :type => "way", :elements => @ways, :elements_count => @ways_count, :current_page => @current_way_page }
91 when "changeset_relations"
93 render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :elements_count => @relations_count, :current_page => @current_relation_page }
95 @comments = if current_user&.moderator?
96 @changeset.comments.unscope(:where => :visible).includes(:author)
98 @changeset.comments.includes(:author)
103 if @changeset.user.active? && @changeset.user.data_public?
104 changesets = conditions_nonempty(@changeset.user.changesets)
105 @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
106 @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
108 render :layout => map_layout
110 rescue ActiveRecord::RecordNotFound
111 render :template => "browse/not_found", :status => :not_found, :layout => map_layout
116 #------------------------------------------------------------
117 # utility functions below.
118 #------------------------------------------------------------
121 # restrict changesets to those enclosed by a bounding box
122 def conditions_bbox(changesets, min_lon, min_lat, max_lon, max_lat)
123 db_min_lat = (min_lat * GeoRecord::SCALE).to_i
124 db_max_lat = (max_lat * GeoRecord::SCALE).to_i
125 db_min_lon = (wrap_lon(min_lon) * GeoRecord::SCALE).to_i
126 db_max_lon = (wrap_lon(max_lon) * GeoRecord::SCALE).to_i
128 changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
130 if max_lon - min_lon >= 360
131 # the query bbox spans the entire world, therefore no lon checks are necessary
133 elsif db_min_lon <= db_max_lon
134 # the normal case when the query bbox doesn't include the antimeridian
135 changesets.where("min_lon < ? and max_lon > ?", db_max_lon, db_min_lon)
137 # the query bbox includes the antimeridian
138 # this case works as if there are two query bboxes:
139 # [-180*SCALE .. db_max_lon], [db_min_lon .. 180*SCALE]
140 # it would be necessary to check if changeset bboxes intersect with either of the query bboxes:
141 # (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)
142 # but the comparisons with -180*SCALE and 180*SCALE are unnecessary:
143 # (changesets.min_lon < db_max_lon) or (changesets.max_lon > db_min_lon)
144 changesets.where("min_lon < ? or max_lon > ?", db_max_lon, db_min_lon)
149 ((lon + 180) % 360) - 180
153 # eliminate empty changesets (where the bbox has not been set)
154 # this should be applied to all changeset list displays
155 def conditions_nonempty(changesets)
156 changesets.where("num_changes > 0")
160 @nodes_count = @changeset.old_nodes.count
161 @current_node_page = params[:node_page].to_i.clamp(1, element_pages_count(@nodes_count))
162 @nodes = @changeset.old_nodes
163 .order(:node_id, :version)
164 .offset(ELEMENTS_PER_PAGE * (@current_node_page - 1))
165 .limit(ELEMENTS_PER_PAGE)
169 @ways_count = @changeset.old_ways.count
170 @current_way_page = params[:way_page].to_i.clamp(1, element_pages_count(@ways_count))
171 @ways = @changeset.old_ways
172 .order(:way_id, :version)
173 .offset(ELEMENTS_PER_PAGE * (@current_way_page - 1))
174 .limit(ELEMENTS_PER_PAGE)
178 @relations_count = @changeset.old_relations.count
179 @current_relation_page = params[:relation_page].to_i.clamp(1, element_pages_count(@relations_count))
180 @relations = @changeset.old_relations
181 .order(:relation_id, :version)
182 .offset(ELEMENTS_PER_PAGE * (@current_relation_page - 1))
183 .limit(ELEMENTS_PER_PAGE)
186 helper_method def element_pages_count(elements_count)
187 [1, 1 + ((elements_count - 1) / ELEMENTS_PER_PAGE)].max
190 helper_method def element_range_values(elements_count, page)
191 { :x => (ELEMENTS_PER_PAGE * (page - 1)) + 1,
192 :y => [ELEMENTS_PER_PAGE * page, elements_count].min,
193 :count => elements_count }