From b4055e1c23eeefb0c8a85a77f312b2827d98f483 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 18 Jul 2019 14:28:37 +0200 Subject: Add server hooks --- shared/core-utils/miscs/miscs.ts | 12 ++++++++- shared/core-utils/plugins/hooks.ts | 41 ++++++++++++++++++++++++++++++ shared/models/plugins/hook-type.enum.ts | 5 ++++ shared/models/plugins/server-hook.model.ts | 34 +++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 shared/core-utils/plugins/hooks.ts create mode 100644 shared/models/plugins/hook-type.enum.ts create mode 100644 shared/models/plugins/server-hook.model.ts (limited to 'shared') 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) { return segmentsA.length - segmentsB.length } +function isPromise (value: any) { + return value && typeof value.then === 'function' +} + +function isCatchable (value: any) { + return value && typeof value.catch === 'function' +} + export { randomInt, - compareSemVer + compareSemVer, + isPromise, + isCatchable } 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 @@ +import { HookType } from '../../models/plugins/hook-type.enum' +import { isCatchable, isPromise } from '../miscs/miscs' + +function getHookType (hookName: string) { + if (hookName.startsWith('filter:')) return HookType.FILTER + if (hookName.startsWith('action:')) return HookType.ACTION + + return HookType.STATIC +} + +async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { + let result = param + + try { + const p = handler(result) + + switch (hookType) { + case HookType.FILTER: + if (isPromise(p)) result = await p + else result = p + break + + case HookType.STATIC: + if (isPromise(p)) await p + break + + case HookType.ACTION: + if (isCatchable(p)) p.catch(err => onError(err)) + break + } + } catch (err) { + onError(err) + } + + return result +} + +export { + getHookType, + internalRunHook +} 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 @@ +export enum HookType { + STATIC = 1, + ACTION = 2, + FILTER = 3 +} 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 @@ +export type ServerFilterHookName = + 'filter:api.videos.list.params' | + 'filter:api.videos.list.result' | + 'filter:api.video.get.result' | + + 'filter:api.video.upload.accept.result' | + 'filter:api.video-thread.create.accept.result' | + 'filter:api.video-comment-reply.create.accept.result' | + + 'filter:api.video-thread-comments.list.params' | + 'filter:api.video-thread-comments.list.result' | + + 'filter:api.video-threads.list.params' | + 'filter:api.video-threads.list.result' | + + 'filter:video.auto-blacklist.result' + +export type ServerActionHookName = + 'action:application.listening' | + + 'action:api.video.updated' | + 'action:api.video.deleted' | + 'action:api.video.uploaded' | + 'action:api.video.viewed' | + + 'action:api.video-thread.created' | + 'action:api.video-comment-reply.created' | + 'action:api.video-comment.deleted' + +export type ServerHookName = ServerFilterHookName | ServerActionHookName + +export interface ServerHook { + runHook (hookName: ServerHookName, params?: any) +} -- cgit v1.2.3