mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.9 KiB
66 lines
1.9 KiB
2 years ago
|
import { Model } from 'objection'
|
||
|
import { has } from 'lodash-es'
|
||
6 years ago
|
|
||
|
/**
|
||
|
* Navigation model
|
||
|
*/
|
||
2 years ago
|
export class Navigation extends Model {
|
||
6 years ago
|
static get tableName() { return 'navigation' }
|
||
|
static get idColumn() { return 'key' }
|
||
|
|
||
|
static get jsonSchema () {
|
||
|
return {
|
||
|
type: 'object',
|
||
|
required: ['key'],
|
||
|
|
||
|
properties: {
|
||
|
key: {type: 'string'},
|
||
6 years ago
|
config: {type: 'array', items: {type: 'object'}}
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
static async getTree({ cache = false, locale = 'en', groups = [], bypassAuth = false } = {}) {
|
||
6 years ago
|
if (cache) {
|
||
5 years ago
|
const navTreeCached = await WIKI.cache.get(`nav:sidebar:${locale}`)
|
||
6 years ago
|
if (navTreeCached) {
|
||
2 years ago
|
return bypassAuth ? navTreeCached : WIKI.db.navigation.getAuthorizedItems(navTreeCached, groups)
|
||
6 years ago
|
}
|
||
|
}
|
||
2 years ago
|
const navTree = await WIKI.db.navigation.query().findOne('key', `site`)
|
||
6 years ago
|
if (navTree) {
|
||
5 years ago
|
// Check for pre-2.3 format
|
||
2 years ago
|
if (has(navTree.config[0], 'kind')) {
|
||
5 years ago
|
navTree.config = [{
|
||
|
locale: 'en',
|
||
5 years ago
|
items: navTree.config.map(item => ({
|
||
|
...item,
|
||
|
visibilityMode: 'all',
|
||
|
visibilityGroups: []
|
||
|
}))
|
||
5 years ago
|
}]
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
for (const tree of navTree.config) {
|
||
|
if (cache) {
|
||
|
await WIKI.cache.set(`nav:sidebar:${tree.locale}`, tree.items, 300)
|
||
|
}
|
||
|
}
|
||
5 years ago
|
if (bypassAuth) {
|
||
|
return locale === 'all' ? navTree.config : WIKI.cache.get(`nav:sidebar:${locale}`)
|
||
|
} else {
|
||
2 years ago
|
return locale === 'all' ? WIKI.db.navigation.getAuthorizedItems(navTree.config, groups) : WIKI.db.navigation.getAuthorizedItems(WIKI.cache.get(`nav:sidebar:${locale}`), groups)
|
||
5 years ago
|
}
|
||
6 years ago
|
} else {
|
||
|
WIKI.logger.warn('Site Navigation is missing or corrupted.')
|
||
|
return []
|
||
|
}
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
static getAuthorizedItems(tree = [], groups = []) {
|
||
2 years ago
|
return tree.filter(leaf => {
|
||
5 years ago
|
return leaf.visibilityMode === 'all' || _.intersection(leaf.visibilityGroups, groups).length > 0
|
||
|
})
|
||
|
}
|
||
6 years ago
|
}
|