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
24 protected formValidatorService: FormValidatorService,
25 private pluginService: PluginService,
26 private pluginAPIService: PluginApiService,
27 private notifier: Notifier,
28 private route: ActivatedRoute
34 this.sub = this.route.params.subscribe(
36 const npmName = routeParams['npmName']
38 this.loadPlugin(npmName)
44 if (this.sub) this.sub.unsubscribe()
48 const settings = this.form.value
50 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
53 this.notifier.success($localize`Settings updated.`)
56 err => this.notifier.error(err.message)
60 hasRegisteredSettings () {
61 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
64 isSettingHidden (setting: RegisterServerSettingOptions) {
68 private loadPlugin (npmName: string) {
69 this.pluginAPIService.getPlugin(npmName)
70 .pipe(switchMap(plugin => {
71 return this.pluginAPIService.getPluginRegisteredSettings(plugin.name, plugin.type)
72 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
75 async ({ plugin, registeredSettings }) => {
77 this.registeredSettings = await this.translateSettings(registeredSettings)
79 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
81 this.buildSettingsForm()
84 err => this.notifier.error(err.message)
88 private buildSettingsForm () {
89 const buildOptions: BuildFormArgument = {}
90 const settingsValues: any = {}
92 for (const setting of this.registeredSettings) {
93 buildOptions[ setting.name ] = null
94 settingsValues[ setting.name ] = this.getSetting(setting.name)
97 this.buildForm(buildOptions)
99 this.form.patchValue(settingsValues)
102 private getSetting (name: string) {
103 const settings = this.plugin.settings
105 if (settings && settings[name] !== undefined) return settings[name]
107 const registered = this.registeredSettings.find(r => r.name === name)
109 return registered.default
112 private async translateSettings (settings: RegisterServerSettingOptions[]) {
113 const npmName = this.pluginService.nameToNpmName(this.plugin.name, this.plugin.type)
115 for (const setting of settings) {
116 for (const key of [ 'label', 'html', 'descriptionHTML' ]) {
117 if (setting[key]) setting[key] = await this.pluginService.translateBy(npmName, setting[key])
120 if (Array.isArray(setting.options)) {
121 const newOptions = []
123 for (const o of setting.options) {
126 label: await this.pluginService.translateBy(npmName, o.label)
130 setting.options = newOptions