-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Solid/Activity Streams format notifications: + Provides notifictions in JSON-LD and Turtle. + Extends notifications to PUT and POST methods. + Add Event-ID header field to the response of a write method.
- Loading branch information
Showing
8 changed files
with
179 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,101 @@ | ||
module.exports = handler | ||
|
||
const libPath = require('path/posix') | ||
|
||
const headerTemplate = require('express-prep/templates').header | ||
const solidRDFTemplate = require('../rdf-notification-template') | ||
|
||
const ALLOWED_RDF_MIME_TYPES = [ | ||
'application/ld+json', | ||
'application/activity+json', | ||
'text/turtle' | ||
] | ||
|
||
function getActivity (method) { | ||
if (method === 'DELETE') { | ||
return 'Delete' | ||
} | ||
return 'Update' | ||
} | ||
|
||
function getParentActivity (method, status) { | ||
if (method === 'DELETE') { | ||
return 'Remove' | ||
} | ||
if (status === 201) { | ||
return 'Add' | ||
} | ||
return 'Update' | ||
} | ||
|
||
function handler (req, res, next) { | ||
res.events.prep.trigger({ | ||
generateNotifications () { | ||
return res.events.prep.defaultNotification({ | ||
...(res.method === 'POST') && { location: res.getHeader('Content-Location') } | ||
}) | ||
} | ||
}) | ||
const { trigger, defaultNotification } = res.events.prep | ||
|
||
const { method } = req | ||
const { statusCode } = res | ||
const eventID = res.getHeader('event-id') | ||
|
||
const parent = `${libPath.dirname(req.path)}/` | ||
const parentID = res.setEventID(parent) | ||
const fullUrl = new URL(req.path, `${req.protocol}://${req.hostname}/`) | ||
const parentUrl = new URL(parent, fullUrl) | ||
|
||
// Date is a hack since node does not seem to provide access to send date. | ||
// Date needs to be shared with parent notification | ||
const eventDate = res._header.match(/^Date: (.*?)$/m)?.[1] || | ||
new Date().toUTCString() | ||
|
||
// If the resource itself newly created, | ||
// it could not have been subscribed for notifications already | ||
if (!((method === 'PUT' || method === 'PATCH') && statusCode === 201)) { | ||
trigger({ | ||
generateNotification ( | ||
negotiatedFields | ||
) { | ||
const mediaType = negotiatedFields['content-type'] | ||
|
||
if (ALLOWED_RDF_MIME_TYPES.includes(mediaType?.[0])) { | ||
return `${headerTemplate(negotiatedFields)}\r\n${solidRDFTemplate({ | ||
activity: getActivity(method), | ||
eventID, | ||
object: String(fullUrl), | ||
date: eventDate, | ||
// We use eTag as a proxy for state for now | ||
state: res.getHeader('ETag'), | ||
mediaType | ||
})}` | ||
} else { | ||
return defaultNotification({ | ||
...(res.method === 'POST') && { location: res.getHeader('Content-Location') } | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// Write a notification to parent container | ||
// POST in Solid creates a child resource | ||
if (method !== 'POST') { | ||
trigger({ | ||
path: parent, | ||
generateNotification ( | ||
negotiatedFields | ||
) { | ||
const mediaType = negotiatedFields['content-type'] | ||
if (ALLOWED_RDF_MIME_TYPES.includes(mediaType?.[0])) { | ||
return `${headerTemplate(negotiatedFields)}\r\n${solidRDFTemplate({ | ||
activity: getParentActivity(method, statusCode), | ||
eventID: parentID, | ||
date: eventDate, | ||
object: String(parentUrl), | ||
target: statusCode === 201 ? String(fullUrl) : undefined, | ||
eTag: undefined, | ||
mediaType | ||
})}` | ||
} | ||
} | ||
}) | ||
} | ||
|
||
next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const CONTEXT_ACTIVITYSTREAMS = 'https://www.w3.org/ns/activitystreams' | ||
const CONTEXT_NOTIFICATION = 'https://www.w3.org/ns/solid/notification/v1' | ||
const CONTEXT_XML_SCHEMA = 'http://www.w3.org/2001/XMLSchema' | ||
|
||
function generateJSONNotification ({ | ||
activity: type, | ||
eventId: id, | ||
date: published, | ||
object, | ||
target, | ||
state = undefined | ||
}) { | ||
return { | ||
published, | ||
type, | ||
id, | ||
object, | ||
...(type === 'Add') && { target }, | ||
...(type === 'Remove') && { origin: target }, | ||
...(state) && { state } | ||
} | ||
} | ||
|
||
function generateTurtleNotification ({ | ||
activity, | ||
eventId, | ||
date, | ||
object, | ||
target, | ||
state = undefined | ||
}) { | ||
const stateLine = `\n notify:state "${state}" ;` | ||
|
||
return `@prefix as: <${CONTEXT_ACTIVITYSTREAMS}#> . | ||
@prefix notify: <${CONTEXT_NOTIFICATION}#> . | ||
@prefix xsd: <${CONTEXT_XML_SCHEMA}#> . | ||
<${eventId}> a as:${activity} ;${state && stateLine} | ||
as:object ${object} ; | ||
as:published "${date}"^^xsd:dateTime .`.replaceAll('\n', '\r\n') | ||
} | ||
|
||
function serializeToJSONLD (notification, isActivityStreams = false) { | ||
notification['@context'] = [CONTEXT_NOTIFICATION] | ||
if (!isActivityStreams) { | ||
notification['@context'].unshift(CONTEXT_ACTIVITYSTREAMS) | ||
} | ||
return JSON.stringify(notification, null, 2) | ||
} | ||
|
||
function rdfTemplate (props) { | ||
const { mediaType } = props | ||
if (mediaType[0] === 'application/activity+json' || (mediaType[0] === 'application/ld+json' && mediaType[1].get('profile')?.toLowerCase() === 'https://www.w3.org/ns/activitystreams')) { | ||
return serializeToJSONLD(generateJSONNotification(props), true) | ||
} | ||
|
||
if (mediaType[0] === 'application/ld+json') { | ||
return serializeToJSONLD(generateJSONNotification(props)) | ||
} | ||
|
||
if (mediaType[0] === 'text/turtle') { | ||
return generateTurtleNotification(props) | ||
} | ||
} | ||
|
||
module.exports = rdfTemplate |