rubberguppe/pub/federation.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict'
const request = require('request-promise-native')
const pubUtils = require('./utils')
// federation communication utilities
module.exports = {
requestObject,
deliver
}
function requestObject (id) {
2019-09-22 05:20:37 +00:00
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 => {
2019-09-25 22:15:39 +00:00
console.log(`delivering to ${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(addr, result.statusCode))
.catch(err => console.log(err))
})
return Promise.all(requests)
}