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 /shared | |
parent | 66e001c848c009412c65cbce41be344d8985fd83 (diff) | |
download | PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.gz PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.zst PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.zip |
Add server hooks
Diffstat (limited to 'shared')
-rw-r--r-- | shared/core-utils/miscs/miscs.ts | 12 | ||||
-rw-r--r-- | shared/core-utils/plugins/hooks.ts | 41 | ||||
-rw-r--r-- | shared/models/plugins/hook-type.enum.ts | 5 | ||||
-rw-r--r-- | shared/models/plugins/server-hook.model.ts | 34 |
4 files changed, 91 insertions, 1 deletions
diff --git a/shared/core-utils/miscs/miscs.ts b/shared/core-utils/miscs/miscs.ts index a3921b568..5de024c08 100644 --- a/shared/core-utils/miscs/miscs.ts +++ b/shared/core-utils/miscs/miscs.ts | |||
@@ -19,7 +19,17 @@ function compareSemVer (a: string, b: string) { | |||
19 | return segmentsA.length - segmentsB.length | 19 | return segmentsA.length - segmentsB.length |
20 | } | 20 | } |
21 | 21 | ||
22 | function isPromise (value: any) { | ||
23 | return value && typeof value.then === 'function' | ||
24 | } | ||
25 | |||
26 | function isCatchable (value: any) { | ||
27 | return value && typeof value.catch === 'function' | ||
28 | } | ||
29 | |||
22 | export { | 30 | export { |
23 | randomInt, | 31 | randomInt, |
24 | compareSemVer | 32 | compareSemVer, |
33 | isPromise, | ||
34 | isCatchable | ||
25 | } | 35 | } |
diff --git a/shared/core-utils/plugins/hooks.ts b/shared/core-utils/plugins/hooks.ts new file mode 100644 index 000000000..047c04f7b --- /dev/null +++ b/shared/core-utils/plugins/hooks.ts | |||
@@ -0,0 +1,41 @@ | |||
1 | import { HookType } from '../../models/plugins/hook-type.enum' | ||
2 | import { isCatchable, isPromise } from '../miscs/miscs' | ||
3 | |||
4 | function getHookType (hookName: string) { | ||
5 | if (hookName.startsWith('filter:')) return HookType.FILTER | ||
6 | if (hookName.startsWith('action:')) return HookType.ACTION | ||
7 | |||
8 | return HookType.STATIC | ||
9 | } | ||
10 | |||
11 | async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { | ||
12 | let result = param | ||
13 | |||
14 | try { | ||
15 | const p = handler(result) | ||
16 | |||
17 | switch (hookType) { | ||
18 | case HookType.FILTER: | ||
19 | if (isPromise(p)) result = await p | ||
20 | else result = p | ||
21 | break | ||
22 | |||
23 | case HookType.STATIC: | ||
24 | if (isPromise(p)) await p | ||
25 | break | ||
26 | |||
27 | case HookType.ACTION: | ||
28 | if (isCatchable(p)) p.catch(err => onError(err)) | ||
29 | break | ||
30 | } | ||
31 | } catch (err) { | ||
32 | onError(err) | ||
33 | } | ||
34 | |||
35 | return result | ||
36 | } | ||
37 | |||
38 | export { | ||
39 | getHookType, | ||
40 | internalRunHook | ||
41 | } | ||
diff --git a/shared/models/plugins/hook-type.enum.ts b/shared/models/plugins/hook-type.enum.ts new file mode 100644 index 000000000..66d24071c --- /dev/null +++ b/shared/models/plugins/hook-type.enum.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export enum HookType { | ||
2 | STATIC = 1, | ||
3 | ACTION = 2, | ||
4 | FILTER = 3 | ||
5 | } | ||
diff --git a/shared/models/plugins/server-hook.model.ts b/shared/models/plugins/server-hook.model.ts new file mode 100644 index 000000000..30469856c --- /dev/null +++ b/shared/models/plugins/server-hook.model.ts | |||
@@ -0,0 +1,34 @@ | |||
1 | export type ServerFilterHookName = | ||
2 | 'filter:api.videos.list.params' | | ||
3 | 'filter:api.videos.list.result' | | ||
4 | 'filter:api.video.get.result' | | ||
5 | |||
6 | 'filter:api.video.upload.accept.result' | | ||
7 | 'filter:api.video-thread.create.accept.result' | | ||
8 | 'filter:api.video-comment-reply.create.accept.result' | | ||
9 | |||
10 | 'filter:api.video-thread-comments.list.params' | | ||
11 | 'filter:api.video-thread-comments.list.result' | | ||
12 | |||
13 | 'filter:api.video-threads.list.params' | | ||
14 | 'filter:api.video-threads.list.result' | | ||
15 | |||
16 | 'filter:video.auto-blacklist.result' | ||
17 | |||
18 | export type ServerActionHookName = | ||
19 | 'action:application.listening' | | ||
20 | |||
21 | 'action:api.video.updated' | | ||
22 | 'action:api.video.deleted' | | ||
23 | 'action:api.video.uploaded' | | ||
24 | 'action:api.video.viewed' | | ||
25 | |||
26 | 'action:api.video-thread.created' | | ||
27 | 'action:api.video-comment-reply.created' | | ||
28 | 'action:api.video-comment.deleted' | ||
29 | |||
30 | export type ServerHookName = ServerFilterHookName | ServerActionHookName | ||
31 | |||
32 | export interface ServerHook { | ||
33 | runHook (hookName: ServerHookName, params?: any) | ||
34 | } | ||