Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/2164'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Mar 16, 2019
2 parents 6547ac2 + d98683a commit 874fddf
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 91 deletions.
9 changes: 6 additions & 3 deletions app/controllers/api/changeset_comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def create
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
@changeset = changeset
render "api/changesets/changeset"
end

##
Expand All @@ -60,7 +61,8 @@ def destroy
comment.update(:visible => false)

# Return a copy of the updated changeset
render :xml => comment.changeset.to_xml.to_s
@changeset = comment.changeset
render "api/changesets/changeset"
end

##
Expand All @@ -79,7 +81,8 @@ def restore
comment.update(:visible => true)

# Return a copy of the updated changeset
render :xml => comment.changeset.to_xml.to_s
@changeset = comment.changeset
render "api/changesets/changeset"
end
end
end
38 changes: 16 additions & 22 deletions app/controllers/api/changesets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def create
# Return XML giving the basic info about the changeset. Does not
# return anything about the nodes, ways and relations in the changeset.
def show
changeset = Changeset.find(params[:id])

render :xml => changeset.to_xml(params[:include_discussion].presence).to_s
@changeset = Changeset.find(params[:id])
@include_discussion = params[:include_discussion].presence
render "changeset"
end

##
Expand Down Expand Up @@ -104,7 +104,8 @@ def expand_bbox
# save the larger bounding box and return the changeset, which
# will include the bigger bounding box.
cs.save!
render :xml => cs.to_xml.to_s
@changeset = cs
render "changeset"
end

##
Expand Down Expand Up @@ -219,18 +220,9 @@ def query
# 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
# preload users, tags and comments, and render result
@changesets = changesets.preload(:user, :changeset_tags, :comments)
render "changesets"
end

##
Expand All @@ -245,12 +237,12 @@ def update
# request *must* be a PUT.
assert_method :put

changeset = Changeset.find(params[:id])
@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
check_changeset_consistency(@changeset, current_user)
@changeset.update_from(new_changeset, current_user)
render "changeset"
end

##
Expand All @@ -270,7 +262,8 @@ def subscribe
changeset.subscribers << current_user

# Return a copy of the updated changeset
render :xml => changeset.to_xml.to_s
@changeset = changeset
render "changeset"
end

##
Expand All @@ -290,7 +283,8 @@ def unsubscribe
changeset.subscribers.delete(current_user)

# Return a copy of the updated changeset
render :xml => changeset.to_xml.to_s
@changeset = changeset
render "changeset"
end

private
Expand Down
61 changes: 0 additions & 61 deletions app/models/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,67 +196,6 @@ def update_closed_at
end
end

def to_xml(include_discussion = false)
doc = OSM::API.new.get_xml_doc
doc.root << to_xml_node(nil, include_discussion)
doc
end

def to_xml_node(user_display_name_cache = nil, include_discussion = false)
el1 = XML::Node.new "changeset"
el1["id"] = id.to_s

user_display_name_cache = {} if user_display_name_cache.nil?

if user_display_name_cache&.key?(user_id)
# use the cache if available
elsif user.data_public?
user_display_name_cache[user_id] = user.display_name
else
user_display_name_cache[user_id] = nil
end

el1["user"] = user_display_name_cache[user_id] unless user_display_name_cache[user_id].nil?
el1["uid"] = user_id.to_s if user.data_public?

tags.each do |k, v|
el2 = XML::Node.new("tag")
el2["k"] = k.to_s
el2["v"] = v.to_s
el1 << el2
end

el1["created_at"] = created_at.xmlschema
el1["closed_at"] = closed_at.xmlschema unless is_open?
el1["open"] = is_open?.to_s

bbox.to_unscaled.add_bounds_to(el1, "_") if bbox.complete?

el1["comments_count"] = comments.length.to_s
el1["changes_count"] = num_changes.to_s

if include_discussion
el2 = XML::Node.new("discussion")
comments.includes(:author).each do |comment|
el3 = XML::Node.new("comment")
el3["date"] = comment.created_at.xmlschema
el3["uid"] = comment.author.id.to_s if comment.author.data_public?
el3["user"] = comment.author.display_name.to_s if comment.author.data_public?
el4 = XML::Node.new("text")
el4.content = comment.body.to_s
el3 << el4
el2 << el3
end
el1 << el2
end

