aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-18 14:28:37 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commitb4055e1c23eeefb0c8a85a77f312b2827d98f483 (patch)
tree51b6b04c1ad10897047817d2eaaa037d1331fa6a /server/lib/plugins
parent66e001c848c009412c65cbce41be344d8985fd83 (diff)
downloadPeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.gz
PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.zst
PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.zip
Add server hooks
Diffstat (limited to 'server/lib/plugins')
-rw-r--r--server/lib/plugins/hooks.ts26
-rw-r--r--server/lib/plugins/plugin-manager.ts22
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 @@
1import { ServerActionHookName, ServerFilterHookName } from '../../../shared/models/plugins/server-hook.model'
2import { PluginManager } from './plugin-manager'
3import { logger } from '../../helpers/logger'
4import * as Bluebird from 'bluebird'
5
6// Helpers to run hooks
7const 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
24export {
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-
14import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' 14import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model'
15import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' 15import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model'
16import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' 16import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model'
17import { ServerHookName, ServerHook } 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'
20import { HookType } from '../../../shared/models/plugins/hook-type.enum'
17 21
18export interface RegisteredPlugin { 22export 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
45export class PluginManager { 49export 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