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'
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
16 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
17 plugin: PeerTubePlugin
18 registeredSettings: RegisterServerSettingOptions[] = []
19 pluginTypeLabel: string
21 private sub: Subscription
22 private npmName: string
25 protected formValidatorService: FormValidatorService,
26 private pluginService: PluginService,
27 private pluginAPIService: PluginApiService,
28 private notifier: Notifier,
29 private route: ActivatedRoute
35 this.sub = this.route.params.subscribe(
37 this.npmName = routeParams['npmName']
39 this.loadPlugin(this.npmName)
45 if (this.sub) this.sub.unsubscribe()
49 const settings = this.form.value
51 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
54 this.notifier.success($localize`Settings updated.`)
57 err => this.notifier.error(err.message)
61 hasRegisteredSettings () {
62 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
65 isSettingHidden (setting: RegisterServerSettingOptions) {
66 const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
68 if (!script?.isSettingHidden) return false
70 return script.isSettingHidden({ setting, formValues: this.form.value })
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 })))
80 async ({ plugin, registeredSettings }) => {
83 this.registeredSettings = await this.translateSettings(registeredSettings)
85 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
87 this.buildSettingsForm()
90 err => this.notifier.error(err.message)
94 private buildSettingsForm () {
95 const buildOptions: BuildFormArgument = {}
96 const settingsValues: any = {}
98 for (const setting of this.registeredSettings) {
99 buildOptions[ setting.name ] = null
100 settingsValues[ setting.name ] = this.getSetting(setting.name)
103 this.buildForm(buildOptions)
105 this.form.patchValue(settingsValues)
108 private getSetting (name: string) {
109 const settings = this.plugin.settings
111 if (settings && settings[name] !== undefined) return settings[name]
113 const registered = this.registeredSettings.find(r => r.name === name)
115 return registered.default
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])
124 if (Array.isArray(setting.options)) {
125 const newOptions = []
127 for (const o of setting.options) {
130 label: await this.pluginService.translateBy(this.npmName, o.label)
134 setting.options = newOptions