From 9c93fe1281b7b1a14d4e469c1b68754bdc66edee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Thu, 6 Oct 2016 20:28:21 +0200 Subject: [PATCH 1/4] test(search): test more properties for query To prevent failing test. --- test/integration/server.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/integration/server.js b/test/integration/server.js index 82cfd54..89942fe 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -212,16 +212,21 @@ describe('server', () => { it('?query', function it(done) { this.timeout(10000); + const queryStr = 'Flaatten'; + const queryRe = new RegExp(queryStr, 'i'); - const url = `/latest/albums/${albumId}/photos?query=Flaatten`; + const url = `/latest/albums/${albumId}/photos?query=${queryStr}`; app.get(url) .set('Origin', 'https://example1.com') .expect(200) .expect(res => res.body.data.forEach(data => { assert( - /flaatten/i.test((data.metadata.photographers || []).join(' ')) || - /flaatten/i.test((data.metadata.persons || '')) + queryRe.test((data.metadata.photographers || []).join(' ')) || + queryRe.test((data.metadata.persons || '')) || + queryRe.test((data.metadata.tags || []).join(' ')) || + queryRe.test((data.metadata.description || '')) || + queryRe.test((data.filename || '')) ); })) .end(done); From 788d77ad1c30e1548386f4354b67ed61bf8c32fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 7 Oct 2016 12:41:10 +0200 Subject: [PATCH 2/4] chore(fotoweb): fix typo --- lib/fotoweb.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fotoweb.js b/lib/fotoweb.js index 45aa58e..3dffe8b 100644 --- a/lib/fotoweb.js +++ b/lib/fotoweb.js @@ -1,6 +1,6 @@ 'use strict'; -const PHOTO_MEDADATA = [ +const PHOTO_METADATA = [ ['q', 'query'], ['5', 'title'], ['20', 'albums'], @@ -13,8 +13,8 @@ const PHOTO_MEDADATA = [ ['222', 'area'], ]; -const PHOTO_METADATA_KEYS = new Map(PHOTO_MEDADATA); -const PHOTO_METADATA_IDS = new Map(PHOTO_MEDADATA.map(key => key.reverse())); +const PHOTO_METADATA_KEYS = new Map(PHOTO_METADATA); +const PHOTO_METADATA_IDS = new Map(PHOTO_METADATA.map(key => key.reverse())); const BASE_URL = process.env.FOTOWEB_BASE_URL; const API_PATH = process.env.FOTOWEB_API_PATH; From 99a460ec98b6c195c12bb9cc481d4dd2f7763f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 7 Oct 2016 12:43:03 +0200 Subject: [PATCH 3/4] chore(fotoweb): don't mutate original array --- lib/fotoweb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fotoweb.js b/lib/fotoweb.js index 3dffe8b..8af07bc 100644 --- a/lib/fotoweb.js +++ b/lib/fotoweb.js @@ -14,7 +14,7 @@ const PHOTO_METADATA = [ ]; const PHOTO_METADATA_KEYS = new Map(PHOTO_METADATA); -const PHOTO_METADATA_IDS = new Map(PHOTO_METADATA.map(key => key.reverse())); +const PHOTO_METADATA_IDS = new Map(PHOTO_METADATA.map(key => [...key].reverse())); const BASE_URL = process.env.FOTOWEB_BASE_URL; const API_PATH = process.env.FOTOWEB_API_PATH; From a1f1a8e617742b38136b0bed0d7bb090a5c052c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Ranum?= Date: Fri, 7 Oct 2016 12:48:24 +0200 Subject: [PATCH 4/4] fix(paging): return correct paging URL's Skadi did not preserve filter query parameters in paging URL's. Now it is, which enables browsing the entire search result using paging URL's. --- index.js | 14 ++++++++++---- lib/fotoweb.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0120d92..d170de9 100644 --- a/index.js +++ b/index.js @@ -176,10 +176,6 @@ router.get('/albums/:album/photos', (req, res, next) => { }, }; - if (req.query.page) { - opts.url = `${opts.url};p=${req.query.page}`; - } - const qs = []; Object.keys(req.query).forEach(tag => { @@ -189,9 +185,19 @@ router.get('/albums/:album/photos', (req, res, next) => { } }); + /* + // If query has filtering queries, paging must be set a reqular query param. + // If not, paging is set as a semi colon param (second conditional). + */ if (qs.length) { opts.url = `${fotoweb.API_URL}/archives/${albumId.split('.')[0]}/`; opts.url = `${opts.url}?${qs.join('&')}`; + + if (req.query.page) { + opts.url = `${opts.url}&p=${req.query.page}`; + } + } else if (req.query.page) { + opts.url = `${opts.url};p=${req.query.page}`; } request.get(opts, (err, resp, body) => { diff --git a/lib/fotoweb.js b/lib/fotoweb.js index 8af07bc..a0a72cd 100644 --- a/lib/fotoweb.js +++ b/lib/fotoweb.js @@ -16,18 +16,39 @@ const PHOTO_METADATA = [ const PHOTO_METADATA_KEYS = new Map(PHOTO_METADATA); const PHOTO_METADATA_IDS = new Map(PHOTO_METADATA.map(key => [...key].reverse())); +const QUERY_PARAMS = [ + ['p', 'page'], + ...PHOTO_METADATA, +]; + +const QUERY_PARAMS_KEYS = new Map(QUERY_PARAMS); +const QUERY_PARAMS_IDS = new Map(QUERY_PARAMS.map(key => [...key].reverse())); + const BASE_URL = process.env.FOTOWEB_BASE_URL; const API_PATH = process.env.FOTOWEB_API_PATH; const API_URL = [BASE_URL, API_PATH].join(''); const API_TOKEN = process.env.FOTOWEB_API_TOKEN; -const PAGING_REGEX = /p=([0-9]+)/; +const QUERY_PARAMS_REGEX = new RegExp( + `((${QUERY_PARAMS.map(key => key[0]).join('|')})=([^&]+))`, 'gi' +); +// Rebuild paging query string to use ID's from PHOTO_METADATA map const parsePagingLinks = (endpoint, pages) => { for (const key in pages) { // eslint-disable-line no-restricted-syntax if (pages[key]) { - if (PAGING_REGEX.test(pages[key])) { - pages[key] = `${endpoint}?page=${pages[key].match(PAGING_REGEX)[1]}`; + if (QUERY_PARAMS_REGEX.test(pages[key])) { + // Find all FotoWeb query keys in paging URL + const keyQueryParams = pages[key].match(QUERY_PARAMS_REGEX) || []; + const idQueryParams = keyQueryParams.map(item => { + const arr = item.split('='); + // Replace FotoWeb key with Skadi ID + // Example: ['25', 'Menneske'] => ['tags', 'Menneske'] + arr[0] = QUERY_PARAMS_KEYS.get(arr[0]); + return arr.join('='); + }); + + pages[key] = `${endpoint}?${idQueryParams.join('&')}`; } else { pages[key] = endpoint; } @@ -42,6 +63,8 @@ const parsePagingLinks = (endpoint, pages) => { module.exports = { PHOTO_METADATA_KEYS, PHOTO_METADATA_IDS, + QUERY_PARAMS_KEYS, + QUERY_PARAMS_IDS, BASE_URL, API_PATH, API_URL,