aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-19 17:30:41 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commit89cd12756035a146bbcc4db78cd3cd64f2f3d88d (patch)
tree896cd9fca1e0baa969b1f7a5b398ec1602761661 /server/lib
parent09071200c73f5358e1d0bfb61a274e4f2c4ec52b (diff)
downloadPeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.tar.gz
PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.tar.zst
PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.zip
Add hook filters tests
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/plugins/hooks.ts19
-rw-r--r--server/lib/plugins/plugin-manager.ts10
-rw-r--r--server/lib/video-blacklist.ts5
3 files changed, 22 insertions, 12 deletions
diff --git a/server/lib/plugins/hooks.ts b/server/lib/plugins/hooks.ts
index 7bb907e6a..b694d4118 100644
--- a/server/lib/plugins/hooks.ts
+++ b/server/lib/plugins/hooks.ts
@@ -3,16 +3,25 @@ import { PluginManager } from './plugin-manager'
3import { logger } from '../../helpers/logger' 3import { logger } from '../../helpers/logger'
4import * as Bluebird from 'bluebird' 4import * as Bluebird from 'bluebird'
5 5
6type PromiseFunction <U, T> = (params: U) => Promise<T> | Bluebird<T>
7type RawFunction <U, T> = (params: U) => T
8
6// Helpers to run hooks 9// Helpers to run hooks
7const Hooks = { 10const Hooks = {
8 wrapObject: <T, U extends ServerFilterHookName>(obj: T, hookName: U) => { 11 wrapObject: <T, U extends ServerFilterHookName>(result: T, hookName: U) => {
9 return PluginManager.Instance.runHook(hookName, obj) as Promise<T> 12 return PluginManager.Instance.runHook(hookName, result) as Promise<T>
13 },
14
15 wrapPromiseFun: async <U, T, V extends ServerFilterHookName>(fun: PromiseFunction<U, T>, params: U, hookName: V) => {
16 const result = await fun(params)
17
18 return PluginManager.Instance.runHook(hookName, result, params)
10 }, 19 },
11 20
12 wrapPromise: async <T, U extends ServerFilterHookName>(fun: Promise<T> | Bluebird<T>, hookName: U) => { 21 wrapFun: async <U, T, V extends ServerFilterHookName>(fun: RawFunction<U, T>, params: U, hookName: V) => {
13 const result = await fun 22 const result = fun(params)
14 23
15 return PluginManager.Instance.runHook(hookName, result) 24 return PluginManager.Instance.runHook(hookName, result, params)
16 }, 25 },
17 26
18 runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => { 27 runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => {
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index 9afda97ea..6485a47c5 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -98,15 +98,15 @@ export class PluginManager implements ServerHook {
98 98
99 // ###################### Hooks ###################### 99 // ###################### Hooks ######################
100 100
101 async runHook (hookName: ServerHookName, param?: any) { 101 async runHook <T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {
102 let result = param 102 if (!this.hooks[hookName]) return Promise.resolve(result)
103
104 if (!this.hooks[hookName]) return result
105 103
106 const hookType = getHookType(hookName) 104 const hookType = getHookType(hookName)
107 105
108 for (const hook of this.hooks[hookName]) { 106 for (const hook of this.hooks[hookName]) {
109 result = await internalRunHook(hook.handler, hookType, param, err => { 107 logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName)
108
109 result = await internalRunHook(hook.handler, hookType, result, params, err => {
110 logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) 110 logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err })
111 }) 111 })
112 } 112 }
diff --git a/server/lib/video-blacklist.ts b/server/lib/video-blacklist.ts
index 32b1a28fa..9bc996f5a 100644
--- a/server/lib/video-blacklist.ts
+++ b/server/lib/video-blacklist.ts
@@ -9,8 +9,9 @@ import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
9import { Hooks } from './plugins/hooks' 9import { Hooks } from './plugins/hooks'
10 10
11async function autoBlacklistVideoIfNeeded (video: VideoModel, user?: UserModel, transaction?: Transaction) { 11async function autoBlacklistVideoIfNeeded (video: VideoModel, user?: UserModel, transaction?: Transaction) {
12 const doAutoBlacklist = await Hooks.wrapPromise( 12 const doAutoBlacklist = await Hooks.wrapPromiseFun(
13 autoBlacklistNeeded({ video, user }), 13 autoBlacklistNeeded,
14 { video, user },
14 'filter:video.auto-blacklist.result' 15 'filter:video.auto-blacklist.result'
15 ) 16 )
16 17