rubberguppe/pub/federation.js
2019-09-28 10:23:24 -05:00

48 lines
1.1 KiB
JavaScript

'use strict'
const request = require('request-promise-native')
const pubUtils = require('./utils')
// federation communication utilities
module.exports = {
requestObject,
deliver
}
function requestObject (id) {
return request({
url: id,
headers: { Accept: 'application/activity+json' },
json: true
})
}
function deliver (actor, activity, addresses) {
if (activity.bto) {
delete activity.bto
}
if (activity.bcc) {
delete activity.bcc
}
const requests = addresses.map(addr => {
return request({
method: 'POST',
url: addr,
headers: {
'Content-Type': 'application/activity+json'
},
httpSignature: {
key: actor._meta.privateKey,
keyId: actor.id,
headers: ['(request-target)', 'host', 'date'],
authorizationHeaderName: 'Signature'
},
json: true,
resolveWithFullResponse: true,
simple: false,
body: pubUtils.toJSONLD(activity)
})
.then(result => console.log('delivery:', addr, result.statusCode))
.catch(err => console.log(err))
})
return Promise.all(requests)
}