From c170e41c6a7597ddb796ea2559bd29e8b522af5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ka=C3=9Fel?= Date: Sat, 2 May 2020 00:11:46 +0200 Subject: [PATCH] feat(server): add proper LDP urls --- lib/ldp.js | 22 +++++++++++----------- lib/server.js | 7 +++++-- lib/util.js | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index f97316b..141d9f9 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -2,7 +2,7 @@ const Boom = require('@hapi/boom') const etag = require('etag') class PagedCollection { - constructor(getPage, total, pageSize) { + constructor(getPage, notebookInfo, total, pageSize) { if (Array.isArray(getPage)) { const items = getPage this._getPage = () => items @@ -13,6 +13,7 @@ class PagedCollection { this.total = total this.pageSize = pageSize } + this.notebookInfo = notebookInfo } getPage(pageNumber) { @@ -31,19 +32,19 @@ function createPage(h, collection, pageNumber, iris) { return Boom.notFound() } + const items = collection.getPage(pageNumber) + const containerUrl = collection.notebookInfo.getContainerUrl() const page = { '@context': 'http://www.w3.org/ns/anno.jsonld', - id: `http://example.org/annotations/?iris=${ - iris ? 1 : 0 - }&page=${pageNumber}`, + id: `${containerUrl}/?page=${pageNumber}&iris=${iris ? 1 : 0}`, type: 'AnnotationPage', partOf: { - id: `http://example.org/annotations/?iris=${iris ? 1 : 0}`, + id: `${containerUrl}/?iris=${iris ? 1 : 0}`, total: collection.total, modified: '2016-07-20T12:00:00Z', }, startIndex: pageNumber === 0 ? 0 : collection.pageSize * pageNumber, - items: collection.getPage(pageNumber), + items: iris ? items.map(item => item.id) : items, } const response = h.response(page) @@ -56,21 +57,20 @@ function createPage(h, collection, pageNumber, iris) { } function createContainer(h, collection, iris) { + const containerUrl = collection.notebookInfo.getContainerUrl() const container = { '@context': [ 'http://www.w3.org/ns/anno.jsonld', 'http://www.w3.org/ns/ldp.jsonld', ], - id: 'http://example.org/annotations/?iris=1', + id: `${containerUrl}/?iris=${iris ? 1 : 0}`, type: ['BasicContainer', 'AnnotationCollection'], total: collection.total, modified: '2016-07-20T12:00:00Z', label: 'tbd', - first: `http://example.org/annotations/?iris=${iris ? 1 : 0}&page=0`, + first: `${containerUrl}/?iris=${iris ? 1 : 0}&page=0`, ...(collection.lastPage > 0 && { - last: `http://example.org/annotations/?iris=${iris ? 1 : 0}&page=${ - collection.lastPage - }`, + last: `${containerUrl}/?iris=${iris ? 1 : 0}&page=${collection.lastPage}`, }), } diff --git a/lib/server.js b/lib/server.js index aa1229b..b24b165 100644 --- a/lib/server.js +++ b/lib/server.js @@ -15,6 +15,7 @@ const { ERROR_BAD_REQUEST, } = require('./error') const { + NotebookInfo, encodeDocUrl, decodeDocUrl, normalizeAnnotation, @@ -67,18 +68,20 @@ async function createServer(backendSwarm, port, {host, ssl}) { method: 'GET', path: '/annotations/{container}', handler: async (request, h) => { + const docUrl = decodeDocUrl(request.params.container) const pageNumber = request.query.page ? Number.parseInt(request.query.page) : null const iris = request.query.iris === '1' + const notebookInfo = new NotebookInfo(host, ssl, docUrl) - const docUrl = decodeDocUrl(request.params.container) try { const annotations = await backendSwarm.getAnnotations(docUrl) const collection = new PagedCollection( annotations.map(annotation => denormalizeAnnotation(host, docUrl, annotation, {ssl}) - ) + ), + notebookInfo ) if (pageNumber !== null) { diff --git a/lib/util.js b/lib/util.js index 18c60eb..c50e4f1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,6 +2,20 @@ const encodeDocUrl = docUrl => Buffer.from(docUrl).toString('hex') const decodeDocUrl = encodedDocUrl => Buffer.from(encodedDocUrl, 'hex').toString() +class NotebookInfo { + constructor(host, ssl, docUrl) { + this.host = host + this.ssl = ssl + this.docUrl = docUrl + } + + getContainerUrl() { + return `${this.ssl ? 'https' : 'http'}://${ + this.host + }/annotations/${encodeDocUrl(this.docUrl)}` + } +} + function normalizeId(host, docUrl, annotationId, opts = {}) { const ssl = opts.ssl || false const pattern = new RegExp( @@ -33,6 +47,7 @@ const denormalizeAnnotation = (host, docUrl, annotation, opts = {}) => { } module.exports = { + NotebookInfo, normalizeId, normalizeAnnotation, denormalizeAnnotation,