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')
|
2019-09-21 21:20:14 +00:00
|
|
|
const pub = require('../pub')
|
|
|
|
const net = require('../net')
|
2019-09-21 21:53:55 +00:00
|
|
|
const store = require('../store')
|
2019-09-21 20:02:50 +00:00
|
|
|
const request = require('request-promise-native')
|
|
|
|
const httpSignature = require('http-signature')
|
|
|
|
const {ObjectId} = require('mongodb')
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-09-21 21:20:14 +00:00
|
|
|
router.post('/', net.validators.activity, function (req, res) {
|
2019-09-13 01:03:04 +00:00
|
|
|
const db = req.app.get('db');
|
2019-09-21 20:02:50 +00:00
|
|
|
let outgoingResponse
|
2019-09-21 21:20:14 +00:00
|
|
|
req.body._meta = {_target: pub.utils.usernameToIRI(req.user)}
|
2019-09-21 20:02:50 +00:00
|
|
|
// side effects
|
|
|
|
switch(req.body.type) {
|
|
|
|
case 'Accept':
|
|
|
|
// workaround for node-http-signature#87
|
|
|
|
const tempUrl = req.url
|
|
|
|
req.url = req.originalUrl
|
|
|
|
sigHead = httpSignature.parse(req, {clockSkew: 3000})
|
|
|
|
req.url = tempUrl
|
|
|
|
// TODO - real key lookup with remote fetch
|
2019-09-21 21:53:55 +00:00
|
|
|
store.actor.getActor(sigHead.keyId.replace(/.*\//, ''), db)
|
2019-09-21 20:02:50 +00:00
|
|
|
.then(user => {
|
|
|
|
const valid = httpSignature.verifySignature(sigHead, user.publicKey.publicKeyPem)
|
|
|
|
console.log('key validation', valid)
|
|
|
|
return res.status(valid ? 200 : 401).send()
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'Follow':
|
|
|
|
req.body._meta._target = req.body.object.id
|
|
|
|
// send acceptance reply
|
|
|
|
Promise.all([
|
2019-09-21 21:53:55 +00:00
|
|
|
store.actor.getOrCreateActor(req.user, db, true),
|
2019-09-21 20:02:50 +00:00
|
|
|
request({
|
2019-09-21 21:20:14 +00:00
|
|
|
url: pub.utils.actorFromActivity(req.body),
|
2019-09-21 20:02:50 +00:00
|
|
|
headers: {Accept: 'application/activity+json'},
|
|
|
|
json: true,
|
|
|
|
})
|
|
|
|
])
|
|
|
|
.then(([user, actor]) => {
|
|
|
|
if (!actor || !actor.inbox) {
|
|
|
|
throw new Error('unable to send follow request acceptance: actor inbox not retrievable')
|
|
|
|
}
|
|
|
|
const newID = new ObjectId()
|
|
|
|
const responseOpts = {
|
|
|
|
method: 'POST',
|
|
|
|
url: actor.inbox,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/activity+json',
|
|
|
|
},
|
|
|
|
httpSignature: {
|
|
|
|
key: user._meta.privateKey,
|
|
|
|
keyId: user.id,
|
|
|
|
headers: ['(request-target)', 'host', 'date'],
|
|
|
|
},
|
|
|
|
json: true,
|
2019-09-21 21:20:14 +00:00
|
|
|
body: pub.utils.toJSONLD({
|
2019-09-21 20:02:50 +00:00
|
|
|
_id: newID,
|
|
|
|
type: 'Accept',
|
|
|
|
id: `https://${req.app.get('domain')}/o/${newID.toHexString()}`,
|
|
|
|
actor: user.id,
|
|
|
|
object: req.body,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
return request(responseOpts)
|
|
|
|
})
|
|
|
|
.then(result => console.log('success', result))
|
|
|
|
.catch(e => console.log(e))
|
|
|
|
break
|
|
|
|
}
|
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-21 21:20:14 +00:00
|
|
|
.find({'_meta._target': pub.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()
|
2019-09-21 21:20:14 +00:00
|
|
|
.then(stream => res.json(pub.utils.arrayToCollection(stream, true)))
|
2019-09-13 01:03:04 +00:00
|
|
|
.catch(err => {
|
|
|
|
console.log(err)
|
|
|
|
return res.status(500).send()
|
|
|
|
})
|
|
|
|
;
|
|
|
|
})
|
|
|
|
|
2018-09-15 07:01:19 +00:00
|
|
|
module.exports = router;
|