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, :pages => @node_pages }
90 render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages }
91 when "changeset_relations"
93 render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages }
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 @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :order => [:node_id, :version], :per_page => ELEMENTS_PER_PAGE, :parameter => "node_page")
164 @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :order => [:way_id, :version], :per_page => ELEMENTS_PER_PAGE, :parameter => "way_page")
168 @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :order => [:relation_id, :version], :per_page => ELEMENTS_PER_PAGE, :parameter => "relation_page")