aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r--client/src/root-helpers/plugins-manager.ts35
1 files changed, 31 insertions, 4 deletions
diff --git a/client/src/root-helpers/plugins-manager.ts b/client/src/root-helpers/plugins-manager.ts
index a1b763ff2..e574e75a3 100644
--- a/client/src/root-helpers/plugins-manager.ts
+++ b/client/src/root-helpers/plugins-manager.ts
@@ -13,8 +13,10 @@ import {
13 PluginType, 13 PluginType,
14 RegisterClientFormFieldOptions, 14 RegisterClientFormFieldOptions,
15 RegisterClientHookOptions, 15 RegisterClientHookOptions,
16 RegisterClientSettingsScript, 16 RegisterClientRouteOptions,
17 RegisterClientSettingsScriptOptions,
17 RegisterClientVideoFieldOptions, 18 RegisterClientVideoFieldOptions,
19 RegisteredExternalAuthConfig,
18 ServerConfigPlugin 20 ServerConfigPlugin
19} from '../../../shared/models' 21} from '../../../shared/models'
20import { environment } from '../environments/environment' 22import { environment } from '../environments/environment'
@@ -36,7 +38,8 @@ type PluginInfo = {
36 38
37type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers 39type PeertubeHelpersFactory = (pluginInfo: PluginInfo) => RegisterClientHelpers
38type OnFormFields = (options: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void 40type OnFormFields = (options: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void
39type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) => void 41type OnSettingsScripts = (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) => void
42type OnClientRoute = (options: RegisterClientRouteOptions) => void
40 43
41const logger = debug('peertube:plugins') 44const logger = debug('peertube:plugins')
42 45
@@ -63,21 +66,29 @@ class PluginsManager {
63 private readonly peertubeHelpersFactory: PeertubeHelpersFactory 66 private readonly peertubeHelpersFactory: PeertubeHelpersFactory
64 private readonly onFormFields: OnFormFields 67 private readonly onFormFields: OnFormFields
65 private readonly onSettingsScripts: OnSettingsScripts 68 private readonly onSettingsScripts: OnSettingsScripts
69 private readonly onClientRoute: OnClientRoute
66 70
67 constructor (options: { 71 constructor (options: {
68 peertubeHelpersFactory: PeertubeHelpersFactory 72 peertubeHelpersFactory: PeertubeHelpersFactory
69 onFormFields?: OnFormFields 73 onFormFields?: OnFormFields
70 onSettingsScripts?: OnSettingsScripts 74 onSettingsScripts?: OnSettingsScripts
75 onClientRoute?: OnClientRoute
71 }) { 76 }) {
72 this.peertubeHelpersFactory = options.peertubeHelpersFactory 77 this.peertubeHelpersFactory = options.peertubeHelpersFactory
73 this.onFormFields = options.onFormFields 78 this.onFormFields = options.onFormFields
74 this.onSettingsScripts = options.onSettingsScripts 79 this.onSettingsScripts = options.onSettingsScripts
80 this.onClientRoute = options.onClientRoute
75 } 81 }
76 82
77 static getPluginPathPrefix (isTheme: boolean) { 83 static getPluginPathPrefix (isTheme: boolean) {
78 return isTheme ? '/themes' : '/plugins' 84 return isTheme ? '/themes' : '/plugins'
79 } 85 }
80 86
87 static getExternalAuthHref (auth: RegisteredExternalAuthConfig) {
88 return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}`
89
90 }
91
81 loadPluginsList (config: HTMLServerConfig) { 92 loadPluginsList (config: HTMLServerConfig) {
82 for (const plugin of config.plugin.registered) { 93 for (const plugin of config.plugin.registered) {
83 this.addPlugin(plugin) 94 this.addPlugin(plugin)
@@ -215,7 +226,7 @@ class PluginsManager {
215 return this.onFormFields(commonOptions, videoFormOptions) 226 return this.onFormFields(commonOptions, videoFormOptions)
216 } 227 }
217 228
218 const registerSettingsScript = (options: RegisterClientSettingsScript) => { 229 const registerSettingsScript = (options: RegisterClientSettingsScriptOptions) => {
219 if (!this.onSettingsScripts) { 230 if (!this.onSettingsScripts) {
220 throw new Error('Registering settings script is not supported') 231 throw new Error('Registering settings script is not supported')
221 } 232 }
@@ -223,13 +234,29 @@ class PluginsManager {
223 return this.onSettingsScripts(pluginInfo, options) 234 return this.onSettingsScripts(pluginInfo, options)
224 } 235 }
225 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
226 const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo) 245 const peertubeHelpers = this.peertubeHelpersFactory(pluginInfo)
227 246
228 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)
229 248
230 const absURL = (environment.apiUrl || window.location.origin) + clientScript.script 249 const absURL = (environment.apiUrl || window.location.origin) + clientScript.script
231 return dynamicImport(absURL) 250 return dynamicImport(absURL)
232 .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 })
233 .then(() => this.sortHooksByPriority()) 260 .then(() => this.sortHooksByPriority())
234 .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))
235 } 262 }