- Changeset.transaction do
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- case p.name
- when 'create':
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- case p.name
- when 'node':
- elem = XML::Node.new 'node'
- node = Node.from_xml_node(p.expand, true)
- elem['old_id'] = p.expand['id']
- create_prim node_ids, node, p.expand
- elem['new_id'] = node.id.to_s
- elem['new_version'] = node.version.to_s
- root << elem
- when 'way':
- elem = XML::Node.new 'way'
- way = Way.from_xml_node(p.expand, true)
- elem['old_id'] = p.expand['id']
- fix_way(way, node_ids)
- raise OSM::APIPreconditionFailedError.new if !way.preconditions_ok?
- create_prim way_ids, way, p.expand
- elem['new_id'] = way.id.to_s
- elem['new_version'] = way.version.to_s
- root << elem
- when 'relation':
- elem = XML::Node.new 'relation'
- relation = Relation.from_xml_node(p.expand, true)
- elem['old_id'] = p.expand['id']
- fix_rel(relation, ids)
- raise OSM::APIPreconditionFailedError.new if !relation.preconditions_ok?
- create_prim rel_ids, relation, p.expand
- elem['new_id'] = relation.id.to_s
- elem['new_version'] = relation.version.to_s
- root << elem
- end
- end
- when 'modify':
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- case p.name
- when 'node':
- elem = XML::Node.new 'node'
- new_node = Node.from_xml_node(p.expand)
- node = Node.find(new_node.id)
- node.update_from new_node, @user
- elem['old_id'] = elem['new_id'] = node.id.to_s
- elem['new_version'] = node.version.to_s
- root << elem
- when 'way':
- elem = XML::Node.new 'way'
- new_way = Way.from_xml_node(p.expand)
- way = Way.find(new_way.id)
- way.update_from new_way, @user
- elem['old_id'] = elem['new_id'] = way.id.to_s
- elem['new_version'] = way.version.to_s
- root << elem
- when 'relation':
- elem = XML::Node.new 'relation'
- new_relation = Relation.from_xml_node(p.expand)
- relation = Relation.find(new_relation.id)
- relation.update_from new_relation, @user
- elem['old_id'] = elem['new_id'] = relation.id.to_s
- elem['new_version'] = relation.version.to_s
- root << elem
- end
- end
- when 'delete':
- while p.read == 1
- break if p.node_type == 15 # end element
- next unless p.node_type == 1 # element
-
- case p.name
- when 'node':
- elem = XML::Node.new 'node'
- node = Node.find(p.expand['id'])
- node.delete_with_history(@user)
- elem['old_id'] = elem['new_id'] = node.id.to_s
- elem['new_version'] = node.version.to_s
- root << elem
- when 'way':
- elem = XML::Node.new 'way'
- way = Way.find(p.expand['id'])
- way.delete_with_history(@user)
- elem['old_id'] = elem['new_id'] = way.id.to_s
- elem['new_version'] = way.version.to_s
- root << elem
- when 'relation':
- elem = XML::Node.new 'relation'
- relation = Relation.find(p.expand['id'])
- relation.delete_with_history(@user)
- elem['old_id'] = elem['new_id'] = relation.id.to_s
- elem['new_version'] = relation.version.to_s
- root << elem
- end
- end
- end
+ render :action => :list, :layout => false
+ end
+ end
+
+ ##
+ # list edits as an atom feed
+ def feed
+ list
+ end
+
+ ##
+ # Add a comment to a changeset
+ def comment
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
+ raise OSM::APIBadUserInput.new("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.new(changeset) if changeset.is_open?
+
+ # Add a comment to the changeset
+ comment = changeset.comments.create(:changeset => changeset,
+ :body => body,
+ :author => @user)
+
+ # Notify current subscribers of the new comment
+ changeset.subscribers.visible.each do |user|
+ if @user != user
+ Notifier.changeset_comment_notification(comment, user).deliver_now
+ end
+ end
+
+ # Add the commenter to the subscribers if necessary
+ changeset.subscribers << @user unless changeset.subscribers.exists?(@user.id)
+
+ # Return a copy of the updated changeset
+ render :text => changeset.to_xml.to_s, :content_type => "text/xml"
+ end
+
+ ##
+ # Adds a subscriber to the changeset
+ def subscribe
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput.new("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::APIChangesetNotYetClosedError.new(changeset) if changeset.is_open?
+ raise OSM::APIChangesetAlreadySubscribedError.new(changeset) if changeset.subscribers.exists?(@user.id)
+
+ # Add the subscriber
+ changeset.subscribers << @user
+
+ # Return a copy of the updated changeset
+ render :text => changeset.to_xml.to_s, :content_type => "text/xml"
+ end
+
+ ##
+ # Removes a subscriber from the changeset
+ def unsubscribe
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput.new("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::APIChangesetNotYetClosedError.new(changeset) if changeset.is_open?
+ raise OSM::APIChangesetNotSubscribedError.new(changeset) unless changeset.subscribers.exists?(@user.id)
+
+ # Remove the subscriber
+ changeset.subscribers.delete(@user)
+
+ # Return a copy of the updated changeset
+ render :text => changeset.to_xml.to_s, :content_type => "text/xml"
+ end
+
+ ##
+ # Sets visible flag on comment to false
+ def hide_comment
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput.new("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 :text => comment.changeset.to_xml.to_s, :content_type => "text/xml"
+ end
+
+ ##
+ # Sets visible flag on comment to true
+ def unhide_comment
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput.new("No id was given") unless params[:id]
+
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset
+ comment = ChangesetComment.find(id)
+
+ # Unhide the comment
+ comment.update(:visible => true)
+
+ # Return a copy of the updated changeset
+ render :text => comment.changeset.to_xml.to_s, :content_type => "text/xml"
+ end
+
+ ##
+ # Get a feed of recent changeset comments
+ def comments_feed
+ if params[:id]
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset
+ changeset = Changeset.find(id)
+
+ # Return comments for this changeset only
+ @comments = changeset.comments.includes(:author, :changeset).limit(comments_limit)
+ else
+ # Return comments
+ @comments = ChangesetComment.includes(:author, :changeset).where(:visible => :true).order("created_at DESC").limit(comments_limit).preload(:changeset)
+ end
+
+ # Render the result
+ respond_to do |format|
+ format.rss
+ end
+ rescue OSM::APIBadUserInput
+ render :text => "", :status => :bad_request
+ 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.new("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.new("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.new 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 @user.nil? || @user.id != u.id