editors.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const Model = require('objection').Model
  2. const autoload = require('auto-load')
  3. const path = require('path')
  4. const _ = require('lodash')
  5. /* global WIKI */
  6. /**
  7. * Editor model
  8. */
  9. module.exports = class Editor extends Model {
  10. static get tableName() { return 'editors' }
  11. static get jsonSchema () {
  12. return {
  13. type: 'object',
  14. required: ['key', 'title', 'isEnabled'],
  15. properties: {
  16. id: {type: 'integer'},
  17. key: {type: 'string'},
  18. title: {type: 'string'},
  19. isEnabled: {type: 'boolean'},
  20. config: {type: 'object'}
  21. }
  22. }
  23. }
  24. static async getEnabledEditors() {
  25. return WIKI.db.editors.query().where({ isEnabled: true })
  26. }
  27. static async refreshEditorsFromDisk() {
  28. try {
  29. const dbEditors = await WIKI.db.editors.query()
  30. const diskEditors = autoload(path.join(WIKI.SERVERPATH, 'modules/editor'))
  31. let newEditors = []
  32. _.forOwn(diskEditors, (strategy, strategyKey) => {
  33. if (!_.some(dbEditors, ['key', strategy.key])) {
  34. newEditors.push({
  35. key: strategy.key,
  36. title: strategy.title,
  37. isEnabled: false,
  38. config: _.reduce(strategy.props, (result, value, key) => {
  39. _.set(result, value, '')
  40. return result
  41. }, {})
  42. })
  43. }
  44. })
  45. if (newEditors.length > 0) {
  46. await WIKI.db.editors.query().insert(newEditors)
  47. WIKI.logger.info(`Loaded ${newEditors.length} new editors: [ OK ]`)
  48. } else {
  49. WIKI.logger.info(`No new editors found: [ SKIPPED ]`)
  50. }
  51. } catch (err) {
  52. WIKI.logger.error(`Failed to scan or load new editors: [ FAILED ]`)
  53. WIKI.logger.error(err)
  54. }
  55. }
  56. }