brainz-social-old/routes/inbox.js

57 lines
2.1 KiB
JavaScript

'use strict';
var db = require('../lib/db');
var express = require('express');
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 === 'string' ) {
res.status(422);
res.body('Unimplemented');
return res.end();
}
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);
return res.end();
} else if ( bodyParsed.type === 'Announce' ) {
res.status(422);
res.body('Unimplemented');
return res.end();
} else {
res.status(422);
res.body('Unimplemented');
return res.end();
}
}
});
}
};