From 18a6f04c071f7a0735eb39b8c67fd51a082d1a31 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Jul 2019 15:54:08 +0200 Subject: WIP plugins: hook on client side --- server/controllers/api/config.ts | 19 ++++++++++++++++++- server/lib/plugins/plugin-manager.ts | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'server') diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 1d12f701b..8563b7437 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -1,6 +1,6 @@ import * as express from 'express' import { snakeCase } from 'lodash' -import { ServerConfig, UserRight } from '../../../shared' +import { ServerConfig, ServerConfigPlugin, UserRight } from '../../../shared' import { About } from '../../../shared/models/server/about.model' import { CustomConfig } from '../../../shared/models/server/custom-config.model' import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' @@ -15,6 +15,8 @@ import { Emailer } from '../../lib/emailer' import { isNumeric } from 'validator' import { objectConverter } from '../../helpers/core-utils' import { CONFIG, reloadConfig } from '../../initializers/config' +import { PluginManager } from '../../lib/plugins/plugin-manager' +import { PluginType } from '../../../shared/models/plugins/plugin.type' const packageJSON = require('../../../../package.json') const configRouter = express.Router() @@ -54,6 +56,20 @@ async function getConfig (req: express.Request, res: express.Response) { .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true) .map(r => parseInt(r, 10)) + const plugins: ServerConfigPlugin[] = [] + const registeredPlugins = PluginManager.Instance.getRegisteredPlugins() + for (const pluginName of Object.keys(registeredPlugins)) { + const plugin = registeredPlugins[ pluginName ] + if (plugin.type !== PluginType.PLUGIN) continue + + plugins.push({ + name: plugin.name, + version: plugin.version, + description: plugin.description, + clientScripts: plugin.clientScripts + }) + } + const json: ServerConfig = { instance: { name: CONFIG.INSTANCE.NAME, @@ -66,6 +82,7 @@ async function getConfig (req: express.Request, res: express.Response) { css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS } }, + plugins, email: { enabled: Emailer.isEnabled() }, diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index b898e64fa..7cbfa8569 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -75,6 +75,27 @@ export class PluginManager { return registered } + getRegisteredPlugins () { + return this.registeredPlugins + } + + async runHook (hookName: string, param?: any) { + let result = param + + const wait = hookName.startsWith('static:') + + for (const hook of this.hooks[hookName]) { + try { + if (wait) result = await hook.handler(param) + else result = hook.handler() + } catch (err) { + logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) + } + } + + return result + } + async unregister (name: string) { const plugin = this.getRegisteredPlugin(name) -- cgit v1.2.3