wikijs-fork/server/models/navigation.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

const Model = require('objection').Model
2020-02-02 21:26:44 +00:00
const _ = require('lodash')
/* 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'}}
}
}
}
2020-02-02 21:26:44 +00:00
static async getTree({ cache = false, locale = 'en' } = {}) {
2018-11-11 18:54:33 +00:00
if (cache) {
2020-02-02 21:26:44 +00:00
const navTreeCached = await WIKI.cache.get(`nav:sidebar:${locale}`)
2018-11-11 18:54:33 +00:00
if (navTreeCached) {
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) {
2020-02-02 21:26:44 +00:00
// Check for pre-2.1 format
if (_.has(navTree.config[0], 'kind')) {
navTree.config = [{
locale: 'en',
items: navTree.config
}]
2018-11-11 18:54:33 +00:00
}
2020-02-02 21:26:44 +00:00
for (const tree of navTree.config) {
if (cache) {
await WIKI.cache.set(`nav:sidebar:${tree.locale}`, tree.items, 300)
}
}
return locale === 'all' ? navTree.config : WIKI.cache.get(`nav:sidebar:${locale}`)
2018-10-22 02:25:17 +00:00
} else {
WIKI.logger.warn('Site Navigation is missing or corrupted.')
return []
}
}
}