2018-09-29 19:39:09 +00:00
|
|
|
const Model = require('objection').Model
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigation model
|
|
|
|
*/
|
|
|
|
module.exports = class Navigation extends Model {
|
|
|
|
static get tableName() { return 'navigation' }
|
|
|
|
static get idColumn() { return 'key' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['key'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
key: {type: 'string'},
|
2018-10-22 02:25:17 +00:00
|
|
|
config: {type: 'array', items: {type: 'object'}}
|
2018-09-29 19:39:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:54:33 +00:00
|
|
|
static async getTree({ cache = false } = {}) {
|
|
|
|
if (cache) {
|
2019-02-13 22:20:46 +00:00
|
|
|
const navTreeCached = await WIKI.cache.get('nav:sidebar')
|
2018-11-11 18:54:33 +00:00
|
|
|
if (navTreeCached) {
|
2019-02-13 22:20:46 +00:00
|
|
|
return navTreeCached
|
2018-11-11 18:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-22 02:25:17 +00:00
|
|
|
const navTree = await WIKI.models.navigation.query().findOne('key', 'site')
|
|
|
|
if (navTree) {
|
2018-11-11 18:54:33 +00:00
|
|
|
if (cache) {
|
2019-02-13 22:20:46 +00:00
|
|
|
await WIKI.cache.set('nav:sidebar', navTree.config, 300)
|
2018-11-11 18:54:33 +00:00
|
|
|
}
|
2018-10-22 02:25:17 +00:00
|
|
|
return navTree.config
|
|
|
|
} else {
|
|
|
|
WIKI.logger.warn('Site Navigation is missing or corrupted.')
|
|
|
|
return []
|
|
|
|
}
|
2018-09-29 19:39:09 +00:00
|
|
|
}
|
|
|
|
}
|