]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Merge branch 'release/3.1.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
1 import { Subscription } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { Notifier, PluginService } from '@app/core'
6 import { BuildFormArgument } from '@app/shared/form-validators'
7 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
8 import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
9 import { PluginApiService } from '../shared/plugin-api.service'
10
11 @Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15 })
16 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
17 plugin: PeerTubePlugin
18 registeredSettings: RegisterServerSettingOptions[] = []
19 pluginTypeLabel: string
20
21 private sub: Subscription
22 private npmName: string
23
24 constructor (
25 protected formValidatorService: FormValidatorService,
26 private pluginService: PluginService,
27 private pluginAPIService: PluginApiService,
28 private notifier: Notifier,
29 private route: ActivatedRoute
30 ) {
31 super()
32 }
33
34 ngOnInit () {
35 this.sub = this.route.params.subscribe(
36 routeParams => {
37 this.npmName = routeParams['npmName']
38
39 this.loadPlugin(this.npmName)
40 }
41 )
42 }
43
44 ngOnDestroy () {
45 if (this.sub) this.sub.unsubscribe()
46 }
47
48 formValidated () {
49 const settings = this.form.value
50
51 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
52 .subscribe(
53 () => {
54 this.notifier.success($localize`Settings updated.`)
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
65 isSettingHidden (setting: RegisterServerSettingOptions) {
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 })
71 }
72
73 private loadPlugin (npmName: string) {
74 this.pluginAPIService.getPlugin(npmName)
75 .pipe(switchMap(plugin => {
76 return this.pluginAPIService.getPluginRegisteredSettings(plugin.name, plugin.type)
77 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
78 }))
79 .subscribe(
80 async ({ plugin, registeredSettings }) => {
81 this.plugin = plugin
82
83 this.registeredSettings = await this.translateSettings(registeredSettings)
84
85 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
86
87 this.buildSettingsForm()
88 },
89
90 err => this.notifier.error(err.message)
91 )
92 }
93
94 private buildSettingsForm () {
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
111 if (settings && settings[name] !== undefined) return settings[name]
112
113 const registered = this.registeredSettings.find(r => r.name === name)
114
115 return registered.default
116 }
117
118 private async translateSettings (settings: RegisterServerSettingOptions[]) {
119 for (const setting of settings) {
120 for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
121 if (setting[key]) setting[key] = await this.pluginService.translateBy(this.npmName, setting[key])
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,
130 label: await this.pluginService.translateBy(this.npmName, o.label)
131 })
132 }
133
134 setting.options = newOptions
135 }
136 }
137
138 return settings
139 }
140
141 }