aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/plugin-manager.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-04-10 15:07:54 +0200
committerChocobozzz <me@florianbigard.com>2020-04-10 15:23:25 +0200
commit5e2b2e2775421cd98286d6e2f75cf38aae7a212c (patch)
treed92e32824d83cecbe5e90206738f393b47e55754 /server/lib/plugins/plugin-manager.ts
parent9afa0901f11c321e071c42ba3c814a3af4843c55 (diff)
downloadPeerTube-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.ts51
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'
13import { PluginType } from '../../../shared/models/plugins/plugin.type' 13import { PluginType } from '../../../shared/models/plugins/plugin.type'
14import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' 14import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
15import { outputFile, readJSON } from 'fs-extra' 15import { outputFile, readJSON } from 'fs-extra'
16import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' 16import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model'
17import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' 17import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks'
18import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' 18import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model'
19import { PluginLibrary } from '../../typings/plugins' 19import { PluginLibrary } from '../../typings/plugins'
20import { ClientHtml } from '../client-html' 20import { ClientHtml } from '../client-html'
21import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model'
22import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
23import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' 21import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model'
24import { buildRegisterHelpers, reinitVideoConstants } from './register-helpers' 22import { RegisterHelpersStore } from './register-helpers-store'
23import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
25 24
26export interface RegisteredPlugin { 25export 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) {