...
 
Commits (2)
......@@ -203,21 +203,57 @@ function makeOSM (argv) {
return summary
}
// We force the parameters in our search IDs to be in a fixed order
function searchID (query) {
const params = {}
for (const prop of ['q', 'lat', 'lon', 'd', 'page']) {
if (query[prop]) {
params[prop] = query[prop]
}
}
// We don't want a distance if it's not a geographical query
if (params.d && !(params.lat && params.lon)) {
delete params.d
}
const qp = qs.stringify(params)
return makeURI(`/osm/search?${qp}`)
}
function normalizeQuery (query) {
const nq = _.pick(query, ['q', 'lat', 'lon', 'd', 'page'])
if (nq.q) {
nq.q.replace('+', ' ')
}
// We only accept 5 decimal places, ~1m resolution
if (nq.lat) {
nq.lat = Number(Number(nq.lat).toFixed(5))
}
if (nq.lon) {
nq.lon = Number(Number(nq.lon).toFixed(5))
}
if (nq.d) {
nq.d = Number(nq.d)
} else if (nq.lat && nq.lon) {
nq.d = DEFAULT_DISTANCE
}
if (nq.page) {
nq.page = Number(nq.page)
}
return nq
}
osm.get('/search', async (req, res, next) => {
debug('osm /search route matched')
const {q, lat, lon, page} = req.query
let {d} = req.query
debug(`q = '${q}' before cleanup`)
const {page} = req.query
const nquery = normalizeQuery(req.query)
const {q, lat, lon} = nquery
const {d} = nquery
if (!q && (!lat && !lon)) {
return next(new Error("Must define either 'q' or 'lat' and 'lon'"))
}
if (q) {
q.replace('+', ' ')
debug(`q = '${q}' after cleanup`)
}
if (!d) {
d = DEFAULT_DISTANCE
}
let query = null
let vb = null
if (lat && lon) {
......@@ -242,30 +278,31 @@ function makeOSM (argv) {
if (page && (offset > items.length)) {
throw new Error(`No such page ${page}`)
}
const pp = qs.stringify(_.extend({}, req.query, {page: ePage}))
const qp = qs.stringify(_.omit(req.query, 'page'))
const pa = _.extend({}, nquery, {page: ePage})
const summary = searchSummary(q, lat, lon, d)
const pageObject = {
type: 'CollectionPage',
id: makeURI(`/osm/search?${pp}`),
id: searchID(pa),
summary: `page ${ePage} of ${summary}`,
items: items.slice(offset, offset + limit)
}
if (items.length > offset + limit - 1) {
const npp = qs.stringify(_.extend({}, req.query, {page: ePage + 1}))
pageObject.next = makeURI(`/osm/search?${npp}`)
const npa = _.extend({}, nquery, {page: ePage + 1})
pageObject.next = searchID(npa)
}
if (ePage > 1) {
const ppp = qs.stringify(_.extend({}, req.query, {page: ePage - 1}))
pageObject.prev = makeURI(`/osm/search?${ppp}`)
const ppa = _.extend({}, nquery, {page: ePage - 1})
pageObject.prev = searchID(ppa)
}
const qa = _.omit(nquery, 'page')
const collection = {
type: 'Collection',
summary: summary,
id: makeURI(`/osm/search?${qp}`),
id: searchID(qa),
totalItems: items.length
}
......@@ -273,7 +310,7 @@ function makeOSM (argv) {
? _.extend({partOf: collection}, pageObject)
: _.extend({first: _.extend({partOf: collection.id}, pageObject)}, collection)
debug(`Returning AS2 for ${req.query}`)
debug(`Returning AS2 for ${nquery}`)
as2Document(req, res, doc)
} catch (err) {
......
......@@ -2733,5 +2733,5 @@
}
}
},
"version": "0.8.0"
"version": "0.8.1"
}
......@@ -31,5 +31,5 @@
"pug": "^2.0.0-rc.4",
"yargs": "^10.0.3"
},
"version": "0.8.0"
"version": "0.8.1"
}