diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-18 14:28:37 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | b4055e1c23eeefb0c8a85a77f312b2827d98f483 (patch) | |
tree | 51b6b04c1ad10897047817d2eaaa037d1331fa6a /server/lib/plugins/plugin-manager.ts | |
parent | 66e001c848c009412c65cbce41be344d8985fd83 (diff) | |
download | PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.gz PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.zst PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.zip |
Add server hooks
Diffstat (limited to 'server/lib/plugins/plugin-manager.ts')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 570b56193..85ee3decb 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -14,6 +14,10 @@ import { RegisterSettingOptions } from '../../../shared/models/plugins/register- | |||
14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' | 14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' |
15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | 15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' |
16 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' | 16 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' |
17 | import { ServerHookName, ServerHook } from '../../../shared/models/plugins/server-hook.model' | ||
18 | import { isCatchable, isPromise } from '../../../shared/core-utils/miscs/miscs' | ||
19 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' | ||
20 | import { HookType } from '../../../shared/models/plugins/hook-type.enum' | ||
17 | 21 | ||
18 | export interface RegisteredPlugin { | 22 | export interface RegisteredPlugin { |
19 | npmName: string | 23 | npmName: string |
@@ -42,7 +46,7 @@ export interface HookInformationValue { | |||
42 | priority: number | 46 | priority: number |
43 | } | 47 | } |
44 | 48 | ||
45 | export class PluginManager { | 49 | export class PluginManager implements ServerHook { |
46 | 50 | ||
47 | private static instance: PluginManager | 51 | private static instance: PluginManager |
48 | 52 | ||
@@ -95,25 +99,17 @@ export class PluginManager { | |||
95 | 99 | ||
96 | // ###################### Hooks ###################### | 100 | // ###################### Hooks ###################### |
97 | 101 | ||
98 | async runHook (hookName: string, param?: any) { | 102 | async runHook (hookName: ServerHookName, param?: any) { |
99 | let result = param | 103 | let result = param |
100 | 104 | ||
101 | if (!this.hooks[hookName]) return result | 105 | if (!this.hooks[hookName]) return result |
102 | 106 | ||
103 | const wait = hookName.startsWith('static:') | 107 | const hookType = getHookType(hookName) |
104 | 108 | ||
105 | for (const hook of this.hooks[hookName]) { | 109 | for (const hook of this.hooks[hookName]) { |
106 | try { | 110 | result = await internalRunHook(hook.handler, hookType, param, err => { |
107 | const p = hook.handler(param) | ||
108 | |||
109 | if (wait) { | ||
110 | result = await p | ||
111 | } else if (p.catch) { | ||
112 | p.catch(err => logger.warn('Hook %s of plugin %s thrown an error.', hookName, hook.pluginName, { err })) | ||
113 | } | ||
114 | } catch (err) { | ||
115 | logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) | 111 | logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) |
116 | } | 112 | }) |
117 | } | 113 | } |
118 | 114 | ||
119 | return result | 115 | return result |