2019-09-15 02:49:33 +00:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const {promisify} = require('util')
|
|
|
|
const {ASContext} = require('./consts')
|
2019-09-15 00:00:26 +00:00
|
|
|
module.exports.validators = require('./validators');
|
|
|
|
const config = require('../config.json')
|
2019-09-13 01:03:04 +00:00
|
|
|
|
|
|
|
function isObject(value) {
|
|
|
|
return value && typeof value === 'object' && value.constructor === Object
|
|
|
|
}
|
|
|
|
// outtermost closure starts the recursion counter
|
|
|
|
// const level = 0;
|
|
|
|
function traverseObject(obj, f) {
|
|
|
|
const traverse = o => {
|
|
|
|
// const level = level + 1
|
|
|
|
// if (level > 5) return o
|
|
|
|
traverseObject(o, f)
|
|
|
|
}
|
|
|
|
if (!isObject(obj)) return obj;
|
|
|
|
Object.keys(obj).forEach(traverse)
|
|
|
|
return f(obj);
|
|
|
|
}
|
|
|
|
module.exports.toJSONLD = function (obj) {
|
2019-09-15 00:00:26 +00:00
|
|
|
obj['@context'] = obj['@context'] || ASContext;
|
2019-09-13 01:03:04 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.arrayToCollection = function (arr, ordered) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
'@context': ASContext,
|
|
|
|
totalItems: arr.length,
|
|
|
|
type: ordered ? 'orderedCollection' : 'collection',
|
|
|
|
[ordered ? 'orderedItems' : 'items']: arr,
|
|
|
|
}
|
2019-09-15 00:00:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 02:49:33 +00:00
|
|
|
function userNameToIRI (user) {
|
2019-09-15 00:00:26 +00:00
|
|
|
return `https://${config.DOMAIN}/u/${user}`
|
2019-09-15 02:49:33 +00:00
|
|
|
}
|
|
|
|
module.exports.userNameToIRI = userNameToIRI
|
|
|
|
|
|
|
|
const generateKeyPairPromise = promisify(crypto.generateKeyPair)
|
|
|
|
module.exports.createLocalActor = function (name, type) {
|
|
|
|
return generateKeyPairPromise('rsa', {
|
|
|
|
modulusLength: 4096,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem'
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: 'aes-256-cbc',
|
|
|
|
passphrase: config.KEYPASS
|
|
|
|
}
|
|
|
|
}).then(pair => {
|
|
|
|
const actorBase = userNameToIRI(name);
|
|
|
|
return {
|
|
|
|
_meta: {
|
|
|
|
privateKey: pair.privateKey,
|
|
|
|
},
|
|
|
|
id: `${actorBase}`,
|
|
|
|
"type": type,
|
|
|
|
"following": `${actorBase}/following`,
|
|
|
|
"followers": `${actorBase}/followers`,
|
|
|
|
"liked": `${actorBase}/liked`,
|
|
|
|
"inbox": `${actorBase}/inbox`,
|
|
|
|
"outbox": `${actorBase}/outbox`,
|
|
|
|
"preferredUsername": name,
|
|
|
|
"name": "Dummy Person",
|
|
|
|
"summary": "Gotta have someone in the db",
|
|
|
|
"icon": `http://${config.DOMAIN}/f/${name}.png`,
|
|
|
|
attachment: [
|
|
|
|
`http://${config.DOMAIN}/f/${name}.glb`
|
|
|
|
],
|
|
|
|
publicKey: {
|
|
|
|
'id': `${actorBase}#main-key`,
|
|
|
|
'owner': `${actorBase}`,
|
|
|
|
'publicKeyPem': pair.publicKey
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|