diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..c1935240 --- /dev/null +++ b/.gitmodules @@ -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 diff --git a/gancio_plugins/gancio-plugin-telegram-bridge b/gancio_plugins/gancio-plugin-telegram-bridge new file mode 160000 index 00000000..a079bff7 --- /dev/null +++ b/gancio_plugins/gancio-plugin-telegram-bridge @@ -0,0 +1 @@ +Subproject commit a079bff738064a08cf00c51b1d09b855265f6306 diff --git a/server/api/controller/plugins.js b/server/api/controller/plugins.js new file mode 100644 index 00000000..60f0d36b --- /dev/null +++ b/server/api/controller/plugins.js @@ -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 \ No newline at end of file diff --git a/server/api/controller/settings.js b/server/api/controller/settings.js index f10248c2..2682eb25 100644 --- a/server/api/controller/settings.js +++ b/server/api/controller/settings.js @@ -9,7 +9,7 @@ const generateKeyPair = promisify(crypto.generateKeyPair) const log = require('../../log') const locales = require('../../../locales/index') const escape = require('lodash/escape') - +const pluginController = require('./plugins') let defaultHostname try { @@ -108,32 +108,7 @@ const settingsController = { }) } - // load custom plugins - 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)}`) - } - }) - } + pluginController._load() }, async set (key, value, is_secret = false) { diff --git a/server/api/index.js b/server/api/index.js index 8a014cf6..c3ca9cc1 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -34,6 +34,7 @@ if (config.status !== 'READY') { const oauthController = require('./controller/oauth') const announceController = require('./controller/announce') const collectionController = require('./controller/collection') + const pluginController = require('./controller/plugins') const helpers = require('../helpers') const storage = require('./storage') const upload = multer({ storage }) @@ -189,6 +190,9 @@ if (config.status !== 'READY') { api.post('/filter', isAdmin, collectionController.addFilter) api.delete('/filter/:id', isAdmin, collectionController.removeFilter) + // - PLUGINS + api.get('/plugins', isAdmin, pluginController.getAll) + // OAUTH api.get('/clients', isAuth, oauthController.getClients) api.get('/client/:client_id', isAuth, oauthController.getClient)