aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-04-09 13:50:31 +0200
committerChocobozzz <me@florianbigard.com>2021-04-09 13:50:31 +0200
commit3c47fa3bc0e3f2362bb17976057287a7e9aba46b (patch)
tree0f74d23e219e7c917034d9cd394fabc4668f16fa
parentc713017f3cd2d617da22c24c579342ec4121cfcd (diff)
downloadPeerTube-3c47fa3bc0e3f2362bb17976057287a7e9aba46b.tar.gz
PeerTube-3c47fa3bc0e3f2362bb17976057287a7e9aba46b.tar.zst
PeerTube-3c47fa3bc0e3f2362bb17976057287a7e9aba46b.zip
Add ability to hide plugin settings
-rw-r--r--client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts18
-rw-r--r--client/src/app/core/plugins/plugin.service.ts7
-rw-r--r--client/src/root-helpers/plugins.ts16
-rw-r--r--client/src/standalone/videos/embed.ts1
-rw-r--r--client/src/types/register-client-option.model.ts12
-rw-r--r--shared/models/plugins/index.ts1
-rw-r--r--shared/models/plugins/register-client-settings-script.model.ts8
7 files changed, 50 insertions, 13 deletions
diff --git a/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts b/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
index 2c5dbea95..ca9ad9922 100644
--- a/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
+++ b/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
@@ -19,6 +19,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
19 pluginTypeLabel: string 19 pluginTypeLabel: string
20 20
21 private sub: Subscription 21 private sub: Subscription
22 private npmName: string
22 23
23 constructor ( 24 constructor (
24 protected formValidatorService: FormValidatorService, 25 protected formValidatorService: FormValidatorService,
@@ -33,9 +34,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
33 ngOnInit () { 34 ngOnInit () {
34 this.sub = this.route.params.subscribe( 35 this.sub = this.route.params.subscribe(
35 routeParams => { 36 routeParams => {
36 const npmName = routeParams['npmName'] 37 this.npmName = routeParams['npmName']
37 38
38 this.loadPlugin(npmName) 39 this.loadPlugin(this.npmName)
39 } 40 }
40 ) 41 )
41 } 42 }
@@ -62,7 +63,11 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
62 } 63 }
63 64
64 isSettingHidden (setting: RegisterServerSettingOptions) { 65 isSettingHidden (setting: RegisterServerSettingOptions) {
65 return false 66 const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
67
68 if (!script?.isSettingHidden) return false
69
70 return script.isSettingHidden({ setting, formValues: this.form.value })
66 } 71 }
67 72
68 private loadPlugin (npmName: string) { 73 private loadPlugin (npmName: string) {
@@ -74,6 +79,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
74 .subscribe( 79 .subscribe(
75 async ({ plugin, registeredSettings }) => { 80 async ({ plugin, registeredSettings }) => {
76 this.plugin = plugin 81 this.plugin = plugin
82
77 this.registeredSettings = await this.translateSettings(registeredSettings) 83 this.registeredSettings = await this.translateSettings(registeredSettings)
78 84
79 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type) 85 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
@@ -110,11 +116,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
110 } 116 }
111 117
112 private async translateSettings (settings: RegisterServerSettingOptions[]) { 118 private async translateSettings (settings: RegisterServerSettingOptions[]) {
113 const npmName = this.pluginService.nameToNpmName(this.plugin.name, this.plugin.type)
114
115 for (const setting of settings) { 119 for (const setting of settings) {
116 for (const key of [ 'label', 'html', 'descriptionHTML' ]) { 120 for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
117 if (setting[key]) setting[key] = await this.pluginService.translateBy(npmName, setting[key]) 121 if (setting[key]) setting[key] = await this.pluginService.translateBy(this.npmName, setting[key])
118 } 122 }
119 123
120 if (Array.isArray(setting.options)) { 124 if (Array.isArray(setting.options)) {
@@ -123,7 +127,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
123 for (const o of setting.options) { 127 for (const o of setting.options) {
124 newOptions.push({ 128 newOptions.push({
125 value: o.value, 129 value: o.value,
126 label: await this.pluginService.translateBy(npmName, o.label) 130 label: await this.pluginService.translateBy(this.npmName, o.label)
127 }) 131 })
128 } 132 }
129 133
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts
index c2dcf9fef..1243bac67 100644
--- a/client/src/app/core/plugins/plugin.service.ts
+++ b/client/src/app/core/plugins/plugin.service.ts
@@ -19,6 +19,7 @@ import {
19 PluginTranslation, 19 PluginTranslation,
20 PluginType, 20 PluginType,
21 PublicServerSetting, 21 PublicServerSetting,
22 RegisterClientSettingsScript,
22 ServerConfigPlugin 23 ServerConfigPlugin
23} from '@shared/models' 24} from '@shared/models'
24import { environment } from '../../../environments/environment' 25import { environment } from '../../../environments/environment'
@@ -58,6 +59,7 @@ export class PluginService implements ClientHook {
58 private formFields: FormFields = { 59 private formFields: FormFields = {
59 video: [] 60 video: []
60 } 61 }
62 private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
61 63
62 constructor ( 64 constructor (
63 private authService: AuthService, 65 private authService: AuthService,
@@ -200,6 +202,10 @@ export class PluginService implements ClientHook {
200 return this.formFields.video.filter(f => f.videoFormOptions.type === type) 202 return this.formFields.video.filter(f => f.videoFormOptions.type === type)
201 } 203 }
202 204
205 getRegisteredSettingsScript (npmName: string) {
206 return this.settingsScripts[npmName]
207 }
208
203 translateBy (npmName: string, toTranslate: string) { 209 translateBy (npmName: string, toTranslate: string) {
204 const helpers = this.helpers[npmName] 210 const helpers = this.helpers[npmName]
205 if (!helpers) { 211 if (!helpers) {
@@ -220,6 +226,7 @@ export class PluginService implements ClientHook {
220 return loadPlugin({ 226 return loadPlugin({
221 hooks: this.hooks, 227 hooks: this.hooks,
222 formFields: this.formFields, 228 formFields: this.formFields,
229 onSettingsScripts: options => this.settingsScripts[npmName] = options,
223 pluginInfo, 230 pluginInfo,
224 peertubeHelpersFactory: () => helpers 231 peertubeHelpersFactory: () => helpers
225 }) 232 })
diff --git a/client/src/root-helpers/plugins.ts b/client/src/root-helpers/plugins.ts
index 4bc2c8eb2..5344c0468 100644
--- a/client/src/root-helpers/plugins.ts
+++ b/client/src/root-helpers/plugins.ts
@@ -7,7 +7,8 @@ import {
7 ClientScript, 7 ClientScript,
8 PluginType, 8 PluginType,
9 RegisterClientHookOptions, 9 RegisterClientHookOptions,
10 ServerConfigPlugin 10 ServerConfigPlugin,
11 RegisterClientSettingsScript
11} from '../../../shared/models' 12} from '../../../shared/models'
12import { ClientScript as ClientScriptModule } from '../types/client-script.model' 13import { ClientScript as ClientScriptModule } from '../types/client-script.model'
13import { importModule } from './utils' 14import { importModule } from './utils'
@@ -54,8 +55,9 @@ function loadPlugin (options: {
54 pluginInfo: PluginInfo 55 pluginInfo: PluginInfo
55 peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers 56 peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers
56 formFields?: FormFields 57 formFields?: FormFields
58 onSettingsScripts?: (options: RegisterClientSettingsScript) => void
57}) { 59}) {
58 const { hooks, pluginInfo, peertubeHelpersFactory, formFields } = options 60 const { hooks, pluginInfo, peertubeHelpersFactory, formFields, onSettingsScripts } = options
59 const { plugin, clientScript } = pluginInfo 61 const { plugin, clientScript } = pluginInfo
60 62
61 const registerHook = (options: RegisterClientHookOptions) => { 63 const registerHook = (options: RegisterClientHookOptions) => {
@@ -86,12 +88,20 @@ function loadPlugin (options: {
86 }) 88 })
87 } 89 }
88 90
91 const registerSettingsScript = (options: RegisterClientSettingsScript) => {
92 if (!onSettingsScripts) {
93 throw new Error('Registering settings script is not supported')
94 }
95
96 return onSettingsScripts(options)
97 }
98
89 const peertubeHelpers = peertubeHelpersFactory(pluginInfo) 99 const peertubeHelpers = peertubeHelpersFactory(pluginInfo)
90 100
91 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) 101 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
92 102
93 return importModule(clientScript.script) 103 return importModule(clientScript.script)
94 .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, peertubeHelpers })) 104 .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers }))
95 .then(() => sortHooksByPriority(hooks)) 105 .then(() => sortHooksByPriority(hooks))
96 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) 106 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
97} 107}
diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts
index ae8f176b7..103014bb0 100644
--- a/client/src/standalone/videos/embed.ts
+++ b/client/src/standalone/videos/embed.ts
@@ -759,6 +759,7 @@ export class PeerTubeEmbed {
759 await loadPlugin({ 759 await loadPlugin({
760 hooks: this.peertubeHooks, 760 hooks: this.peertubeHooks,
761 pluginInfo, 761 pluginInfo,
762 onSettingsScripts: () => undefined,
762 peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations) 763 peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations)
763 }) 764 })
764 } 765 }
diff --git a/client/src/types/register-client-option.model.ts b/client/src/types/register-client-option.model.ts
index 7e5356a2b..16c921344 100644
--- a/client/src/types/register-client-option.model.ts
+++ b/client/src/types/register-client-option.model.ts
@@ -1,12 +1,18 @@
1import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model' 1import {
2import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' 2 RegisterClientFormFieldOptions,
3import { ServerConfig } from '@shared/models/server' 3 RegisterClientHookOptions,
4 RegisterClientSettingsScript,
5 RegisterClientVideoFieldOptions,
6 ServerConfig
7} from '@shared/models'
4 8
5export type RegisterClientOptions = { 9export type RegisterClientOptions = {
6 registerHook: (options: RegisterClientHookOptions) => void 10 registerHook: (options: RegisterClientHookOptions) => void
7 11
8 registerVideoField: (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void 12 registerVideoField: (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void
9 13
14 registerSettingsScript: (options: RegisterClientSettingsScript) => void
15
10 peertubeHelpers: RegisterClientHelpers 16 peertubeHelpers: RegisterClientHelpers
11} 17}
12 18
diff --git a/shared/models/plugins/index.ts b/shared/models/plugins/index.ts
index 96621460a..740083f0e 100644
--- a/shared/models/plugins/index.ts
+++ b/shared/models/plugins/index.ts
@@ -20,6 +20,7 @@ export * from './plugin-video-privacy-manager.model'
20export * from './plugin.type' 20export * from './plugin.type'
21export * from './public-server.setting' 21export * from './public-server.setting'
22export * from './register-client-hook.model' 22export * from './register-client-hook.model'
23export * from './register-client-settings-script.model'
23export * from './register-client-form-field.model' 24export * from './register-client-form-field.model'
24export * from './register-server-hook.model' 25export * from './register-server-hook.model'
25export * from './register-server-setting.model' 26export * from './register-server-setting.model'
diff --git a/shared/models/plugins/register-client-settings-script.model.ts b/shared/models/plugins/register-client-settings-script.model.ts
new file mode 100644
index 000000000..ac16af366
--- /dev/null
+++ b/shared/models/plugins/register-client-settings-script.model.ts
@@ -0,0 +1,8 @@
1import { RegisterServerSettingOptions } from "./register-server-setting.model"
2
3export interface RegisterClientSettingsScript {
4 isSettingHidden (options: {
5 setting: RegisterServerSettingOptions
6 formValues: { [name: string]: any }
7 }): boolean
8}