|
1 | 1 | module.exports = handler
|
2 | 2 |
|
| 3 | +const libPath = require('path/posix') |
| 4 | + |
| 5 | +const headerTemplate = require('express-prep/templates').header |
| 6 | +const solidRDFTemplate = require('../rdf-notification-template') |
| 7 | + |
| 8 | +const ALLOWED_RDF_MIME_TYPES = [ |
| 9 | + 'application/ld+json', |
| 10 | + 'application/activity+json', |
| 11 | + 'text/turtle' |
| 12 | +] |
| 13 | + |
| 14 | +function getActivity (method) { |
| 15 | + if (method === 'DELETE') { |
| 16 | + return 'Delete' |
| 17 | + } |
| 18 | + return 'Update' |
| 19 | +} |
| 20 | + |
| 21 | +function getParentActivity (method, status) { |
| 22 | + if (method === 'DELETE') { |
| 23 | + return 'Remove' |
| 24 | + } |
| 25 | + if (status === 201) { |
| 26 | + return 'Add' |
| 27 | + } |
| 28 | + return 'Update' |
| 29 | +} |
| 30 | + |
3 | 31 | function handler (req, res, next) {
|
4 |
| - res.events.prep.trigger({ |
5 |
| - generateNotifications () { |
6 |
| - return res.events.prep.defaultNotification({ |
7 |
| - ...(res.method === 'POST') && { location: res.getHeader('Content-Location') } |
8 |
| - }) |
9 |
| - } |
10 |
| - }) |
| 32 | + const { trigger, defaultNotification } = res.events.prep |
| 33 | + |
| 34 | + const { method } = req |
| 35 | + const { statusCode } = res |
| 36 | + const eventID = res.getHeader('event-id') |
| 37 | + |
| 38 | + const parent = `${libPath.dirname(req.path)}/` |
| 39 | + const parentID = res.setEventID(parent) |
| 40 | + const fullUrl = new URL(req.path, `${req.protocol}://${req.hostname}/`) |
| 41 | + const parentUrl = new URL(parent, fullUrl) |
| 42 | + |
| 43 | + // Date is a hack since node does not seem to provide access to send date. |
| 44 | + // Date needs to be shared with parent notification |
| 45 | + const eventDate = res._header.match(/^Date: (.*?)$/m)?.[1] || |
| 46 | + new Date().toUTCString() |
| 47 | + |
| 48 | + // If the resource itself newly created, |
| 49 | + // it could not have been subscribed for notifications already |
| 50 | + if (!((method === 'PUT' || method === 'PATCH') && statusCode === 201)) { |
| 51 | + trigger({ |
| 52 | + generateNotification ( |
| 53 | + negotiatedFields |
| 54 | + ) { |
| 55 | + const mediaType = negotiatedFields['content-type'] |
| 56 | + |
| 57 | + if (ALLOWED_RDF_MIME_TYPES.includes(mediaType?.[0])) { |
| 58 | + return `${headerTemplate(negotiatedFields)}\r\n${solidRDFTemplate({ |
| 59 | + activity: getActivity(method), |
| 60 | + eventID, |
| 61 | + object: String(fullUrl), |
| 62 | + date: eventDate, |
| 63 | + // We use eTag as a proxy for state for now |
| 64 | + state: res.getHeader('ETag'), |
| 65 | + mediaType |
| 66 | + })}` |
| 67 | + } else { |
| 68 | + return defaultNotification({ |
| 69 | + ...(res.method === 'POST') && { location: res.getHeader('Content-Location') } |
| 70 | + }) |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + // Write a notification to parent container |
| 77 | + // POST in Solid creates a child resource |
| 78 | + if (method !== 'POST') { |
| 79 | + trigger({ |
| 80 | + path: parent, |
| 81 | + generateNotification ( |
| 82 | + negotiatedFields |
| 83 | + ) { |
| 84 | + const mediaType = negotiatedFields['content-type'] |
| 85 | + if (ALLOWED_RDF_MIME_TYPES.includes(mediaType?.[0])) { |
| 86 | + return `${headerTemplate(negotiatedFields)}\r\n${solidRDFTemplate({ |
| 87 | + activity: getParentActivity(method, statusCode), |
| 88 | + eventID: parentID, |
| 89 | + date: eventDate, |
| 90 | + object: String(parentUrl), |
| 91 | + target: statusCode === 201 ? String(fullUrl) : undefined, |
| 92 | + eTag: undefined, |
| 93 | + mediaType |
| 94 | + })}` |
| 95 | + } |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | + |
11 | 100 | next()
|
12 | 101 | }
|
0 commit comments