+ a.timestamp <=> b.timestamp
+ end
+ end
+
+ # create changeset and user caches
+ changeset_cache = {}
+ user_display_name_cache = {}
+
+ # create an osmChange document for the output
+ result = OSM::API.new.get_xml_doc
+ result.root.name = "osmChange"
+
+ # generate an output element for each operation. note: we avoid looking
+ # at the history because it is simpler - but it would be more correct to
+ # check these assertions.
+ elements.each do |elt|
+ result.root <<
+ if elt.version == 1
+ # first version, so it must be newly-created.
+ created = XML::Node.new "create"
+ created << elt.to_xml_node(changeset_cache, user_display_name_cache)
+ elsif elt.visible
+ # must be a modify
+ modified = XML::Node.new "modify"
+ modified << elt.to_xml_node(changeset_cache, user_display_name_cache)
+ else
+ # if the element isn't visible then it must have been deleted
+ deleted = XML::Node.new "delete"
+ deleted << elt.to_xml_node(changeset_cache, user_display_name_cache)
+ end
+ end
+
+ render :xml => result.to_s
+ end
+
+ ##
+ # query changesets by bounding box, time, user or open/closed status.
+ def query
+ # find any bounding box
+ bbox = BoundingBox.from_bbox_params(params) if params["bbox"]
+
+ # create the conditions that the user asked for. some or all of
+ # these may be nil.
+ changesets = Changeset.all
+ changesets = conditions_bbox(changesets, bbox)
+ changesets = conditions_user(changesets, params["user"], params["display_name"])
+ changesets = conditions_time(changesets, params["time"])
+ changesets = conditions_open(changesets, params["open"])
+ changesets = conditions_closed(changesets, params["closed"])
+ changesets = conditions_ids(changesets, params["changesets"])
+
+ # sort and limit the changesets
+ changesets = changesets.order("created_at DESC").limit(100)
+
+ # preload users, tags and comments
+ changesets = changesets.preload(:user, :changeset_tags, :comments)
+
+ # create the results document
+ results = OSM::API.new.get_xml_doc
+
+ # add all matching changesets to the XML results document
+ changesets.order("created_at DESC").limit(100).each do |cs|
+ results.root << cs.to_xml_node
+ end
+
+ render :xml => results.to_s
+ end
+
+ ##
+ # updates a changeset's tags. none of the changeset's attributes are
+ # user-modifiable, so they will be ignored.
+ #
+ # changesets are not (yet?) versioned, so we don't have to deal with
+ # history tables here. changesets are locked to a single user, however.
+ #
+ # after succesful update, returns the XML of the changeset.
+ def update
+ # request *must* be a PUT.
+ assert_method :put
+
+ changeset = Changeset.find(params[:id])
+ new_changeset = Changeset.from_xml(request.raw_post)
+
+ check_changeset_consistency(changeset, current_user)
+ changeset.update_from(new_changeset, current_user)
+ render :xml => changeset.to_xml.to_s
+ end
+
+ ##
+ # list non-empty changesets in reverse chronological order
+ def index
+ @params = params.permit(:display_name, :bbox, :friends, :nearby, :max_id, :list)
+
+ if request.format == :atom && @params[:max_id]
+ redirect_to url_for(@params.merge(:max_id => nil)), :status => :moved_permanently
+ return
+ end
+
+ if @params[:display_name]
+ user = User.find_by(:display_name => @params[:display_name])
+ if !user || !user.active?
+ render_unknown_user @params[:display_name]
+ return
+ end
+ end
+
+ if (@params[:friends] || @params[:nearby]) && !current_user
+ require_user
+ return
+ end
+
+ if request.format == :html && !@params[:list]
+ require_oauth
+ render :action => :history, :layout => map_layout
+ else
+ changesets = conditions_nonempty(Changeset.all)
+
+ if @params[:display_name]
+ changesets = if user.data_public? || user == current_user
+ changesets.where(:user_id => user.id)
+ else
+ changesets.where("false")
+ end
+ elsif @params[:bbox]
+ changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
+ elsif @params[:friends] && current_user
+ changesets = changesets.where(:user_id => current_user.friend_users.identifiable)
+ elsif @params[:nearby] && current_user
+ changesets = changesets.where(:user_id => current_user.nearby)
+ end
+
+ changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
+
+ @edits = changesets.order("changesets.id DESC").limit(20).preload(:user, :changeset_tags, :comments)
+
+ render :action => :index, :layout => false
+ end
+ end
+
+ ##
+ # list edits as an atom feed
+ def feed
+ index
+ end
+
+ ##
+ # Adds a subscriber to the changeset
+ def subscribe
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset and check it is valid
+ changeset = Changeset.find(id)
+ raise OSM::APIChangesetAlreadySubscribedError, changeset if changeset.subscribers.exists?(current_user.id)
+
+ # Add the subscriber
+ changeset.subscribers << current_user
+
+ # Return a copy of the updated changeset
+ render :xml => changeset.to_xml.to_s
+ end
+
+ ##
+ # Removes a subscriber from the changeset
+ def unsubscribe
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset and check it is valid
+ changeset = Changeset.find(id)
+ raise OSM::APIChangesetNotSubscribedError, changeset unless changeset.subscribers.exists?(current_user.id)
+
+ # Remove the subscriber
+ changeset.subscribers.delete(current_user)
+
+ # Return a copy of the updated changeset
+ render :xml => changeset.to_xml.to_s
+ end
+
+ private
+
+ #------------------------------------------------------------
+ # utility functions below.
+ #------------------------------------------------------------
+
+ ##
+ # if a bounding box was specified do some sanity checks.
+ # restrict changesets to those enclosed by a bounding box
+ # we need to return both the changesets and the bounding box
+ def conditions_bbox(changesets, bbox)
+ if bbox
+ bbox.check_boundaries
+ bbox = bbox.to_scaled
+
+ changesets.where("min_lon < ? and max_lon > ? and min_lat < ? and max_lat > ?",
+ bbox.max_lon.to_i, bbox.min_lon.to_i,
+ bbox.max_lat.to_i, bbox.min_lat.to_i)
+ else
+ changesets
+ end
+ end
+
+ ##
+ # restrict changesets to those by a particular user
+ def conditions_user(changesets, user, name)
+ if user.nil? && name.nil?
+ changesets
+ else
+ # shouldn't provide both name and UID
+ raise OSM::APIBadUserInput, "provide either the user ID or display name, but not both" if user && name
+
+ # use either the name or the UID to find the user which we're selecting on.
+ u = if name.nil?
+ # user input checking, we don't have any UIDs < 1
+ raise OSM::APIBadUserInput, "invalid user ID" if user.to_i < 1
+
+ u = User.find(user.to_i)
+ else
+ u = User.find_by(:display_name => name)
+ end
+
+ # make sure we found a user
+ raise OSM::APINotFoundError if u.nil?
+
+ # should be able to get changesets of public users only, or
+ # our own changesets regardless of public-ness.
+ unless u.data_public?
+ # get optional user auth stuff so that users can see their own
+ # changesets if they're non-public
+ setup_user_auth
+
+ raise OSM::APINotFoundError if current_user.nil? || current_user != u