]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Add ability to hide plugin settings
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
CommitLineData
dba85a1e
C
1import { Subscription } from 'rxjs'
2import { map, switchMap } from 'rxjs/operators'
67ed6552
C
3import { Component, OnDestroy, OnInit } from '@angular/core'
4import { ActivatedRoute } from '@angular/router'
c713017f 5import { Notifier, PluginService } from '@app/core'
7ed1edbb
C
6import { BuildFormArgument } from '@app/shared/form-validators'
7import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
67ed6552
C
8import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
9import { PluginApiService } from '../shared/plugin-api.service'
d00dc28d
C
10
11@Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15})
109d893f 16export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
dba85a1e 17 plugin: PeerTubePlugin
9ae88819 18 registeredSettings: RegisterServerSettingOptions[] = []
dba85a1e
C
19 pluginTypeLabel: string
20
21 private sub: Subscription
3c47fa3b 22 private npmName: string
dba85a1e
C
23
24 constructor (
25 protected formValidatorService: FormValidatorService,
c713017f
C
26 private pluginService: PluginService,
27 private pluginAPIService: PluginApiService,
dba85a1e
C
28 private notifier: Notifier,
29 private route: ActivatedRoute
30 ) {
31 super()
32 }
d00dc28d
C
33
34 ngOnInit () {
dba85a1e
C
35 this.sub = this.route.params.subscribe(
36 routeParams => {
3c47fa3b 37 this.npmName = routeParams['npmName']
dba85a1e 38
3c47fa3b 39 this.loadPlugin(this.npmName)
dba85a1e
C
40 }
41 )
42 }
43
44 ngOnDestroy () {
45 if (this.sub) this.sub.unsubscribe()
46 }
47
48 formValidated () {
49 const settings = this.form.value
50
c713017f 51 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
dba85a1e
C
52 .subscribe(
53 () => {
66357162 54 this.notifier.success($localize`Settings updated.`)
dba85a1e
C
55 },
56
57 err => this.notifier.error(err.message)
58 )
59 }
60
61 hasRegisteredSettings () {
62 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
63 }
64
c713017f 65 isSettingHidden (setting: RegisterServerSettingOptions) {
3c47fa3b
C
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 })
c713017f
C
71 }
72
dba85a1e 73 private loadPlugin (npmName: string) {
c713017f 74 this.pluginAPIService.getPlugin(npmName)
dba85a1e 75 .pipe(switchMap(plugin => {
c713017f 76 return this.pluginAPIService.getPluginRegisteredSettings(plugin.name, plugin.type)
ba211e73 77 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
dba85a1e
C
78 }))
79 .subscribe(
c713017f 80 async ({ plugin, registeredSettings }) => {
dba85a1e 81 this.plugin = plugin
3c47fa3b 82
c713017f 83 this.registeredSettings = await this.translateSettings(registeredSettings)
dba85a1e 84
c713017f 85 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
dba85a1e
C
86
87 this.buildSettingsForm()
88 },
89
90 err => this.notifier.error(err.message)
91 )
92 }
93
94 private buildSettingsForm () {
dba85a1e
C
95 const buildOptions: BuildFormArgument = {}
96 const settingsValues: any = {}
97
98 for (const setting of this.registeredSettings) {
99 buildOptions[ setting.name ] = null
100 settingsValues[ setting.name ] = this.getSetting(setting.name)
101 }
102
103 this.buildForm(buildOptions)
104
105 this.form.patchValue(settingsValues)
106 }
107
108 private getSetting (name: string) {
109 const settings = this.plugin.settings
110
253d4ab6 111 if (settings && settings[name] !== undefined) return settings[name]
dba85a1e
C
112
113 const registered = this.registeredSettings.find(r => r.name === name)
d00dc28d 114
dba85a1e 115 return registered.default
d00dc28d
C
116 }
117
c713017f 118 private async translateSettings (settings: RegisterServerSettingOptions[]) {
c713017f
C
119 for (const setting of settings) {
120 for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
3c47fa3b 121 if (setting[key]) setting[key] = await this.pluginService.translateBy(this.npmName, setting[key])
c713017f
C
122 }
123
124 if (Array.isArray(setting.options)) {
125 const newOptions = []
126
127 for (const o of setting.options) {
128 newOptions.push({
129 value: o.value,
3c47fa3b 130 label: await this.pluginService.translateBy(this.npmName, o.label)
c713017f
C
131 })
132 }
133
134 setting.options = newOptions
135 }
136 }
137
138 return settings
139 }
140
d00dc28d 141}