Add ability to hide plugin settings

This commit is contained in:
Chocobozzz 2021-04-09 13:50:31 +02:00
parent c713017f3c
commit 3c47fa3bc0
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
7 changed files with 50 additions and 13 deletions

View File

@ -19,6 +19,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
pluginTypeLabel: string
private sub: Subscription
private npmName: string
constructor (
protected formValidatorService: FormValidatorService,
@ -33,9 +34,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
ngOnInit () {
this.sub = this.route.params.subscribe(
routeParams => {
const npmName = routeParams['npmName']
this.npmName = routeParams['npmName']
this.loadPlugin(npmName)
this.loadPlugin(this.npmName)
}
)
}
@ -62,7 +63,11 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
}
isSettingHidden (setting: RegisterServerSettingOptions) {
return false
const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
if (!script?.isSettingHidden) return false
return script.isSettingHidden({ setting, formValues: this.form.value })
}
private loadPlugin (npmName: string) {
@ -74,6 +79,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
.subscribe(
async ({ plugin, registeredSettings }) => {
this.plugin = plugin
this.registeredSettings = await this.translateSettings(registeredSettings)
this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
@ -110,11 +116,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
}
private async translateSettings (settings: RegisterServerSettingOptions[]) {
const npmName = this.pluginService.nameToNpmName(this.plugin.name, this.plugin.type)
for (const setting of settings) {
for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
if (setting[key]) setting[key] = await this.pluginService.translateBy(npmName, setting[key])
if (setting[key]) setting[key] = await this.pluginService.translateBy(this.npmName, setting[key])
}
if (Array.isArray(setting.options)) {
@ -123,7 +127,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
for (const o of setting.options) {
newOptions.push({
value: o.value,
label: await this.pluginService.translateBy(npmName, o.label)
label: await this.pluginService.translateBy(this.npmName, o.label)
})
}

View File

@ -19,6 +19,7 @@ import {
PluginTranslation,
PluginType,
PublicServerSetting,
RegisterClientSettingsScript,
ServerConfigPlugin
} from '@shared/models'
import { environment } from '../../../environments/environment'
@ -58,6 +59,7 @@ export class PluginService implements ClientHook {
private formFields: FormFields = {
video: []
}
private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
constructor (
private authService: AuthService,
@ -200,6 +202,10 @@ export class PluginService implements ClientHook {
return this.formFields.video.filter(f => f.videoFormOptions.type === type)
}
getRegisteredSettingsScript (npmName: string) {
return this.settingsScripts[npmName]
}
translateBy (npmName: string, toTranslate: string) {
const helpers = this.helpers[npmName]
if (!helpers) {
@ -220,6 +226,7 @@ export class PluginService implements ClientHook {
return loadPlugin({
hooks: this.hooks,
formFields: this.formFields,
onSettingsScripts: options => this.settingsScripts[npmName] = options,
pluginInfo,
peertubeHelpersFactory: () => helpers
})

View File

@ -7,7 +7,8 @@ import {
ClientScript,
PluginType,
RegisterClientHookOptions,
ServerConfigPlugin
ServerConfigPlugin,
RegisterClientSettingsScript
} from '../../../shared/models'
import { ClientScript as ClientScriptModule } from '../types/client-script.model'
import { importModule } from './utils'
@ -54,8 +55,9 @@ function loadPlugin (options: {
pluginInfo: PluginInfo
peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers
formFields?: FormFields
onSettingsScripts?: (options: RegisterClientSettingsScript) => void
}) {
const { hooks, pluginInfo, peertubeHelpersFactory, formFields } = options
const { hooks, pluginInfo, peertubeHelpersFactory, formFields, onSettingsScripts } = options
const { plugin, clientScript } = pluginInfo
const registerHook = (options: RegisterClientHookOptions) => {
@ -86,12 +88,20 @@ function loadPlugin (options: {
})
}
const registerSettingsScript = (options: RegisterClientSettingsScript) => {
if (!onSettingsScripts) {
throw new Error('Registering settings script is not supported')
}
return onSettingsScripts(options)
}
const peertubeHelpers = peertubeHelpersFactory(pluginInfo)
console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
return importModule(clientScript.script)
.then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, peertubeHelpers }))
.then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers }))
.then(() => sortHooksByPriority(hooks))
.catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
}

View File

@ -759,6 +759,7 @@ export class PeerTubeEmbed {
await loadPlugin({
hooks: this.peertubeHooks,
pluginInfo,
onSettingsScripts: () => undefined,
peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations)
})
}

View File

@ -1,12 +1,18 @@
import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model'
import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
import { ServerConfig } from '@shared/models/server'
import {
RegisterClientFormFieldOptions,
RegisterClientHookOptions,
RegisterClientSettingsScript,
RegisterClientVideoFieldOptions,
ServerConfig
} from '@shared/models'
export type RegisterClientOptions = {
registerHook: (options: RegisterClientHookOptions) => void
registerVideoField: (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void
registerSettingsScript: (options: RegisterClientSettingsScript) => void
peertubeHelpers: RegisterClientHelpers
}

View File

@ -20,6 +20,7 @@ export * from './plugin-video-privacy-manager.model'
export * from './plugin.type'
export * from './public-server.setting'
export * from './register-client-hook.model'
export * from './register-client-settings-script.model'
export * from './register-client-form-field.model'
export * from './register-server-hook.model'
export * from './register-server-setting.model'

View File

@ -0,0 +1,8 @@
import { RegisterServerSettingOptions } from "./register-server-setting.model"
export interface RegisterClientSettingsScript {
isSettingHidden (options: {
setting: RegisterServerSettingOptions
formValues: { [name: string]: any }
}): boolean
}