aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/plugins.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-08-20 11:46:25 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-08-20 14:23:57 +0200
commitf95628636b6ccdf3eae2449ca718e075b072f678 (patch)
tree35d51980c87b7d6747bdff6e37bdfe37e3c989dc /client/src/root-helpers/plugins.ts
parenta9f6802e7dac4f21599076bc1119bb6ff16961dc (diff)
downloadPeerTube-f95628636b6ccdf3eae2449ca718e075b072f678.tar.gz
PeerTube-f95628636b6ccdf3eae2449ca718e075b072f678.tar.zst
PeerTube-f95628636b6ccdf3eae2449ca718e075b072f678.zip
Support plugin hooks in embed
Diffstat (limited to 'client/src/root-helpers/plugins.ts')
-rw-r--r--client/src/root-helpers/plugins.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/client/src/root-helpers/plugins.ts b/client/src/root-helpers/plugins.ts
new file mode 100644
index 000000000..011721761
--- /dev/null
+++ b/client/src/root-helpers/plugins.ts
@@ -0,0 +1,81 @@
1import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
2import { ClientHookName, ClientScript, RegisterClientHookOptions, ServerConfigPlugin, PluginType, clientHookObject } from '../../../shared/models'
3import { RegisterClientHelpers } from 'src/types/register-client-option.model'
4import { ClientScript as ClientScriptModule } from '../types/client-script.model'
5import { importModule } from './utils'
6
7interface HookStructValue extends RegisterClientHookOptions {
8 plugin: ServerConfigPlugin
9 clientScript: ClientScript
10}
11
12type Hooks = { [ name: string ]: HookStructValue[] }
13
14type PluginInfo = {
15 plugin: ServerConfigPlugin
16 clientScript: ClientScript
17 pluginType: PluginType
18 isTheme: boolean
19}
20
21async function runHook<T> (hooks: Hooks, hookName: ClientHookName, result?: T, params?: any) {
22 if (!hooks[hookName]) return result
23
24 const hookType = getHookType(hookName)
25
26 for (const hook of hooks[hookName]) {
27 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
28
29 result = await internalRunHook(hook.handler, hookType, result, params, err => {
30 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
31 })
32 }
33
34 return result
35}
36
37function loadPlugin (hooks: Hooks, pluginInfo: PluginInfo, peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers) {
38 const { plugin, clientScript } = pluginInfo
39
40 const registerHook = (options: RegisterClientHookOptions) => {
41 if (clientHookObject[options.target] !== true) {
42 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
43 return
44 }
45
46 if (!hooks[options.target]) hooks[options.target] = []
47
48 hooks[options.target].push({
49 plugin,
50 clientScript,
51 target: options.target,
52 handler: options.handler,
53 priority: options.priority || 0
54 })
55 }
56
57 const peertubeHelpers = peertubeHelpersFactory(pluginInfo)
58
59 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
60
61 return importModule(clientScript.script)
62 .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
63 .then(() => sortHooksByPriority(hooks))
64 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
65}
66
67export {
68 HookStructValue,
69 Hooks,
70 PluginInfo,
71 loadPlugin,
72 runHook
73}
74
75function sortHooksByPriority (hooks: Hooks) {
76 for (const hookName of Object.keys(hooks)) {
77 hooks[hookName].sort((a, b) => {
78 return b.priority - a.priority
79 })
80 }
81}