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
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
31 @params = params.permit(:display_name, :bbox, :friends, :nearby, :before, :after, :list)
33 if request.format == :atom && (@params[:before] || @params[:after])
34 redirect_to url_for(@params.merge(:before => nil, :after => nil)), :status => :moved_permanently
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]
46 if (@params[:friends] || @params[:nearby]) && !current_user
51 if request.format == :html && !@params[:list]
53 render :action => :history, :layout => map_layout
55 changesets = conditions_nonempty(Changeset.all)
57 if @params[:display_name]
58 changesets = if user.data_public? || user == current_user
59 changesets.where(:user => user)
61 changesets.where("false")
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)
72 @changesets = get_page_items(changesets, :includes => [:user, :changeset_tags, :comments])
74 render :action => :index, :layout => false
79 # list edits as an atom feed
86 @changeset = Changeset.find(params.expect(:id))
87 case turbo_frame_request_id
88 when "changeset_nodes"
90 render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :elements_count => @nodes_count, :current_page => @current_node_page }
93 render :partial => "elements", :locals => { :type => "way", :elements => @ways, :elements_count => @ways_count, :current_page => @current_way_page }
94 when "changeset_relations"
96 render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :elements_count => @relations_count, :current_page => @current_relation_page }
98 @comments = if current_user&.moderator?
99 @changeset.comments.unscope(:where => :visible).includes(:author)
101 @changeset.comments.includes(:author)
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
111 render :layout => map_layout
113 rescue ActiveRecord::RecordNotFound
114 render :template => "browse/not_found", :status => :not_found, :layout => map_layout
119 #------------------------------------------------------------
120 # utility functions below.
121 #------------------------------------------------------------
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
131 changesets = changesets.where("min_lat < ? and max_lat > ?", db_max_lat, db_min_lat)
133 if max_lon - min_lon >= 360
134 # the query bbox spans the entire world, therefore no lon checks are necessary
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)
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)
152 ((lon + 180) % 360) - 180
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")
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)
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)
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)
189 helper_method def element_pages_count(elements_count)
190 [1, 1 + ((elements_count - 1) / ELEMENTS_PER_PAGE)].max
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 }