1 # frozen_string_literal: true
3 # The ChangesetController is the RESTful interface to Changeset objects
5 class ChangesetsController < ApplicationController
7 include PaginationMethods
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
18 around_action :web_timeout
20 ELEMENTS_PER_PAGE = 20
23 # list non-empty changesets in reverse chronological order
25 param! :before, Integer, :min => 1
26 param! :after, Integer, :min => 1
28 @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
30 if request.format == :atom && (@params[:before] || @params[:after])
31 redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
35 if @params[:display_name]
36 user = User.find_by(:display_name => @params[:display_name])
37 if !user || !user.active?
38 render_unknown_user @params[:display_name]
43 if (@params[:friends] || @params[:nearby]) && !current_user
48 if request.format == :html && !@params[:list]
50 render :action => :history, :layout => map_layout
52 changesets = conditions_nonempty(Changeset.all)
54 if @params[:display_name]
55 changesets = if user.data_public? || user == current_user
56 changesets.where(:user => user)
58 changesets.where("false")
61 bbox_array = @params[:bbox].split(",").map(&:to_f)
62 raise OSM::APIBadUserInput, "The parameter bbox must be of the form min_lon,min_lat,max_lon,max_lat" unless bbox_array.count == 4
64 changesets = conditions_bbox(changesets, *bbox_array)
65 elsif @params[:friends] && current_user
66 changesets = changesets.where(:user => current_user.followings.identifiable)
67 elsif @params[:nearby] && current_user
68 changesets = changesets.where(:user => current_user.nearby)
71 @changesets, @newer_changesets_id, @older_changesets_id = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
73 render :action => :index, :layout => false
78 # list edits as an atom feed
85 @changeset = Changeset.find(params[:id])
86 case turbo_frame_request_id
87 when "changeset_nodes"
89 render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :elements_count => @nodes_count, :current_page => @current_node_page }
92 render :partial => "elements", :locals => { :type => "way", :elements => @ways, :elements_count => @ways_count, :current_page => @current_way_page }
93 when "changeset_relations"
95 render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :elements_count => @relations_count, :current_page => @current_relation_page }
97 @comments = if current_user&.moderator?
98 @changeset.comments.unscope(:where => :visible).includes(:author)
100 @changeset.comments.includes(:author)
105 if @changeset.user.active? && @changeset.user.data_public?
106 changesets = conditions_nonempty(@changeset.user.changesets)
107 @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
108 @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
110 render :layout => map_layout
112 rescue ActiveRecord::RecordNotFound
113 render :template => "browse/not_found", :status => :not_found, :layout => map_layout
118 #------------------------------------------------------------
119 # utility functions below.
120 #------------------------------------------------------------
123 # restrict changesets to those enclosed by a bounding box
124 def conditions_bbox(changesets, min_lon, min_lat, max_lon, max_lat)
125 db_min_lat = (min_lat * GeoRecord::SCALE).to_i
126 db_max_lat = (max_lat * GeoRecord::SCALE).to_i
127 db_min_lon = (wrap_lon(min_lon) * GeoRecord::SCALE).to_i
128 db_max_lon = (wrap_lon(max_lon) * GeoRecord::SCALE).to_i
130 changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
132 if max_lon - min_lon >= 360
133 # the query bbox spans the entire world, therefore no lon checks are necessary
135 elsif db_min_lon <= db_max_lon
136 # the normal case when the query bbox doesn't include the antimeridian
137 changesets.where("min_lon < ? and max_lon > ?", db_max_lon, db_min_lon)
139 # the query bbox includes the antimeridian
140 # this case works as if there are two query bboxes:
141 # [-180*SCALE .. db_max_lon], [db_min_lon .. 180*SCALE]
142 # it would be necessary to check if changeset bboxes intersect with either of the query bboxes:
143 # (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)
144 # but the comparisons with -180*SCALE and 180*SCALE are unnecessary:
145 # (changesets.min_lon < db_max_lon) or (changesets.max_lon > db_min_lon)
146 changesets.where("min_lon < ? or max_lon > ?", db_max_lon, db_min_lon)
151 ((lon + 180) % 360) - 180
155 # eliminate empty changesets (where the bbox has not been set)
156 # this should be applied to all changeset list displays
157 def conditions_nonempty(changesets)
158 changesets.where("num_changes > 0")
162 @nodes_count = @changeset.actual_num_changed_nodes
163 @current_node_page = params[:node_page].to_i.clamp(1, element_pages_count(@nodes_count))
164 @nodes = @changeset.old_nodes
165 .order(:node_id, :version)
166 .offset(ELEMENTS_PER_PAGE * (@current_node_page - 1))
167 .limit(ELEMENTS_PER_PAGE)
171 @ways_count = @changeset.actual_num_changed_ways
172 @current_way_page = params[:way_page].to_i.clamp(1, element_pages_count(@ways_count))
173 @ways = @changeset.old_ways
174 .order(:way_id, :version)
175 .offset(ELEMENTS_PER_PAGE * (@current_way_page - 1))
176 .limit(ELEMENTS_PER_PAGE)
180 @relations_count = @changeset.actual_num_changed_relations
181 @current_relation_page = params[:relation_page].to_i.clamp(1, element_pages_count(@relations_count))
182 @relations = @changeset.old_relations
183 .order(:relation_id, :version)
184 .offset(ELEMENTS_PER_PAGE * (@current_relation_page - 1))
185 .limit(ELEMENTS_PER_PAGE)
188 helper_method def element_pages_count(elements_count)
189 [1, 1 + ((elements_count - 1) / ELEMENTS_PER_PAGE)].max
192 helper_method def element_range_values(elements_count, page)
193 { :x => (ELEMENTS_PER_PAGE * (page - 1)) + 1,
194 :y => [ELEMENTS_PER_PAGE * page, elements_count].min,
195 :count => elements_count }