diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-10 15:07:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-04-10 15:23:25 +0200 |
commit | 5e2b2e2775421cd98286d6e2f75cf38aae7a212c (patch) | |
tree | d92e32824d83cecbe5e90206738f393b47e55754 /server/lib/plugins/plugin-manager.ts | |
parent | 9afa0901f11c321e071c42ba3c814a3af4843c55 (diff) | |
download | PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.tar.gz PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.tar.zst PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.zip |
Add ability for plugins to add custom routes
Diffstat (limited to 'server/lib/plugins/plugin-manager.ts')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 44530d203..37fb07716 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -13,15 +13,14 @@ import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | |||
13 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 13 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
14 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' | 14 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
15 | import { outputFile, readJSON } from 'fs-extra' | 15 | import { outputFile, readJSON } from 'fs-extra' |
16 | import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' | 16 | import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model' |
17 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' | 17 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' |
18 | import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' | 18 | import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' |
19 | import { PluginLibrary } from '../../typings/plugins' | 19 | import { PluginLibrary } from '../../typings/plugins' |
20 | import { ClientHtml } from '../client-html' | 20 | import { ClientHtml } from '../client-html' |
21 | import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model' | ||
22 | import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' | ||
23 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' | 21 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' |
24 | import { buildRegisterHelpers, reinitVideoConstants } from './register-helpers' | 22 | import { RegisterHelpersStore } from './register-helpers-store' |
23 | import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model' | ||
25 | 24 | ||
26 | export interface RegisteredPlugin { | 25 | export interface RegisteredPlugin { |
27 | npmName: string | 26 | npmName: string |
@@ -59,10 +58,11 @@ export class PluginManager implements ServerHook { | |||
59 | private static instance: PluginManager | 58 | private static instance: PluginManager |
60 | 59 | ||
61 | private registeredPlugins: { [name: string]: RegisteredPlugin } = {} | 60 | private registeredPlugins: { [name: string]: RegisteredPlugin } = {} |
62 | private settings: { [name: string]: RegisterServerSettingOptions[] } = {} | ||
63 | private hooks: { [name: string]: HookInformationValue[] } = {} | 61 | private hooks: { [name: string]: HookInformationValue[] } = {} |
64 | private translations: PluginLocalesTranslations = {} | 62 | private translations: PluginLocalesTranslations = {} |
65 | 63 | ||
64 | private registerHelpersStore: { [npmName: string]: RegisterHelpersStore } = {} | ||
65 | |||
66 | private constructor () { | 66 | private constructor () { |
67 | } | 67 | } |
68 | 68 | ||
@@ -103,7 +103,17 @@ export class PluginManager implements ServerHook { | |||
103 | } | 103 | } |
104 | 104 | ||
105 | getRegisteredSettings (npmName: string) { | 105 | getRegisteredSettings (npmName: string) { |
106 | return this.settings[npmName] || [] | 106 | const store = this.registerHelpersStore[npmName] |
107 | if (store) return store.getSettings() | ||
108 | |||
109 | return [] | ||
110 | } | ||
111 | |||
112 | getRouter (npmName: string) { | ||
113 | const store = this.registerHelpersStore[npmName] | ||
114 | if (!store) return null | ||
115 | |||
116 | return store.getRouter() | ||
107 | } | 117 | } |
108 | 118 | ||
109 | getTranslations (locale: string) { | 119 | getTranslations (locale: string) { |
@@ -164,7 +174,6 @@ export class PluginManager implements ServerHook { | |||
164 | } | 174 | } |
165 | 175 | ||
166 | delete this.registeredPlugins[plugin.npmName] | 176 | delete this.registeredPlugins[plugin.npmName] |
167 | delete this.settings[plugin.npmName] | ||
168 | 177 | ||
169 | this.deleteTranslations(plugin.npmName) | 178 | this.deleteTranslations(plugin.npmName) |
170 | 179 | ||
@@ -176,7 +185,10 @@ export class PluginManager implements ServerHook { | |||
176 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) | 185 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) |
177 | } | 186 | } |
178 | 187 | ||
179 | reinitVideoConstants(plugin.npmName) | 188 | const store = this.registerHelpersStore[plugin.npmName] |
189 | store.reinitVideoConstants(plugin.npmName) | ||
190 | |||
191 | delete this.registerHelpersStore[plugin.npmName] | ||
180 | 192 | ||
181 | logger.info('Regenerating registered plugin CSS to global file.') | 193 | logger.info('Regenerating registered plugin CSS to global file.') |
182 | await this.regeneratePluginGlobalCSS() | 194 | await this.regeneratePluginGlobalCSS() |
@@ -429,34 +441,21 @@ export class PluginManager implements ServerHook { | |||
429 | // ###################### Generate register helpers ###################### | 441 | // ###################### Generate register helpers ###################### |
430 | 442 | ||
431 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions { | 443 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions { |
432 | const registerHook = (options: RegisterServerHookOptions) => { | 444 | const onHookAdded = (options: RegisterServerHookOptions) => { |
433 | if (serverHookObject[options.target] !== true) { | ||
434 | logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, npmName) | ||
435 | return | ||
436 | } | ||
437 | |||
438 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | 445 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
439 | 446 | ||
440 | this.hooks[options.target].push({ | 447 | this.hooks[options.target].push({ |
441 | npmName, | 448 | npmName: npmName, |
442 | pluginName: plugin.name, | 449 | pluginName: plugin.name, |
443 | handler: options.handler, | 450 | handler: options.handler, |
444 | priority: options.priority || 0 | 451 | priority: options.priority || 0 |
445 | }) | 452 | }) |
446 | } | 453 | } |
447 | 454 | ||
448 | const registerSetting = (options: RegisterServerSettingOptions) => { | 455 | const registerHelpersStore = new RegisterHelpersStore(npmName, plugin, onHookAdded.bind(this)) |
449 | if (!this.settings[npmName]) this.settings[npmName] = [] | 456 | this.registerHelpersStore[npmName] = registerHelpersStore |
450 | |||
451 | this.settings[npmName].push(options) | ||
452 | } | ||
453 | |||
454 | const registerHelpers = buildRegisterHelpers(npmName, plugin) | ||
455 | 457 | ||
456 | return Object.assign(registerHelpers, { | 458 | return registerHelpersStore.buildRegisterHelpers() |
457 | registerHook, | ||
458 | registerSetting | ||
459 | }) | ||
460 | } | 459 | } |
461 | 460 | ||
462 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { | 461 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { |