2019-09-15 02:49:33 +00:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const {promisify} = require('util')
|
2019-09-21 21:20:14 +00:00
|
|
|
const utils = require('../utils')
|
2019-09-15 00:00:26 +00:00
|
|
|
const config = require('../config.json')
|
2019-09-21 21:20:14 +00:00
|
|
|
const db = require('../db')
|
|
|
|
const pub = require('../pub')
|
2019-09-13 01:03:04 +00:00
|
|
|
|
2019-09-21 21:20:14 +00:00
|
|
|
module.exports.consts = require('./consts')
|
2019-09-17 01:58:32 +00:00
|
|
|
|
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);
|
|
|
|
}
|
2019-09-15 00:00:26 +00:00
|
|
|
|
2019-09-21 20:02:50 +00:00
|
|
|
const actorProj = {_id: 0, _meta: 0}
|
|
|
|
const metaActorProj = {_id: 0}
|
|
|
|
async function getOrCreateActor(preferredUsername, db, includeMeta) {
|
2019-09-21 21:20:14 +00:00
|
|
|
const id = pub.utils.usernameToIRI(preferredUsername)
|
2019-09-17 01:58:32 +00:00
|
|
|
let user = await db.collection('objects')
|
|
|
|
.find({id: id})
|
|
|
|
.limit(1)
|
2019-09-21 20:02:50 +00:00
|
|
|
// strict comparison as we don't want to return private keys on accident
|
|
|
|
.project(includeMeta === true ? metaActorProj : actorProj)
|
2019-09-17 01:58:32 +00:00
|
|
|
.next()
|
|
|
|
if (user) {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
// auto create groups whenever an unknown actor is referenced
|
2019-09-21 21:20:14 +00:00
|
|
|
user = await pub.actor.createLocalActor(preferredUsername, 'Group')
|
2019-09-17 01:58:32 +00:00
|
|
|
await db.collection('objects').insertOne(user)
|
|
|
|
// only executed on success
|
|
|
|
delete user._id
|
2019-09-21 20:02:50 +00:00
|
|
|
if (includeMeta !== true) {
|
|
|
|
delete user._meta
|
|
|
|
}
|
2019-09-17 01:58:32 +00:00
|
|
|
return user
|
|
|
|
}
|
2019-09-21 21:20:14 +00:00
|
|
|
module.exports.getOrCreateActor = getOrCreateActor
|