ldn-inbox-server
Version:
A demonstration Event Notifications Inbox server
61 lines (51 loc) • 1.8 kB
JavaScript
const fs = require('fs');
const md5 = require('md5');
const logger = require('../../lib/util.js').getLogger();
/**
* Demonstration notification handler, that creates an 'Offer'
* notification for each incoming notificaation to request the
* archivation of the event log.
*/
async function handle({path,options,config}) {
if (! config) {
logger.error('no configuration found for offer_memento');
return { path, options, success: false };
}
if (!config['actor'] || !config['target']) {
logger.error('no actor/target entry');
return { path, options, success: false };
}
if (!options['artifact']) {
logger.info(`no artifact found (ignoring this request)`);
return { path, options, success: true };
}
if (!options['eventlog']) {
logger.info(`no artifact found (ignoring this request)`);
return { path, options, success: true };
}
try {
const data = JSON.stringify({
'@context': [
"https://www.w3.org/ns/activitystreams" ,
{"schema": "https://schema.org/"}
],
type: 'Offer',
actor: config['actor'],
object: {
id: options['eventlog']['id'],
type: [ "Document", "schema:Dataset" ]
},
target: config['target']
},null,4);
const outboxFile = options['outbox'] + '/' + md5(data) + '.jsonld';
logger.info(`storing Offer to ${outboxFile}`);
fs.writeFileSync(outboxFile,data);
return { path, options, success: true };
}
catch(e) {
logger.error(`failed to process ${path}`);
logger.error(e);
return { path, options, success: false };
}
}
module.exports = { handle };