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) }, :only => [:index, :feed, :show]
11 before_action :require_oauth, :only => :show
12 before_action :check_database_writable, :only => [:subscribe, :unsubscribe]
16 around_action :web_timeout
19 # list non-empty changesets in reverse chronological order
21 param! :max_id, Integer, :min => 1
23 @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
25 if request.format == :atom && @params[:max_id]
26 redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
30 if @params[:display_name]
31 user = User.find_by(:display_name => @params[:display_name])
32 if !user || !user.active?
33 render_unknown_user @params[:display_name]
38 if (@params[:friends] || @params[:nearby]) && !current_user
43 if request.format == :html && !@params[:list]
45 render :action => :history, :layout => map_layout
47 changesets = conditions_nonempty(Changeset.all)
49 if @params[:display_name]
50 changesets = if user.data_public? || user == current_user
51 changesets.where(:user => user)
53 changesets.where("false")
56 changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
57 elsif @params[:friends] && current_user
58 changesets = changesets.where(:user => current_user.friends.identifiable)
59 elsif @params[:nearby] && current_user
60 changesets = changesets.where(:user => current_user.nearby)
63 changesets = changesets.where(:changesets => { :id => ..@params[:max_id] }) if @params[:max_id]
65 @changesets = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
67 render :action => :index, :layout => false
72 # list edits as an atom feed
79 @changeset = Changeset.find(params[:id])
80 case turbo_frame_request_id
81 when "changeset_nodes"
82 @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "node_page")
83 render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :pages => @node_pages }
85 @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "way_page")
86 render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages }
87 when "changeset_relations"
88 @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "relation_page")
89 render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages }
91 @comments = if current_user&.moderator?
92 @changeset.comments.unscope(:where => :visible).includes(:author)
94 @changeset.comments.includes(:author)
96 @node_pages, @nodes = paginate(:old_nodes, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "node_page")
97 @way_pages, @ways = paginate(:old_ways, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "way_page")
98 @relation_pages, @relations = paginate(:old_relations, :conditions => { :changeset_id => @changeset.id }, :per_page => 20, :parameter => "relation_page")
99 if @changeset.user.active? && @changeset.user.data_public?
100 changesets = conditions_nonempty(@changeset.user.changesets)
101 @next_by_user = changesets.where("id > ?", @changeset.id).reorder(:id => :asc).first
102 @prev_by_user = changesets.where(:id => ...@changeset.id).reorder(:id => :desc).first
104 render :layout => map_layout
106 rescue ActiveRecord::RecordNotFound
107 render :template => "browse/not_found", :status => :not_found, :layout => map_layout
111 # subscribe to a changeset
113 @changeset = Changeset.find(params[:id])
116 @changeset.subscribe(current_user) unless @changeset.subscribed?(current_user)
118 redirect_to changeset_path(@changeset)
120 rescue ActiveRecord::RecordNotFound
121 render :action => "no_such_entry", :status => :not_found
125 # unsubscribe from a changeset
127 @changeset = Changeset.find(params[:id])
130 @changeset.unsubscribe(current_user)
132 redirect_to changeset_path(@changeset)
134 rescue ActiveRecord::RecordNotFound
135 render :action => "no_such_entry", :status => :not_found
140 #------------------------------------------------------------
141 # utility functions below.
142 #------------------------------------------------------------
145 # if a bounding box was specified do some sanity checks.
146 # restrict changesets to those enclosed by a bounding box
147 def conditions_bbox(changesets, bbox)
149 bbox.check_boundaries
150 bbox = bbox.to_scaled
152 changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
153 bbox.max_lon.to_i, bbox.min_lon.to_i,
154 bbox.max_lat.to_i, bbox.min_lat.to_i)
161 # eliminate empty changesets (where the bbox has not been set)
162 # this should be applied to all changeset list displays
163 def conditions_nonempty(changesets)
164 changesets.where("num_changes > 0")