Skip to content

Commit

Permalink
feat(server): add proper LDP urls
Browse files Browse the repository at this point in the history
  • Loading branch information
snvfk1n committed May 1, 2020
1 parent fd1766f commit c170e41
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
22 changes: 11 additions & 11 deletions lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +13,7 @@ class PagedCollection {
this.total = total
this.pageSize = pageSize
}
this.notebookInfo = notebookInfo
}

getPage(pageNumber) {
Expand All @@ -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)
Expand All @@ -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}`,
}),
}

Expand Down
7 changes: 5 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
ERROR_BAD_REQUEST,
} = require('./error')
const {
NotebookInfo,
encodeDocUrl,
decodeDocUrl,
normalizeAnnotation,
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -33,6 +47,7 @@ const denormalizeAnnotation = (host, docUrl, annotation, opts = {}) => {
}

module.exports = {
NotebookInfo,
normalizeId,
normalizeAnnotation,
denormalizeAnnotation,
Expand Down

0 comments on commit c170e41

Please sign in to comment.