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.

42 lines
1008 B

  1. const Model = require('objection').Model
  2. /* global WIKI */
  3. /**
  4. * Navigation model
  5. */
  6. module.exports = class Navigation extends Model {
  7. static get tableName() { return 'navigation' }
  8. static get idColumn() { return 'key' }
  9. static get jsonSchema () {
  10. return {
  11. type: 'object',
  12. required: ['key'],
  13. properties: {
  14. key: {type: 'string'},
  15. config: {type: 'array', items: {type: 'object'}}
  16. }
  17. }
  18. }
  19. static async getTree({ cache = false } = {}) {
  20. if (cache) {
  21. const navTreeCached = await WIKI.redis.get('nav:sidebar')
  22. if (navTreeCached) {
  23. return JSON.parse(navTreeCached)
  24. }
  25. }
  26. const navTree = await WIKI.models.navigation.query().findOne('key', 'site')
  27. if (navTree) {
  28. if (cache) {
  29. await WIKI.redis.set('nav:sidebar', JSON.stringify(navTree.config), 'EX', 300)
  30. }
  31. return navTree.config
  32. } else {
  33. WIKI.logger.warn('Site Navigation is missing or corrupted.')
  34. return []
  35. }
  36. }
  37. }