2019-09-21 21:20:14 +00:00
const crypto = require ( 'crypto' )
2019-09-22 05:20:37 +00:00
const { promisify } = require ( 'util' )
2019-09-21 21:20:14 +00:00
2019-09-22 05:05:30 +00:00
const store = require ( '../store' )
2019-09-21 21:20:14 +00:00
const pubUtils = require ( './utils' )
const config = require ( '../config.json' )
const generateKeyPairPromise = promisify ( crypto . generateKeyPair )
module . exports = {
2019-09-22 05:20:37 +00:00
createLocalActor ,
getOrCreateActor
2019-09-21 21:20:14 +00:00
}
function createLocalActor ( name , type ) {
2019-09-22 05:20:37 +00:00
return generateKeyPairPromise ( 'rsa' , {
modulusLength : 4096 ,
publicKeyEncoding : {
type : 'spki' ,
format : 'pem'
} ,
privateKeyEncoding : {
type : 'pkcs8' ,
format : 'pem'
}
} ) . then ( pair => {
const actorBase = pubUtils . 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 ,
2019-09-25 03:12:31 +00:00
name : ` ${ name } group ` ,
summary : ` I'm a group about ${ name } . Follow me to get all the group posts. Tag me to share with the group. Create other groups by searching for or tagging @yourGroupName@ ${ config . DOMAIN } ` ,
icon : ` https:// ${ config . DOMAIN } /f/guppe.png ` ,
2019-09-22 05:20:37 +00:00
publicKey : {
id : ` ${ actorBase } #main-key ` ,
owner : ` ${ actorBase } ` ,
publicKeyPem : pair . publicKey
}
}
} )
2019-09-21 21:20:14 +00:00
}
2019-09-22 05:05:30 +00:00
2019-09-23 17:52:22 +00:00
async function getOrCreateActor ( preferredUsername , includeMeta ) {
2019-09-22 05:20:37 +00:00
const id = pubUtils . usernameToIRI ( preferredUsername )
2019-09-23 17:52:22 +00:00
let user = await store . actor . getActor ( id , includeMeta )
2019-09-22 05:20:37 +00:00
if ( user ) {
2019-09-22 05:05:30 +00:00
return user
2019-09-22 05:20:37 +00:00
}
// auto create groups whenever an unknown actor is referenced
user = await createLocalActor ( preferredUsername , 'Group' )
2019-09-23 17:52:22 +00:00
await store . object . save ( user )
2019-09-22 05:20:37 +00:00
// only executed on success
delete user . _id
if ( includeMeta !== true ) {
delete user . _meta
}
return user
}