rubberguppe/routes/inbox.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

const express = require('express')
const router = express.Router()
2019-09-21 21:20:14 +00:00
const pub = require('../pub')
const net = require('../net')
const store = require('../store')
2018-09-15 07:01:19 +00:00
router.post('/', net.validators.activity, net.security.verifySignature, function (req, res) {
2019-09-22 05:20:37 +00:00
req.body._meta = { _target: pub.utils.usernameToIRI(req.user) }
2019-09-25 22:15:39 +00:00
console.log(req.body)
const toDo = {
saveActivity: true,
saveObject: false
}
// side effects
2019-09-22 05:20:37 +00:00
switch (req.body.type) {
case 'Accept':
// TODO - side effect necessary for following collection?
break
case 'Follow':
2019-09-25 22:15:39 +00:00
// TODO resolve object and ensure specified target matches inbox user
// req.body._meta._target = req.body.object.id
// send acceptance reply
pub.actor.getOrCreateActor(req.user, true)
.then(user => {
const to = [pub.utils.actorFromActivity(req.body)]
const accept = pub.activity.build('Accept', user.id, req.body, to)
return pub.activity.addToOutbox(user, accept)
})
.catch(e => console.log(e))
break
case 'Create':
2019-09-25 22:15:39 +00:00
toDo.saveObject = true
pub.actor.getOrCreateActor(req.user, true)
.then(user => {
const to = [user.followers]
const cc = [
pub.utils.actorFromActivity(req.body),
'https://www.w3.org/ns/activitystreams#Public'
]
2019-09-25 22:15:39 +00:00
const announce = pub.activity.build('Announce', user.id, req.body.object.id, to, cc)
return pub.activity.addToOutbox(user, announce)
}).catch(e => console.log(e))
break
2019-09-25 22:15:39 +00:00
case 'Undo':
pub.activity.undo(req.body.object, req.body.actor)
.catch(err => console.log(err))
break
}
const tasks = []
if (toDo.saveObject) {
tasks.push(pub.object.resolve(req.body.object))
}
if (toDo.saveActivity) {
tasks.push(store.stream.save(req.body))
}
2019-09-25 22:15:39 +00:00
Promise.all(tasks).then(() => res.status(200).send())
.catch(err => {
console.log(err)
res.status(500).send()
})
2019-09-22 05:20:37 +00:00
})
2018-09-15 07:01:19 +00:00
router.get('/', function (req, res) {
2019-09-22 05:20:37 +00:00
const db = req.app.get('db')
db.collection('streams')
2019-09-22 05:20:37 +00:00
.find({ '_meta._target': pub.utils.usernameToIRI(req.user) })
.sort({ _id: -1 })
.project({ _id: 0, _meta: 0, '@context': 0, 'object._id': 0, 'object.@context': 0, 'object._meta': 0 })
.toArray()
2019-09-21 21:20:14 +00:00
.then(stream => res.json(pub.utils.arrayToCollection(stream, true)))
.catch(err => {
console.log(err)
return res.status(500).send()
})
})
2019-09-22 05:20:37 +00:00
module.exports = router