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 { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
8 import { PluginApiService } from '../shared/plugin-api.service'
11 selector: 'my-plugin-show-installed',
12 templateUrl: './plugin-show-installed.component.html',
13 styleUrls: [ './plugin-show-installed.component.scss' ]
15 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
16 plugin: PeerTubePlugin
17 registeredSettings: RegisterServerSettingOptions[] = []
18 pluginTypeLabel: string
20 private sub: Subscription
23 protected formValidatorService: FormValidatorService,
24 private pluginService: PluginApiService,
25 private notifier: Notifier,
26 private route: ActivatedRoute
32 this.sub = this.route.params.subscribe(
34 const npmName = routeParams['npmName']
36 this.loadPlugin(npmName)
42 if (this.sub) this.sub.unsubscribe()
46 const settings = this.form.value
48 this.pluginService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
51 this.notifier.success($localize`Settings updated.`)
54 err => this.notifier.error(err.message)
58 hasRegisteredSettings () {
59 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
62 private loadPlugin (npmName: string) {
63 this.pluginService.getPlugin(npmName)
64 .pipe(switchMap(plugin => {
65 return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
66 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
69 ({ plugin, registeredSettings }) => {
71 this.registeredSettings = registeredSettings
73 this.pluginTypeLabel = this.pluginService.getPluginTypeLabel(this.plugin.type)
75 this.buildSettingsForm()
78 err => this.notifier.error(err.message)
82 private buildSettingsForm () {
83 const buildOptions: BuildFormArgument = {}
84 const settingsValues: any = {}
86 for (const setting of this.registeredSettings) {
87 buildOptions[ setting.name ] = null
88 settingsValues[ setting.name ] = this.getSetting(setting.name)
91 this.buildForm(buildOptions)
93 this.form.patchValue(settingsValues)
96 private getSetting (name: string) {
97 const settings = this.plugin.settings
99 if (settings && settings[name]) return settings[name]
101 const registered = this.registeredSettings.find(r => r.name === name)
103 return registered.default