Skip to content

Commit c170e41

Browse files
committed
feat(server): add proper LDP urls
1 parent fd1766f commit c170e41

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

lib/ldp.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const Boom = require('@hapi/boom')
22
const etag = require('etag')
33

44
class PagedCollection {
5-
constructor(getPage, total, pageSize) {
5+
constructor(getPage, notebookInfo, total, pageSize) {
66
if (Array.isArray(getPage)) {
77
const items = getPage
88
this._getPage = () => items
@@ -13,6 +13,7 @@ class PagedCollection {
1313
this.total = total
1414
this.pageSize = pageSize
1515
}
16+
this.notebookInfo = notebookInfo
1617
}
1718

1819
getPage(pageNumber) {
@@ -31,19 +32,19 @@ function createPage(h, collection, pageNumber, iris) {
3132
return Boom.notFound()
3233
}
3334

35+
const items = collection.getPage(pageNumber)
36+
const containerUrl = collection.notebookInfo.getContainerUrl()
3437
const page = {
3538
'@context': 'http://www.w3.org/ns/anno.jsonld',
36-
id: `http://example.org/annotations/?iris=${
37-
iris ? 1 : 0
38-
}&page=${pageNumber}`,
39+
id: `${containerUrl}/?page=${pageNumber}&iris=${iris ? 1 : 0}`,
3940
type: 'AnnotationPage',
4041
partOf: {
41-
id: `http://example.org/annotations/?iris=${iris ? 1 : 0}`,
42+
id: `${containerUrl}/?iris=${iris ? 1 : 0}`,
4243
total: collection.total,
4344
modified: '2016-07-20T12:00:00Z',
4445
},
4546
startIndex: pageNumber === 0 ? 0 : collection.pageSize * pageNumber,
46-
items: collection.getPage(pageNumber),
47+
items: iris ? items.map(item => item.id) : items,
4748
}
4849

4950
const response = h.response(page)
@@ -56,21 +57,20 @@ function createPage(h, collection, pageNumber, iris) {
5657
}
5758

5859
function createContainer(h, collection, iris) {
60+
const containerUrl = collection.notebookInfo.getContainerUrl()
5961
const container = {
6062
'@context': [
6163
'http://www.w3.org/ns/anno.jsonld',
6264
'http://www.w3.org/ns/ldp.jsonld',
6365
],
64-
id: 'http://example.org/annotations/?iris=1',
66+
id: `${containerUrl}/?iris=${iris ? 1 : 0}`,
6567
type: ['BasicContainer', 'AnnotationCollection'],
6668
total: collection.total,
6769
modified: '2016-07-20T12:00:00Z',
6870
label: 'tbd',
69-
first: `http://example.org/annotations/?iris=${iris ? 1 : 0}&page=0`,
71+
first: `${containerUrl}/?iris=${iris ? 1 : 0}&page=0`,
7072
...(collection.lastPage > 0 && {
71-
last: `http://example.org/annotations/?iris=${iris ? 1 : 0}&page=${
72-
collection.lastPage
73-
}`,
73+
last: `${containerUrl}/?iris=${iris ? 1 : 0}&page=${collection.lastPage}`,
7474
}),
7575
}
7676

lib/server.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
ERROR_BAD_REQUEST,
1616
} = require('./error')
1717
const {
18+
NotebookInfo,
1819
encodeDocUrl,
1920
decodeDocUrl,
2021
normalizeAnnotation,
@@ -67,18 +68,20 @@ async function createServer(backendSwarm, port, {host, ssl}) {
6768
method: 'GET',
6869
path: '/annotations/{container}',
6970
handler: async (request, h) => {
71+
const docUrl = decodeDocUrl(request.params.container)
7072
const pageNumber = request.query.page
7173
? Number.parseInt(request.query.page)
7274
: null
7375
const iris = request.query.iris === '1'
76+
const notebookInfo = new NotebookInfo(host, ssl, docUrl)
7477

75-
const docUrl = decodeDocUrl(request.params.container)
7678
try {
7779
const annotations = await backendSwarm.getAnnotations(docUrl)
7880
const collection = new PagedCollection(
7981
annotations.map(annotation =>
8082
denormalizeAnnotation(host, docUrl, annotation, {ssl})
81-
)
83+
),
84+
notebookInfo
8285
)
8386

8487
if (pageNumber !== null) {

lib/util.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ const encodeDocUrl = docUrl => Buffer.from(docUrl).toString('hex')
22
const decodeDocUrl = encodedDocUrl =>
33
Buffer.from(encodedDocUrl, 'hex').toString()
44

5+
class NotebookInfo {
6+
constructor(host, ssl, docUrl) {
7+
this.host = host
8+
this.ssl = ssl
9+
this.docUrl = docUrl
10+
}
11+
12+
getContainerUrl() {
13+
return `${this.ssl ? 'https' : 'http'}://${
14+
this.host
15+
}/annotations/${encodeDocUrl(this.docUrl)}`
16+
}
17+
}
18+
519
function normalizeId(host, docUrl, annotationId, opts = {}) {
620
const ssl = opts.ssl || false
721
const pattern = new RegExp(
@@ -33,6 +47,7 @@ const denormalizeAnnotation = (host, docUrl, annotation, opts = {}) => {
3347
}
3448

3549
module.exports = {
50+
NotebookInfo,
3651
normalizeId,
3752
normalizeAnnotation,
3853
denormalizeAnnotation,

0 commit comments

Comments
 (0)