a new plugins controller

This commit is contained in:
lesion 2022-08-09 18:32:47 +02:00
parent 9f748423c9
commit 10ba78de96
No known key found for this signature in database
GPG key ID: 352918250B012177
5 changed files with 63 additions and 27 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "gancio_plugins/gancio-plugin-telegram-bridge"]
path = gancio_plugins/gancio-plugin-telegram-bridge
url = https://framagit.org/les/gancio-plugin-telegram-bridge.git

@ -0,0 +1 @@
Subproject commit a079bff738064a08cf00c51b1d09b855265f6306

View file

@ -0,0 +1,53 @@
const path = require('path')
const fs = require('fs')
const log = require('../../log')
const config = require('../../config')
const pluginController = {
plugins: [],
getAll (req, res, next) {
res.json(pluginController.plugins)
},
_load () {
const settingsController = require('./settings')
// load custom plugins
const plugins_path = config.plugins_path || path.resolve(process.env.cwd || '', 'gancio_plugins')
log.info(`Loading plugin ${plugins_path}`)
if (fs.existsSync(plugins_path)) {
const notifier = require('../../notifier')
const plugins = fs.readdirSync(plugins_path)
.map(e => path.resolve(plugins_path, e, 'index.js'))
.filter(index => fs.existsSync(index))
plugins.forEach( pluginFile => {
try {
const plugin = require(pluginFile)
if (typeof plugin.load !== 'function') return
const name = plugin.configuration.name
console.log(`Found plugin '${name}'`)
pluginController.plugins.push(plugin.configuration)
if (settingsController.settings['plugin_' + name]) {
const pluginSetting = settingsController.settings['plugin_' + name]
if (pluginSetting.enable) {
plugin.load({ settings: settingsController.settings }, settingsController.settings.plugins)
if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.on('Create', plugin.onEventCreate)
}
if (typeof plugin.onEventDelete === 'function') {
notifier.emitter.on('Delete', plugin.onEventDelete)
}
if (typeof plugin.onEventUpdate === 'function') {
notifier.emitter.on('Update', plugin.onEventUpdate)
}
}
} else {
settingsController.set('plugin_' + name, { enable: false })
}
} catch (e) {
log.warn(`Unable to load plugin ${pluginFile}: ${String(e)}`)
}
})
}
}
}
module.exports = pluginController

View file

@ -9,7 +9,7 @@ const generateKeyPair = promisify(crypto.generateKeyPair)
const log = require('../../log') const log = require('../../log')
const locales = require('../../../locales/index') const locales = require('../../../locales/index')
const escape = require('lodash/escape') const escape = require('lodash/escape')
const pluginController = require('./plugins')
let defaultHostname let defaultHostname
try { try {
@ -108,32 +108,7 @@ const settingsController = {
}) })
} }
// load custom plugins pluginController._load()
const plugins_path = path.resolve(process.env.cwd || '', 'plugins')
if (process.env.NODE_ENV === 'production' && fs.existsSync(plugins_path)) {
const notifier = require('../../notifier')
const pluginsFile = fs.readdirSync(plugins_path).filter(e => path.extname(e).toLowerCase() === '.js')
pluginsFile.forEach( pluginFile => {
try {
const plugin = require(path.resolve(plugins_path, pluginFile))
if (typeof plugin.load !== 'function') return
plugin.load({ settings: settingsController.settings })
settingsController.settings.plugins.push(plugin)
log.info(`Plugin ${pluginFile} loaded!`)
if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.on('Create', plugin.onEventCreate)
}
if (typeof plugin.onEventDelete === 'function') {
notifier.emitter.on('Delete', plugin.onEventDelete)
}
if (typeof plugin.onEventUpdate === 'function') {
notifier.emitter.on('Update', plugin.onEventUpdate)
}
} catch (e) {
log.warn(`Unable to load plugin ${pluginFile}: ${String(e)}`)
}
})
}
}, },
async set (key, value, is_secret = false) { async set (key, value, is_secret = false) {

View file

@ -34,6 +34,7 @@ if (config.status !== 'READY') {
const oauthController = require('./controller/oauth') const oauthController = require('./controller/oauth')
const announceController = require('./controller/announce') const announceController = require('./controller/announce')
const collectionController = require('./controller/collection') const collectionController = require('./controller/collection')
const pluginController = require('./controller/plugins')
const helpers = require('../helpers') const helpers = require('../helpers')
const storage = require('./storage') const storage = require('./storage')
const upload = multer({ storage }) const upload = multer({ storage })
@ -189,6 +190,9 @@ if (config.status !== 'READY') {
api.post('/filter', isAdmin, collectionController.addFilter) api.post('/filter', isAdmin, collectionController.addFilter)
api.delete('/filter/:id', isAdmin, collectionController.removeFilter) api.delete('/filter/:id', isAdmin, collectionController.removeFilter)
// - PLUGINS
api.get('/plugins', isAdmin, pluginController.getAll)
// OAUTH // OAUTH
api.get('/clients', isAuth, oauthController.getClients) api.get('/clients', isAuth, oauthController.getClients)
api.get('/client/:client_id', isAuth, oauthController.getClient) api.get('/client/:client_id', isAuth, oauthController.getClient)