43 lines
1.6 KiB
JavaScript
43 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
var db = require('../lib/db');
|
|
var jsonld = require('jsonld');
|
|
|
|
module.exports = {
|
|
/**
|
|
* @param {express.IRoute} routeObj
|
|
*/
|
|
route: (routeObj) => {
|
|
routeObj.post(async (req, res, _next) => {
|
|
if ( req.body ) {
|
|
var bodyParsed = await jsonld.compact(req.body, 'https://www.w3.org/ns/activitystreams');
|
|
if ( bodyParsed.type === 'Create') {
|
|
var to;
|
|
var cc;
|
|
if ( typeof bodyParsed.object.to === 'string' ) {
|
|
to = [bodyParsed.object.to];
|
|
} else {
|
|
to = bodyParsed.object.to;
|
|
}
|
|
if ( typeof bodyParsed.object.cc === 'string' ) {
|
|
to = [bodyParsed.object.cc];
|
|
} else {
|
|
cc = bodyParsed.object.cc;
|
|
}
|
|
await db('inbox').insert({
|
|
origin: bodyParsed.object.attributedTo,
|
|
to: JSON.stringify(to),
|
|
cc: JSON.stringify(cc),
|
|
object: JSON.stringify(
|
|
await jsonld.compact(
|
|
await jsonld.expand(bodyParsed)[0]['https://www.w3.org/ns/activitystreams#object'], 'https://www.w3.org/ns/activitystreams'
|
|
)
|
|
)
|
|
});
|
|
res.status(204);
|
|
res.end();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
};
|