+ 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
+
+ ##
+ # Add a comment to a changeset
+ def comment
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+ raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
+
+ # Extract the arguments
+ id = params[:id].to_i
+ body = params[:text]
+
+ # Find the changeset and check it is valid
+ changeset = Changeset.find(id)
+ raise OSM::APIChangesetNotYetClosedError, changeset if changeset.is_open?
+
+ # Add a comment to the changeset
+ comment = changeset.comments.create(:changeset => changeset,
+ :body => body,
+ :author => current_user)
+
+ # Notify current subscribers of the new comment
+ changeset.subscribers.visible.each do |user|
+ Notifier.changeset_comment_notification(comment, user).deliver_now if current_user != user
+ end
+
+ # Add the commenter to the subscribers if necessary
+ changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
+
+ # Return a copy of the updated changeset
+ render :xml => changeset.to_xml.to_s
+ 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
+
+ ##
+ # Sets visible flag on comment to false
+ def hide_comment
+ # 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
+ comment = ChangesetComment.find(id)
+
+ # Hide the comment
+ comment.update(:visible => false)
+
+ # Return a copy of the updated changeset
+ render :xml => comment.changeset.to_xml.to_s
+ end
+
+ ##
+ # Sets visible flag on comment to true
+ def unhide_comment
+ # 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
+ comment = ChangesetComment.find(id)