1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetsController < ApplicationController
9 before_action :authorize_web
10 before_action :set_locale
11 before_action -> { check_database_readable(:need_api => true) }, :only => [:index, :feed]
12 before_action :check_database_writable, :only => [:subscribe, :unsubscribe]
16 around_action :web_timeout
18 # Helper methods for checking consistency
19 include ConsistencyValidations
22 # list non-empty changesets in reverse chronological order
24 @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
26 if request.format == :atom && @params[:max_id]
27 redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
31 if @params[:display_name]
32 user = User.find_by(:display_name => @params[:display_name])
33 if !user || !user.active?
34 render_unknown_user @params[:display_name]
39 if (@params[:friends] || @params[:nearby]) && !current_user
44 if request.format == :html && !@params[:list]
46 render :action => :history, :layout => map_layout
48 changesets = conditions_nonempty(Changeset.all)
50 if @params[:display_name]
51 changesets = if user.data_public? || user == current_user
52 changesets.where(:user => user)
54 changesets.where("false")
57 changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
58 elsif @params[:friends] && current_user
59 changesets = changesets.where(:user => current_user.friends.identifiable)
60 elsif @params[:nearby] && current_user
61 changesets = changesets.where(:user => current_user.nearby)
64 changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
66 @changesets = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
68 render :action => :index, :layout => false
73 # list edits as an atom feed
79 # subscribe to a changeset
81 @changeset = Changeset.find(params[:id])
84 @changeset.subscribe(current_user) unless @changeset.subscribed?(current_user)
86 redirect_to changeset_path(@changeset)
88 rescue ActiveRecord::RecordNotFound
89 render :action => "no_such_entry", :status => :not_found
93 # unsubscribe from a changeset
95 @changeset = Changeset.find(params[:id])
98 @changeset.unsubscribe(current_user)
100 redirect_to changeset_path(@changeset)
102 rescue ActiveRecord::RecordNotFound
103 render :action => "no_such_entry", :status => :not_found
108 #------------------------------------------------------------
109 # utility functions below.
110 #------------------------------------------------------------
113 # if a bounding box was specified do some sanity checks.
114 # restrict changesets to those enclosed by a bounding box
115 def conditions_bbox(changesets, bbox)
117 bbox.check_boundaries
118 bbox = bbox.to_scaled
120 changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
121 bbox.max_lon.to_i, bbox.min_lon.to_i,
122 bbox.max_lat.to_i, bbox.min_lat.to_i)
129 # eliminate empty changesets (where the bbox has not been set)
130 # this should be applied to all changeset list displays
131 def conditions_nonempty(changesets)
132 changesets.where("num_changes > 0")