diff options
Diffstat (limited to 'server/lib/plugins')
-rw-r--r-- | server/lib/plugins/hooks.ts | 26 | ||||
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 22 |
2 files changed, 35 insertions, 13 deletions
diff --git a/server/lib/plugins/hooks.ts b/server/lib/plugins/hooks.ts new file mode 100644 index 000000000..7bb907e6a --- /dev/null +++ b/server/lib/plugins/hooks.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models/plugins/server-hook.model' | ||
2 | import { PluginManager } from './plugin-manager' | ||
3 | import { logger } from '../../helpers/logger' | ||
4 | import * as Bluebird from 'bluebird' | ||
5 | |||
6 | // Helpers to run hooks | ||
7 | const Hooks = { | ||
8 | wrapObject: <T, U extends ServerFilterHookName>(obj: T, hookName: U) => { | ||
9 | return PluginManager.Instance.runHook(hookName, obj) as Promise<T> | ||
10 | }, | ||
11 | |||
12 | wrapPromise: async <T, U extends ServerFilterHookName>(fun: Promise<T> | Bluebird<T>, hookName: U) => { | ||
13 | const result = await fun | ||
14 | |||
15 | return PluginManager.Instance.runHook(hookName, result) | ||
16 | }, | ||
17 | |||
18 | runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => { | ||
19 | PluginManager.Instance.runHook(hookName, params) | ||
20 | .catch(err => logger.error('Fatal hook error.', { err })) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | export { | ||
25 | Hooks | ||
26 | } | ||
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 |