2019-09-15 00:00:26 +00:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2019-09-13 01:03:04 +00:00
|
|
|
const utils = require('../utils')
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-09-15 00:00:26 +00:00
|
|
|
router.post('/', utils.validators.activity, function (req, res) {
|
2019-09-13 01:03:04 +00:00
|
|
|
const db = req.app.get('db');
|
|
|
|
req.body._target = req.user
|
2019-09-15 00:00:26 +00:00
|
|
|
Promise.all([
|
|
|
|
db.collection('objects').insertOne(req.body.object),
|
|
|
|
db.collection('streams').insertOne(req.body)
|
|
|
|
]).then(() => res.status(200).send())
|
2019-09-13 01:03:04 +00:00
|
|
|
.catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
res.status(500).send()
|
|
|
|
})
|
2018-09-15 07:01:19 +00:00
|
|
|
});
|
|
|
|
|
2019-09-15 00:00:26 +00:00
|
|
|
router.get('/', function (req, res) {
|
2019-09-13 01:03:04 +00:00
|
|
|
const db = req.app.get('db');
|
|
|
|
db.collection('streams')
|
2019-09-20 01:55:32 +00:00
|
|
|
.find({'_meta._target': utils.usernameToIRI(req.user)})
|
2019-09-13 01:03:04 +00:00
|
|
|
.sort({_id: -1})
|
2019-09-20 01:55:32 +00:00
|
|
|
.project({_id: 0, _meta: 0, '@context': 0, 'object._id': 0, 'object.@context': 0, 'object._meta': 0})
|
2019-09-13 01:03:04 +00:00
|
|
|
.toArray()
|
|
|
|
.then(stream => res.json(utils.arrayToCollection(stream, true)))
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
return res.status(500).send()
|
|
|
|
})
|
|
|
|
;
|
|
|
|
})
|
|
|
|
|
2018-09-15 07:01:19 +00:00
|
|
|
module.exports = router;
|