# NOTE: changesets don't include the XML of the changes within them,
# they are just structures for tagging. to get the osmChange of a
# changeset, see the download method of the controller.

el1
end

##
# update this instance from another instance given and the user who is
# doing the updating. note that this method is not for updating the
Expand Down
43 changes: 43 additions & 0 deletions app/views/api/changesets/_changeset.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# basic attributes

attrs = {
"id" => changeset.id,
"created_at" => changeset.created_at.xmlschema,
"open" => changeset.is_open?,
"comments_count" => changeset.comments.length,
"changes_count" => changeset.num_changes
}
attrs["closed_at"] = changeset.closed_at unless changeset.is_open?
changeset.bbox.to_unscaled.add_bounds_to(attrs, "_") if changeset.bbox.complete?

# user attributes

if changeset.user.data_public?
attrs["uid"] = changeset.user_id
attrs["user"] = changeset.user.display_name
end

xml.changeset(attrs) do |changeset_xml_node|
changeset.tags.each do |k, v|
changeset_xml_node.tag(:k => k, :v => v)
end

# include discussion if requested

if @include_discussion
changeset_xml_node.discussion do |discussion_xml_node|
changeset.comments.includes(:author).each do |comment|
cattrs = {
"date" => comment.created_at.xmlschema
}
if comment.author.data_public?
cattrs["uid"] = comment.author.id
cattrs["user"] = comment.author.display_name
end
discussion_xml_node.comment(cattrs) do |comment_xml_node|
comment_xml_node.text(comment.body)
end
end
end
end
end
7 changes: 7 additions & 0 deletions app/views/api/changesets/changeset.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
xml.instruct! :xml, :version => "1.0"

# basic attributes

xml.osm(OSM::API.new.xml_root_attributes) do |osm|
osm << render(:partial => "api/changesets/changeset.builder", :locals => { :changeset => @changeset })
end
9 changes: 9 additions & 0 deletions app/views/api/changesets/changesets.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
xml.instruct! :xml, :version => "1.0"

# basic attributes

xml.osm(OSM::API.new.xml_root_attributes) do |osm|
@changesets.each do |changeset|
osm << render(:partial => "api/changesets/changeset.builder", :locals => { :changeset => changeset })
end
end
24 changes: 19 additions & 5 deletions test/controllers/api/changesets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ def test_changeset_update
changeset = create(:changeset, :user => user)

## First try with a non-public user
new_changeset = private_changeset.to_xml
new_changeset = create_changeset_xml(:user => private_user)
new_tag = XML::Node.new "tag"
new_tag["k"] = "tagtesting"
new_tag["v"] = "valuetesting"
Expand All @@ -1695,8 +1695,7 @@ def test_changeset_update
assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"

## Now try with the public user
create(:changeset_tag, :changeset => changeset)
new_changeset = changeset.to_xml
new_changeset = create_changeset_xml(:id => 1)
new_tag = XML::Node.new "tag"
new_tag["k"] = "tagtesting"
new_tag["v"] = "valuetesting"
Expand All @@ -1718,7 +1717,7 @@ def test_changeset_update
assert_response :success

assert_select "osm>changeset[id='#{changeset.id}']", 1
assert_select "osm>changeset>tag", 2
assert_select "osm>changeset>tag", 1
assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
end

Expand All @@ -1729,7 +1728,7 @@ def test_changeset_update_invalid
basic_authorization create(:user).email, "test"

changeset = create(:changeset)
new_changeset = changeset.to_xml
new_changeset = create_changeset_xml(:user => changeset.user, :id => changeset.id)
new_tag = XML::Node.new "tag"
new_tag["k"] = "testing"
new_tag["v"] = "testing"
Expand Down Expand Up @@ -1959,5 +1958,20 @@ def xml_attr_rewrite(xml, name, value)
xml.find("//osm/way").first[name] = value.to_s
xml
end

##
# build XML for changesets
def create_changeset_xml(user: nil, id: nil)
root = XML::Document.new
root.root = XML::Node.new "osm"
cs = XML::Node.new "changeset"
if user
cs["user"] = user.display_name
cs["uid"] = user.id.to_s
end
cs["id"] = id.to_s if id
root.root << cs
root
end
end
end

0 comments on commit 874fddf

Please sign in to comment.