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, FormReactive, FormValidatorService } from '@app/shared/shared-forms'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
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,
26 private pluginService: 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.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
53 this.notifier.success(this.i18n('Settings updated.'))
56 err => this.notifier.error(err.message)
60 hasRegisteredSettings () {
61 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
64 private loadPlugin (npmName: string) {
65 this.pluginService.getPlugin(npmName)
66 .pipe(switchMap(plugin => {
67 return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
68 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
71 ({ plugin, registeredSettings }) => {
73 this.registeredSettings = registeredSettings
75 this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
77 this.buildSettingsForm()
80 err => this.notifier.error(err.message)
84 private buildSettingsForm () {
85 const buildOptions: BuildFormArgument = {}
86 const settingsValues: any = {}
88 for (const setting of this.registeredSettings) {
89 buildOptions[ setting.name ] = null
90 settingsValues[ setting.name ] = this.getSetting(setting.name)
93 this.buildForm(buildOptions)
95 this.form.patchValue(settingsValues)
98 private getSetting (name: string) {
99 const settings = this.plugin.settings
101 if (settings && settings[name]) return settings[name]
103 const registered = this.registeredSettings.find(r => r.name === name)
105 return registered.default