Skip to content

Commit

Permalink
Add changeset comment search api with filtering by author and time
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Dec 20, 2024
1 parent d08d2f0 commit 91a4251
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 14 deletions.
1 change: 1 addition & 0 deletions app/abilities/api_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(token)
can :create, Note unless token

can [:read, :download], Changeset
can :read, ChangesetComment
can :read, Tracepoint
can :read, User
can :read, Node
Expand Down
15 changes: 13 additions & 2 deletions app/controllers/api/changeset_comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
module Api
class ChangesetCommentsController < ApiController
before_action :check_api_writable
before_action :authorize
include QueryMethods

before_action :check_api_writable, :except => [:index]
before_action :authorize, :except => [:index]

authorize_resource

before_action :require_public_data, :only => [:create]

before_action :set_request_formats

##
# show all comments or search for a subset
def index
@comments = ChangesetComment.includes(:author).where(:visible => true).order("created_at DESC")
@comments = query_conditions_time(@comments)
@comments = query_conditions_user(@comments, :author)
@comments = query_limit(@comments)
end

##
# Add a comment to a changeset
def create
Expand Down
12 changes: 12 additions & 0 deletions app/views/api/changeset_comments/_changeset_comment.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cattrs = {
"id" => changeset_comment.id,
"date" => changeset_comment.created_at.xmlschema,
"visible" => changeset_comment.visible
}
if changeset_comment.author.data_public?
cattrs["uid"] = changeset_comment.author.id
cattrs["user"] = changeset_comment.author.display_name
end
xml.comment(cattrs) do |comment_xml_node|
comment_xml_node.text(changeset_comment.body)
end
7 changes: 7 additions & 0 deletions app/views/api/changeset_comments/index.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
xml.instruct! :xml, :version => "1.0"

xml.osm(OSM::API.new.xml_root_attributes) do |osm|
@comments.includes(:author).each do |comment|
osm << render(comment)
end
end
13 changes: 1 addition & 12 deletions app/views/api/changesets/_changeset.xml.builder
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,7 @@ xml.changeset(attrs) do |changeset_xml_node|
if @comments
changeset_xml_node.discussion do |discussion_xml_node|
@comments.each do |comment|
cattrs = {
"id" => comment.id,
"date" => comment.created_at.xmlschema,
"visible" => comment.visible
}
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
discussion_xml_node << render(comment)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
post "changeset/comment/:id/hide" => "changeset_comments#destroy", :as => :changeset_comment_hide, :id => /\d+/
post "changeset/comment/:id/unhide" => "changeset_comments#restore", :as => :changeset_comment_unhide, :id => /\d+/

resources :changeset_comments, :only => [:index], :controller => "changeset_comments", :as => :api_changeset_comments

put "node/create" => "nodes#create"
get "node/:id/ways" => "ways#ways_for_node", :as => :node_ways, :id => /\d+/
get "node/:id/relations" => "relations#relations_for_node", :as => :node_relations, :id => /\d+/
Expand Down
4 changes: 4 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ tracepoints_per_page: 5000
default_changeset_query_limit: 100
# Maximum limit on the number of changesets returned by the changeset query api method
max_changeset_query_limit: 100
# Default limit on the number of changeset comments returned by the api
default_changeset_comment_query_limit: 100
# Maximum limit on the number of changesets comments returned by the api
max_changeset_comment_query_limit: 10000
# Default limit on the number of changeset comments in feeds
default_changeset_comments_feed_query_limit: 100
# Maximum limit on the number of changesets comments in feeds
Expand Down
54 changes: 54 additions & 0 deletions test/controllers/api/changeset_comments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class ChangesetCommentsControllerTest < ActionDispatch::IntegrationTest
##
# test all routes which lead to this controller
def test_routes
assert_routing(
{ :path => "/api/0.6/changeset_comments", :method => :get },
{ :controller => "api/changeset_comments", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/changeset/1/comment", :method => :post },
{ :controller => "api/changeset_comments", :action => "create", :id => "1" }
Expand All @@ -31,6 +35,40 @@ def test_routes
)
end

##
# view comments
def test_index
user1 = create(:user)
user2 = create(:user)
changeset1 = create(:changeset, :closed, :user => user2)
comment11 = create(:changeset_comment, :changeset => changeset1, :author => user1, :created_at => "2023-01-01", :body => "changeset 1 question")
comment12 = create(:changeset_comment, :changeset => changeset1, :author => user2, :created_at => "2023-02-01", :body => "changeset 1 answer")
changeset2 = create(:changeset, :closed, :user => user1)
comment21 = create(:changeset_comment, :changeset => changeset2, :author => user1, :created_at => "2023-03-01", :body => "changeset 2 note")
comment22 = create(:changeset_comment, :changeset => changeset2, :author => user1, :created_at => "2023-04-01", :body => "changeset 2 extra note")
comment23 = create(:changeset_comment, :changeset => changeset2, :author => user2, :created_at => "2023-05-01", :body => "changeset 2 review")

get api_changeset_comments_path
assert_response :success
assert_comments_in_order [comment23, comment22, comment21, comment12, comment11]

get api_changeset_comments_path(:limit => 3)
assert_response :success
assert_comments_in_order [comment23, comment22, comment21]

get api_changeset_comments_path(:from => "2023-03-15T00:00:00Z")
assert_response :success
assert_comments_in_order [comment23, comment22]

get api_changeset_comments_path(:from => "2023-01-15T00:00:00Z", :to => "2023-04-15T00:00:00Z")
assert_response :success
assert_comments_in_order [comment22, comment21, comment12]

get api_changeset_comments_path(:user => user1.id)
assert_response :success
assert_comments_in_order [comment22, comment21, comment11]
end

##
# create comment success
def test_create_comment_success
Expand Down Expand Up @@ -320,5 +358,21 @@ def test_api_write_and_terms_agreed_via_token
end
assert_response :success
end

private

##
# check that certain comments exist in the output in the specified order
def assert_comments_in_order(comments)
assert_select "osm>comment", comments.size
comments.each_with_index do |comment, index|
assert_select "osm>comment:nth-child(#{index + 1})", 1 do
assert_select ">@id", comment.id.to_s
assert_select ">@uid", comment.author.id.to_s
assert_select ">@user", comment.author.display_name
assert_select ">text", comment.body
end
end
end
end
end

0 comments on commit 91a4251

Please sign in to comment.