2018-05-28 18:46:55 +00:00
|
|
|
const Model = require('objection').Model
|
|
|
|
const autoload = require('auto-load')
|
|
|
|
const path = require('path')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Editor model
|
|
|
|
*/
|
|
|
|
module.exports = class Editor extends Model {
|
|
|
|
static get tableName() { return 'editors' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['key', 'title', 'isEnabled'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
key: {type: 'string'},
|
|
|
|
title: {type: 'string'},
|
|
|
|
isEnabled: {type: 'boolean'},
|
|
|
|
config: {type: 'object'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getEnabledEditors() {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.editors.query().where({ isEnabled: true })
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static async refreshEditorsFromDisk() {
|
|
|
|
try {
|
2018-07-30 02:23:33 +00:00
|
|
|
const dbEditors = await WIKI.models.editors.query()
|
2018-05-28 18:46:55 +00:00
|
|
|
const diskEditors = autoload(path.join(WIKI.SERVERPATH, 'modules/editor'))
|
|
|
|
let newEditors = []
|
|
|
|
_.forOwn(diskEditors, (strategy, strategyKey) => {
|
|
|
|
if (!_.some(dbEditors, ['key', strategy.key])) {
|
|
|
|
newEditors.push({
|
|
|
|
key: strategy.key,
|
|
|
|
title: strategy.title,
|
|
|
|
isEnabled: false,
|
|
|
|
config: _.reduce(strategy.props, (result, value, key) => {
|
|
|
|
_.set(result, value, '')
|
|
|
|
return result
|
|
|
|
}, {})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (newEditors.length > 0) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.editors.query().insert(newEditors)
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.info(`Loaded ${newEditors.length} new editors: [ OK ]`)
|
|
|
|
} else {
|
|
|
|
WIKI.logger.info(`No new editors found: [ SKIPPED ]`)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(`Failed to scan or load new editors: [ FAILED ]`)
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|