]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/plugins/plugin-manager.ts
Log error on unknown hook
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-manager.ts
index 9afda97ead83cbf225a5aea94bd02649f2ea667f..cfe63e50d1a681dc1d94028382572a88a0b15cdd 100644 (file)
@@ -13,7 +13,7 @@ import { RegisterSettingOptions } from '../../../shared/models/plugins/register-
 import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model'
 import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model'
 import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model'
-import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model'
+import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model'
 import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks'
 import { RegisterOptions } from '../../typings/plugins/register-options.model'
 import { PluginLibrary } from '../../typings/plugins'
@@ -98,15 +98,15 @@ export class PluginManager implements ServerHook {
 
   // ###################### Hooks ######################
 
-  async runHook (hookName: ServerHookName, param?: any) {
-    let result = param
-
-    if (!this.hooks[hookName]) return result
+  async runHook <T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {
+    if (!this.hooks[hookName]) return Promise.resolve(result)
 
     const hookType = getHookType(hookName)
 
     for (const hook of this.hooks[hookName]) {
-      result = await internalRunHook(hook.handler, hookType, param, err => {
+      logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName)
+
+      result = await internalRunHook(hook.handler, hookType, result, params, err => {
         logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err })
       })
     }
@@ -125,6 +125,13 @@ export class PluginManager implements ServerHook {
       try {
         await this.registerPluginOrTheme(plugin)
       } catch (err) {
+        // Try to unregister the plugin
+        try {
+          await this.unregister(PluginModel.buildNpmName(plugin.name, plugin.type))
+        } catch {
+          // we don't care if we cannot unregister it
+        }
+
         logger.error('Cannot register plugin %s, skipping.', plugin.name, { err })
       }
     }
@@ -381,6 +388,11 @@ export class PluginManager implements ServerHook {
 
   private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterOptions {
     const registerHook = (options: RegisterHookOptions) => {
+      if (serverHookObject[options.target] !== true) {
+        logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, npmName)
+        return
+      }
+
       if (!this.hooks[options.target]) this.hooks[options.target] = []
 
       this.hooks[options.target].push({