2019-09-15 00:00:26 +00:00
|
|
|
const {ObjectId} = require('mongodb')
|
|
|
|
// const activities = ['Create', ]
|
2019-09-21 21:20:14 +00:00
|
|
|
const pub = require('../pub')
|
2019-09-15 00:00:26 +00:00
|
|
|
|
|
|
|
function validateObject (object) {
|
|
|
|
if (object && object.id) {
|
2019-09-21 21:20:14 +00:00
|
|
|
object['@context'] = object['@context'] || pub.consts.ASContext
|
2019-09-15 00:00:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateActivity (object) {
|
|
|
|
if (object && object.id && object.actor) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.activity = function activity (req, res, next) {
|
|
|
|
// TODO real validation
|
|
|
|
if (!validateActivity(req.body)) {
|
|
|
|
return res.status(400).send('Invalid activity')
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.outboxActivity = function outboxActivity (req, res, next) {
|
|
|
|
if (!validateActivity(req.body)) {
|
|
|
|
if (!validateObject(req.body)) {
|
|
|
|
return res.status(400).send('Invalid activity')
|
|
|
|
}
|
2019-09-17 01:58:32 +00:00
|
|
|
const newID = new ObjectId()
|
2019-09-15 00:00:26 +00:00
|
|
|
req.body = {
|
|
|
|
_id: newID,
|
2019-09-21 21:20:14 +00:00
|
|
|
'@context': pub.consts.ASContext,
|
2019-09-15 00:00:26 +00:00
|
|
|
type: 'Create',
|
2019-09-20 01:55:32 +00:00
|
|
|
id: `https://${req.app.get('domain')}/o/${newID.toHexString()}`,
|
2019-09-15 00:00:26 +00:00
|
|
|
actor: req.body.attributedTo,
|
|
|
|
object: req.body,
|
|
|
|
published: new Date().toISOString(),
|
|
|
|
to: req.body.to,
|
|
|
|
cc: req.body.cc,
|
|
|
|
bcc: req.body.cc,
|
|
|
|
audience: req.body.audience,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|