aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/plugins-manager.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-10 15:01:12 +0100
committerChocobozzz <me@florianbigard.com>2021-12-10 15:01:12 +0100
commitd63e6d4604dfbe4938c7d66832c9202364c5bb64 (patch)
tree6bd444be722276ff214d911284a400e374bdddc8 /client/src/root-helpers/plugins-manager.ts
parent03a65456f44a6152bb68975e29e076c8c5754cd6 (diff)
downloadPeerTube-d63e6d4604dfbe4938c7d66832c9202364c5bb64.tar.gz
PeerTube-d63e6d4604dfbe4938c7d66832c9202364c5bb64.tar.zst
PeerTube-d63e6d4604dfbe4938c7d66832c9202364c5bb64.zip
Add ability for plugins to register client routes
Diffstat (limited to 'client/src/root-helpers/plugins-manager.ts')
-rw-r--r--client/src/root-helpers/plugins-manager.ts29
1 files changed, 25 insertions, 4 deletions
diff --git a/client/src/root-helpers/plugins-manager.ts b/client/src/root-helpers/plugins-manager.ts
index 9cba63373..e574e75a3 100644
--- a/client/src/root-helpers/plugins-manager.ts
+++ b/client/src/root-helpers/plugins-manager.ts
@@ -13,7 +13,8 @@ import {
13 PluginType, 13 PluginType,
14 RegisterClientFormFieldOptions, 14 RegisterClientFormFieldOptions,
15 RegisterClientHookOptions, 15 RegisterClientHookOptions,
16 RegisterClientSettingsScript, 16 RegisterClientRouteOptions,
17 RegisterClientSettingsScriptOptions,
17 RegisterClientVideoFieldOptions, 18 RegisterClientVideoFieldOptions,
18 RegisteredExternalAuthConfig, 19 RegisteredExternalAuthConfig,
19 ServerConfigPlugin 20 ServerConfigPlugin
@@ -37,7 +38,8 @@ type PluginInfo = {
37 38
38type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers 39type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers
39type OnFormFields = (options: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void 40type OnFormFields = (options: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void
40type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) => void 41type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) => void
42type OnClientRoute = (options: RegisterClientRouteOptions) => void
41 43
42const logger = debug('peertube:plugins') 44const logger = debug('peertube:plugins')
43 45
@@ -64,15 +66,18 @@ class PluginsManager {
64 private readonly peertubeHelpersFactory: PeertubeHelpersFactory 66 private readonly peertubeHelpersFactory: PeertubeHelpersFactory
65 private readonly onFormFields: OnFormFields 67 private readonly onFormFields: OnFormFields
66 private readonly onSettingsScripts: OnSettingsScripts 68 private readonly onSettingsScripts: OnSettingsScripts
69 private readonly onClientRoute: OnClientRoute
67 70
68 constructor (options: { 71 constructor (options: {
69 peertubeHelpersFactory: PeertubeHelpersFactory 72 peertubeHelpersFactory: PeertubeHelpersFactory
70 onFormFields?: OnFormFields 73 onFormFields?: OnFormFields
71 onSettingsScripts?: OnSettingsScripts 74 onSettingsScripts?: OnSettingsScripts
75 onClientRoute?: OnClientRoute
72 }) { 76 }) {
73 this.peertubeHelpersFactory = options.peertubeHelpersFactory 77 this.peertubeHelpersFactory = options.peertubeHelpersFactory
74 this.onFormFields = options.onFormFields 78 this.onFormFields = options.onFormFields
75 this.onSettingsScripts = options.onSettingsScripts 79 this.onSettingsScripts = options.onSettingsScripts
80 this.onClientRoute = options.onClientRoute
76 } 81 }
77 82
78 static getPluginPathPrefix (isTheme: boolean) { 83 static getPluginPathPrefix (isTheme: boolean) {
@@ -221,7 +226,7 @@ class PluginsManager {
221 return this.onFormFields(commonOptions, videoFormOptions) 226 return this.onFormFields(commonOptions, videoFormOptions)
222 } 227 }
223 228
224 const registerSettingsScript = (options: RegisterClientSettingsScript) => { 229 const registerSettingsScript = (options: RegisterClientSettingsScriptOptions) => {
225 if (!this.onSettingsScripts) { 230 if (!this.onSettingsScripts) {
226 throw new Error('Registering settings script is not supported') 231 throw new Error('Registering settings script is not supported')
227 } 232 }
@@ -229,13 +234,29 @@ class PluginsManager {
229 return this.onSettingsScripts(pluginInfo, options) 234 return this.onSettingsScripts(pluginInfo, options)
230 } 235 }
231 236
237 const registerClientRoute = (options: RegisterClientRouteOptions) => {
238 if (!this.onClientRoute) {
239 throw new Error('Registering client route is not supported')
240 }
241
242 return this.onClientRoute(options)
243 }
244
232 const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo) 245 const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo)
233 246
234 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) 247 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
235 248
236 const absURL = (environment.apiUrl || window.location.origin) + clientScript.script 249 const absURL = (environment.apiUrl || window.location.origin) + clientScript.script
237 return dynamicImport(absURL) 250 return dynamicImport(absURL)
238 .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers })) 251 .then((script: ClientScriptModule) => {
252 return script.register({
253 registerHook,
254 registerVideoField,
255 registerSettingsScript,
256 registerClientRoute,
257 peertubeHelpers
258 })
259 })
239 .then(() => this.sortHooksByPriority()) 260 .then(() => this.sortHooksByPriority())
240 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) 261 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
241 } 262 }