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.
63 lines
1.7 KiB
63 lines
1.7 KiB
7 years ago
|
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() {
|
||
6 years ago
|
return WIKI.models.editors.query().where({ isEnabled: true })
|
||
7 years ago
|
}
|
||
|
|
||
|
static async refreshEditorsFromDisk() {
|
||
|
try {
|
||
6 years ago
|
const dbEditors = await WIKI.models.editors.query()
|
||
7 years ago
|
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) {
|
||
6 years ago
|
await WIKI.models.editors.query().insert(newEditors)
|
||
7 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|