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 } 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: PluginApiService,
26 private notifier: Notifier,
27 private route: ActivatedRoute
33 this.sub = this.route.params.subscribe(
35 const npmName = routeParams['npmName']
37 this.loadPlugin(npmName)
43 if (this.sub) this.sub.unsubscribe()
47 const settings = this.form.value
49 this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
52 this.notifier.success($localize`Settings updated.`)
55 err => this.notifier.error(err.message)
59 hasRegisteredSettings () {
60 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
63 private loadPlugin (npmName: string) {
64 this.pluginService.getPlugin(npmName)
65 .pipe(switchMap(plugin => {
66 return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
67 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
70 ({ plugin, registeredSettings }) => {
72 this.registeredSettings = registeredSettings
74 this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
76 this.buildSettingsForm()
79 err => this.notifier.error(err.message)
83 private buildSettingsForm () {
84 const buildOptions: BuildFormArgument = {}
85 const settingsValues: any = {}
87 for (const setting of this.registeredSettings) {
88 buildOptions[ setting.name ] = null
89 settingsValues[ setting.name ] = this.getSetting(setting.name)
92 this.buildForm(buildOptions)
94 this.form.patchValue(settingsValues)
97 private getSetting (name: string) {
98 const settings = this.plugin.settings
100 if (settings && settings[name] !== undefined) return settings[name]
102 const registered = this.registeredSettings.find(r => r.name === name)
104 return registered.default