rubberguppe/pub/federation.js

49 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 => {
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)
})
2019-09-28 15:23:24 +00:00
.then(result => console.log('delivery:', addr, result.statusCode))
.catch(err => console.log(err))
})
return Promise.all(requests)
}