aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/lib/plugins/plugin-manager.ts90
-rw-r--r--server/typings/plugins/index.d.ts2
-rw-r--r--server/typings/plugins/plugin-library.model.ts (renamed from shared/models/plugins/plugin-library.model.ts)0
-rw-r--r--server/typings/plugins/register-options.model.ts19
-rw-r--r--shared/models/plugins/register-options.model.ts14
5 files changed, 72 insertions, 53 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index e76dbb53e..ac31b06dc 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -4,7 +4,6 @@ import { basename, join } from 'path'
4import { CONFIG } from '../../initializers/config' 4import { CONFIG } from '../../initializers/config'
5import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' 5import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins'
6import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' 6import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
7import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.model'
8import { createReadStream, createWriteStream } from 'fs' 7import { createReadStream, createWriteStream } from 'fs'
9import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' 8import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
10import { PluginType } from '../../../shared/models/plugins/plugin.type' 9import { PluginType } from '../../../shared/models/plugins/plugin.type'
@@ -14,10 +13,10 @@ import { RegisterSettingOptions } from '../../../shared/models/plugins/register-
14import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' 13import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model'
15import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' 14import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model'
16import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' 15import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model'
17import { ServerHookName, ServerHook } from '../../../shared/models/plugins/server-hook.model' 16import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model'
18import { isCatchable, isPromise } from '../../../shared/core-utils/miscs/miscs'
19import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' 17import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks'
20import { HookType } from '../../../shared/models/plugins/hook-type.enum' 18import { RegisterOptions } from '../../typings/plugins/register-options.model'
19import { PluginLibrary } from '../../typings/plugins'
21 20
22export interface RegisteredPlugin { 21export interface RegisteredPlugin {
23 npmName: string 22 npmName: string
@@ -287,47 +286,15 @@ export class PluginManager implements ServerHook {
287 private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { 286 private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) {
288 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) 287 const npmName = PluginModel.buildNpmName(plugin.name, plugin.type)
289 288
290 const registerHook = (options: RegisterHookOptions) => {
291 if (!this.hooks[options.target]) this.hooks[options.target] = []
292
293 this.hooks[options.target].push({
294 npmName,
295 pluginName: plugin.name,
296 handler: options.handler,
297 priority: options.priority || 0
298 })
299 }
300
301 const registerSetting = (options: RegisterSettingOptions) => {
302 if (!this.settings[npmName]) this.settings[npmName] = []
303
304 this.settings[npmName].push(options)
305 }
306
307 const settingsManager: PluginSettingsManager = {
308 getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name),
309
310 setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value)
311 }
312
313 const storageManager: PluginStorageManager = {
314 getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key),
315
316 storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data)
317 }
318
319 const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) 289 const library: PluginLibrary = require(join(pluginPath, packageJSON.library))
320 290
321 if (!isLibraryCodeValid(library)) { 291 if (!isLibraryCodeValid(library)) {
322 throw new Error('Library code is not valid (miss register or unregister function)') 292 throw new Error('Library code is not valid (miss register or unregister function)')
323 } 293 }
324 294
325 library.register({ 295 const registerHelpers = this.getRegisterHelpers(npmName, plugin)
326 registerHook, 296 library.register(registerHelpers)
327 registerSetting, 297 .catch(err => logger.error('Cannot register plugin %s.', npmName, { err }))
328 settingsManager,
329 storageManager
330 }).catch(err => logger.error('Cannot register plugin %s.', npmName, { err }))
331 298
332 logger.info('Add plugin %s CSS to global file.', npmName) 299 logger.info('Add plugin %s CSS to global file.', npmName)
333 300
@@ -407,6 +374,51 @@ export class PluginManager implements ServerHook {
407 return plugins 374 return plugins
408 } 375 }
409 376
377 // ###################### Generate register helpers ######################
378
379 private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterOptions {
380 const registerHook = (options: RegisterHookOptions) => {
381 if (!this.hooks[options.target]) this.hooks[options.target] = []
382
383 this.hooks[options.target].push({
384 npmName,
385 pluginName: plugin.name,
386 handler: options.handler,
387 priority: options.priority || 0
388 })
389 }
390
391 const registerSetting = (options: RegisterSettingOptions) => {
392 if (!this.settings[npmName]) this.settings[npmName] = []
393
394 this.settings[npmName].push(options)
395 }
396
397 const settingsManager: PluginSettingsManager = {
398 getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name),
399
400 setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value)
401 }
402
403 const storageManager: PluginStorageManager = {
404 getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key),
405
406 storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data)
407 }
408
409 const peertubeHelpers = {
410 logger
411 }
412
413 return {
414 registerHook,
415 registerSetting,
416 settingsManager,
417 storageManager,
418 peertubeHelpers
419 }
420 }
421
410 static get Instance () { 422 static get Instance () {
411 return this.instance || (this.instance = new this()) 423 return this.instance || (this.instance = new this())
412 } 424 }
diff --git a/server/typings/plugins/index.d.ts b/server/typings/plugins/index.d.ts
new file mode 100644
index 000000000..bddb3089e
--- /dev/null
+++ b/server/typings/plugins/index.d.ts
@@ -0,0 +1,2 @@
1export * from './register-options.model'
2export * from './plugin-library.model'
diff --git a/shared/models/plugins/plugin-library.model.ts b/server/typings/plugins/plugin-library.model.ts
index fd90a3b46..fd90a3b46 100644
--- a/shared/models/plugins/plugin-library.model.ts
+++ b/server/typings/plugins/plugin-library.model.ts
diff --git a/server/typings/plugins/register-options.model.ts b/server/typings/plugins/register-options.model.ts
new file mode 100644
index 000000000..007ef5e1b
--- /dev/null
+++ b/server/typings/plugins/register-options.model.ts
@@ -0,0 +1,19 @@
1import { logger } from '../../helpers/logger'
2import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model'
3import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model'
4import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model'
5import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model'
6
7export type RegisterOptions = {
8 registerHook: (options: RegisterHookOptions) => void
9
10 registerSetting: (options: RegisterSettingOptions) => void
11
12 settingsManager: PluginSettingsManager
13
14 storageManager: PluginStorageManager
15
16 peertubeHelpers: {
17 logger: typeof logger
18 }
19}
diff --git a/shared/models/plugins/register-options.model.ts b/shared/models/plugins/register-options.model.ts
deleted file mode 100644
index e3a7cff08..000000000
--- a/shared/models/plugins/register-options.model.ts
+++ /dev/null
@@ -1,14 +0,0 @@
1import { RegisterHookOptions } from './register-hook.model'
2import { RegisterSettingOptions } from './register-setting.model'
3import { PluginSettingsManager } from './plugin-settings-manager.model'
4import { PluginStorageManager } from './plugin-storage-manager.model'
5
6export type RegisterOptions = {
7 registerHook: (options: RegisterHookOptions) => void
8
9 registerSetting: (options: RegisterSettingOptions) => void
10
11 settingsManager: PluginSettingsManager
12
13 storageManager: PluginStorageManager
14}