2019-09-22 05:05:30 +00:00
|
|
|
'use strict'
|
|
|
|
const store = require('../store')
|
2019-09-22 05:20:37 +00:00
|
|
|
const federation = require('./federation')
|
2019-09-23 17:52:22 +00:00
|
|
|
const pubUtils = require('./utils')
|
2019-09-22 05:05:30 +00:00
|
|
|
module.exports = {
|
2019-09-23 17:52:22 +00:00
|
|
|
resolveObject,
|
|
|
|
resolve: resolveObject
|
2019-09-22 05:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find object in local DB or fetch from origin server
|
2019-09-23 17:52:22 +00:00
|
|
|
async function resolveObject (id) {
|
|
|
|
let object
|
|
|
|
if (pubUtils.validateObject(id)) {
|
|
|
|
// already an object
|
|
|
|
object = id
|
|
|
|
} else {
|
|
|
|
// resolve id to local object
|
|
|
|
object = await store.object.get(id)
|
|
|
|
if (object) {
|
|
|
|
return object
|
|
|
|
}
|
|
|
|
// resolve remote object from id
|
|
|
|
object = await federation.requestObject(id)
|
|
|
|
}
|
|
|
|
// cache non-collection objects
|
|
|
|
if (object.type !== 'Collection' && object.type !== 'OrderedCollection') {
|
|
|
|
await store.object.save(object)
|
2019-09-22 05:20:37 +00:00
|
|
|
}
|
|
|
|
return object
|
|
|
|
}
|