aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/plugins')
-rw-r--r--server/lib/plugins/plugin-manager.ts90
1 files changed, 51 insertions, 39 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 }