1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetsController < ApplicationController
8 before_action :authorize_web
9 before_action :set_locale
10 before_action -> { check_database_readable(:need_api => true) }
11 before_action :require_oauth, :only => :show
15 around_action :web_timeout
18 # list non-empty changesets in reverse chronological order
20 param! :max_id, Integer, :min => 1
22 @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
24 if request.format == :atom && @params[:max_id]
25 redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
29 if @params[:display_name]
30 user = User.find_by(:display_name => @params[:display_name])
31 if !user || !user.active?
32 render_unknown_user @params[:display_name]
37 if (@params[:friends] || @params[:nearby]) && !current_user
42 if request.format == :html && !@params[:list]
44 render :action => :history, :layout => map_layout
46 changesets = conditions_nonempty(Changeset.all)
48 if @params[:display_name]
49 changesets = if user.data_public? || user == current_user
50 changesets.where(:user => user)
52 changesets.where("false")
55 changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
56 elsif @params[:friends] && current_user
57 changesets = changesets.where(:user => current_user.followings.identifiable)
58 elsif @params[:nearby] && current_user
59 changesets = changesets.where(:user => current_user.nearby)
62 changesets = changesets.where(:changesets => { :id => ..@params[:max_id] }) if @params[:max_id]
64 @changesets = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
66 render :action => :index, :layout => false
71 # list edits as an atom feed
78 @changeset = Changeset.find(params[:id])
79 case turbo_frame_request_id
80 when "changeset_nodes"
81 @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :order => [:node_id, :version], :per_page => 20, :parameter => "node_page")
82 render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :pages => @node_pages }
84 @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :order => [:way_id, :version], :per_page => 20, :parameter => "way_page")
85 render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages }
86 when "changeset_relations"
87 @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :order => [:relation_id, :version], :per_page => 20, :parameter => "relation_page")
88 render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages }
90 @comments = if current_user&.moderator?
91 @changeset.comments.unscope(:where => :visible).includes(:author)
93 @changeset.comments.includes(:author)
95 @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :order => [:node_id, :version], :per_page => 20, :parameter => "node_page")
96 @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :order => [:way_id, :version], :per_page => 20, :parameter => "way_page")
97 @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :order => [:relation_id, :version], :per_page => 20, :parameter => "relation_page")
98 if @changeset.user.active? && @changeset.user.data_public?
99 changesets = conditions_nonempty(@changeset.user.changesets)
100 @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
101 @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
103 render :layout => map_layout
105 rescue ActiveRecord::RecordNotFound
106 render :template => "browse/not_found", :status => :not_found, :layout => map_layout
111 #------------------------------------------------------------
112 # utility functions below.
113 #------------------------------------------------------------
116 # if a bounding box was specified do some sanity checks.
117 # restrict changesets to those enclosed by a bounding box
118 def conditions_bbox(changesets, bbox)
120 bbox.check_boundaries
121 bbox = bbox.to_scaled
123 changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
124 bbox.max_lon.to_i, bbox.min_lon.to_i,
125 bbox.max_lat.to_i, bbox.min_lat.to_i)
132 # eliminate empty changesets (where the bbox has not been set)
133 # this should be applied to all changeset list displays
134 def conditions_nonempty(changesets)
135 changesets.where("num_changes > 0")