Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ids to changeset comments output #289

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/cgimap/output_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct changeset_info {
};

struct changeset_comment_info {
osm_changeset_comment_id_t id;
osm_user_id_t author_id;
std::string body;
std::string created_at;
Expand Down
1 change: 1 addition & 0 deletions include/cgimap/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using osm_user_id_t = uint64_t ;
using osm_changeset_id_t = int64_t;
using osm_changeset_comment_id_t = int32_t;
using osm_nwr_id_t = uint64_t;
using osm_nwr_signed_id_t = int64_t;
using tile_id_t = uint32_t;
Expand Down
8 changes: 5 additions & 3 deletions src/backend/apidb/common_pgsql_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,19 @@ void extract_comments(const pqxx_tuple &row, comments_t &comments) {
changeset_comment_info comment;
comments.clear();

auto id = psql_array_to_vector(row["comment_id"].c_str());
auto author_id = psql_array_to_vector(row["comment_author_id"].c_str());
auto display_name = psql_array_to_vector(row["comment_display_name"].c_str());
auto body = psql_array_to_vector(row["comment_body"].c_str());
auto created_at = psql_array_to_vector(row["comment_created_at"].c_str());

if (author_id.size()!=display_name.size() || display_name.size()!=body.size()
|| body.size()!=created_at.size()) {
if (id.size()!=author_id.size() || author_id.size()!=display_name.size()
|| display_name.size()!=body.size() || body.size()!=created_at.size()) {
throw std::runtime_error("Mismatch in comments author_id, display_name, body and created_at size");
}

for (std::size_t i=0; i<author_id.size(); i++) {
for (std::size_t i=0; i<id.size(); i++) {
comment.id = std::stol(id[i]);
comment.author_id = std::stol(author_id[i]);
comment.author_display_name = display_name[i];
comment.body = body[i];
Expand Down
18 changes: 12 additions & 6 deletions src/backend/apidb/readonly_pgsql_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,19 +339,25 @@ void readonly_pgsql_selection::write_changesets(output_formatter &formatter,
"SELECT c.id, "
"to_char(c.created_at,'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') AS created_at, "
"to_char(c.closed_at, 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') AS closed_at, "
"c.min_lat, c.max_lat, c.min_lon, c.max_lon, c.num_changes, t.keys as tag_k, "
"t.values as tag_v, cc.author_id as comment_author_id, "
"c.min_lat, c.max_lat, c.min_lon, c.max_lon, "
"c.num_changes, "
"t.keys as tag_k, t.values as tag_v, "
"cc.id as comment_id, "
"cc.author_id as comment_author_id, "
"cc.display_name as comment_display_name, "
"cc.body as comment_body, cc.created_at as comment_created_at "
"cc.body as comment_body, "
"cc.created_at as comment_created_at "
"FROM changesets c "
"LEFT JOIN LATERAL "
"(SELECT array_agg(k) AS keys, array_agg(v) AS values "
"FROM changeset_tags WHERE c.id=changeset_id ) t ON true "
"LEFT JOIN LATERAL "
"(SELECT array_agg(author_id) as author_id, array_agg(display_name) "
"as display_name, array_agg(body) as body, "
"(SELECT array_agg(id) as id, "
"array_agg(author_id) as author_id, "
"array_agg(display_name) as display_name, "
"array_agg(body) as body, "
"array_agg(created_at) as created_at FROM "
"(SELECT cc.author_id, u.display_name, cc.body, "
"(SELECT cc.id, cc.author_id, u.display_name, cc.body, "
"to_char(cc.created_at,'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') AS created_at "
"FROM changeset_comments cc JOIN users u ON cc.author_id = u.id "
"where cc.changeset_id=c.id AND cc.visible ORDER BY cc.created_at) x "
Expand Down
1 change: 1 addition & 0 deletions src/backend/staticxml/staticxml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ struct xml_parser {
parser->m_cur_changeset->m_info.comments_count += 1;

changeset_comment_info info;
info.id = get_attribute<osm_changeset_comment_id_t>("id", 3, attributes);
info.author_id = get_attribute<osm_user_id_t>("uid", 4, attributes);
info.author_display_name = get_attribute<std::string>("user", 5, attributes);
info.created_at = get_attribute<std::string>("date", 5, attributes);
Expand Down
2 changes: 2 additions & 0 deletions src/json_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ void json_formatter::write_changeset(const changeset_info &elem,
writer->start_array();
for (const auto & comment : comments) {
writer->start_object();
writer->object_key("id");
writer->entry_int(comment.id);
writer->object_key("date");
writer->entry_string(comment.created_at);
writer->object_key("uid");
Expand Down
3 changes: 2 additions & 1 deletion src/output_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ bool changeset_info::is_open_at(const std::chrono::system_clock::time_point &now
}

bool changeset_comment_info::operator==(const changeset_comment_info &other) const {
return ((author_id == other.author_id) &&
return ((id == other.id) &&
(author_id == other.author_id) &&
(body == other.body) &&
(created_at == other.created_at) &&
(author_display_name == other.author_display_name));
Expand Down
1 change: 1 addition & 0 deletions src/xml_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void xml_formatter::write_changeset(const changeset_info &elem,
writer->start("discussion");
for (const auto & comment : comments) {
writer->start("comment");
writer->attribute("id", comment.id);
writer->attribute("date", comment.created_at);
writer->attribute("uid", comment.author_id);
writer->attribute("user", comment.author_display_name);
Expand Down
15 changes: 14 additions & 1 deletion test/changesets.testcore/data.osm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<changeset id="1" user="FakeUser1" uid="1" created_at="2015-08-09T10:33:13Z" closed_at="2015-08-09T11:33:13Z" min_lat="51.5288506" min_lon="-0.1465242" max_lat="51.528862" max_lon="-0.1464925" comments_count="1" num_changes="1">
<tag k="created_by" v="manually"/>
<discussion>
<comment date="2015-08-09T10:33:13Z" uid="1" user="FakeUser1">
<comment id="1" date="2015-08-09T10:33:13Z" uid="1" user="FakeUser1">
<text>First post!!!!</text>
</comment>
</discussion>
Expand All @@ -17,6 +17,19 @@
<changeset id="9" user="FakeUser2" uid="2" created_at="2017-03-13T16:12:00Z" closed_at="2017-03-13T16:12:00Z" comments_count="0" num_changes="0"/>
<changeset id="10" user="FakeUser2" uid="2" created_at="2017-03-13T16:27:00Z" closed_at="2017-03-13T16:27:00Z" comments_count="0" num_changes="0"/>
<changeset id="11" user="FakeUser2" uid="2" created_at="2017-03-13T16:32:00Z" closed_at="2017-03-13T16:32:02Z" comments_count="0" num_changes="0"/>
<changeset id="12" user="FakeUser1" uid="1" created_at="2020-01-01T12:34:56Z" closed_at="2020-01-01T13:12:11Z" comments_count="3" num_changes="0">
<discussion>
<comment id="2" date="2020-01-02T12:00:00Z" uid="1" user="FakeUser1">
<text>it was empty</text>
</comment>
<comment id="3" date="2020-01-03T12:00:00Z" uid="2" user="FakeUser2">
<text>why?</text>
</comment>
<comment id="4" date="2020-01-04T12:00:00Z" uid="1" user="FakeUser1">
<text>because</text>
</comment>
</discussion>
</changeset>

<node id="1" version="1" changeset="1" lat="0.0" lon="0.0" user="FakeUser1" uid="1" visible="true" timestamp="2015-08-09T10:33:13Z"/>
<node id="2" version="1" changeset="3" lat="0.0" lon="0.0" user="FakeUser2" uid="2" visible="true" timestamp="2017-03-13T09:52:00Z"/>
Expand Down
2 changes: 1 addition & 1 deletion test/changesets.testcore/not_found.case
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# empty changeset - the changeset does not exist, so returns a 404.
Request-Method: GET
Request-URI: /api/0.6/changeset/12/download
Request-URI: /api/0.6/changeset/13/download
Date: 2017-03-13T16:58:00Z
---
Status: 404 Not Found
Expand Down
2 changes: 1 addition & 1 deletion test/changesets.testcore/read_discussion.case
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Content-Type: application/xml; charset=utf-8
<changeset id="1" user="FakeUser1" uid="1" created_at="2015-08-09T10:33:13Z" closed_at="2015-08-09T11:33:13Z" open="false" min_lat="51.5288506" min_lon="-0.1465242" max_lat="51.5288620" max_lon="-0.1464925" comments_count="1" changes_count="1">
<tag k="created_by" v="manually"/>
<discussion>
<comment date="2015-08-09T10:33:13Z" uid="1" user="FakeUser1">
<comment id="1" date="2015-08-09T10:33:13Z" uid="1" user="FakeUser1">
<text>First post!!!</text>
</comment>
</discussion>
Expand Down
1 change: 1 addition & 0 deletions test/changesets.testcore/read_discussion_json.case
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Cache-Control: private, max-age=0, must-revalidate
"created_by": "manually"
},
"discussion": [{
"id": 1,
"date": "2015-08-09T10:33:13Z",
"uid": 1,
"user": "FakeUser1",
Expand Down
24 changes: 24 additions & 0 deletions test/changesets.testcore/read_discussion_multiple.case
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Request-Method: GET
Request-URI: /api/0.6/changeset/12?include_discussion=1
HTTP-X-Error-Format: xml
Date: 2021-01-01T12:00:00Z
---
Status: 200 OK
Content-Type: application/xml; charset=utf-8
!Content-Disposition:
---
<osm version="0.6" generator="***" copyright="***" attribution="***" license="***">
<changeset id="12" user="FakeUser1" uid="1" created_at="2020-01-01T12:34:56Z" closed_at="2020-01-01T13:12:11Z" open="false" comments_count="3" changes_count="0">
<discussion>
<comment id="2" date="2020-01-02T12:00:00Z" uid="1" user="FakeUser1">
<text>it was empty</text>
</comment>
<comment id="3" date="2020-01-03T12:00:00Z" uid="2" user="FakeUser2">
<text>why?</text>
</comment>
<comment id="4" date="2020-01-04T12:00:00Z" uid="1" user="FakeUser1">
<text>because</text>
</comment>
</discussion>
</changeset>
</osm>
47 changes: 47 additions & 0 deletions test/changesets.testcore/read_discussion_multiple_json.case
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Request-Method: GET
Request-URI: /api/0.6/changeset/12.json?include_discussion=1
Date: 2021-01-01T12:00:00Z
---
Status: 200 OK
Content-Type: application/json; charset=utf-8
Content-Encoding: identity
Cache-Control: private, max-age=0, must-revalidate
!Content-Disposition:
---
{
"version": "0.6",
"generator": "***",
"copyright": "***",
"attribution": "***",
"license": "***",
"elements": [{
"type": "changeset",
"id": 12,
"created_at": "2020-01-01T12:34:56Z",
"closed_at": "2020-01-01T13:12:11Z",
"open": false,
"user": "FakeUser1",
"uid": 1,
"comments_count": 3,
"changes_count": 0,
"discussion": [{
"id": 2,
"date": "2020-01-02T12:00:00Z",
"uid": 1,
"user": "FakeUser1",
"text": "it was empty"
},{
"id": 3,
"date": "2020-01-03T12:00:00Z",
"uid": 2,
"user": "FakeUser2",
"text": "why?"
},{
"id": 4,
"date": "2020-01-04T12:00:00Z",
"uid": 1,
"user": "FakeUser1",
"text": "because"
}]
}]
}
1 change: 1 addition & 0 deletions test/test_apidb_backend_changesets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ void check_changeset_with_comments_impl(
comments_t comments;
{
changeset_comment_info comment;
comment.id = 1;
comment.author_id = 3;
comment.body = "a nice comment!";
comment.created_at = "2015-09-05T20:37:01Z";
Expand Down
3 changes: 2 additions & 1 deletion test/test_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ std::ostream &operator<<(std::ostream &out, const test_formatter::changeset_t &c
<< "include_comments=" << c.m_include_comments << ", "
<< "comments[";
for (const comments_t::value_type &v : c.m_comments) {
out << "comment(author_id=" << v.author_id << ", "
out << "comment(id=" << v.id << ", "
<< "author_id=" << v.author_id << ", "
<< "body=\"" << v.body << "\", "
<< "created_at=\"" << v.created_at << "\", "
<< "author_display_name=\"" << v.author_display_name << "\"), ";
Expand Down
Loading