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 { HooksService, 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 hooks: HooksService,
30 private route: ActivatedRoute
36 this.sub = this.route.params.subscribe(
38 this.npmName = routeParams['npmName']
40 this.loadPlugin(this.npmName)
46 if (this.sub) this.sub.unsubscribe()
50 const settings = this.form.value
52 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
55 this.notifier.success($localize`Settings updated.`)
58 error: err => this.notifier.error(err.message)
62 hasRegisteredSettings () {
63 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
66 isSettingHidden (setting: RegisterServerSettingOptions) {
67 const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
69 if (!script?.isSettingHidden) return false
71 return script.isSettingHidden({ setting, formValues: this.form.value })
74 getWrapperId (setting: RegisterServerSettingOptions) {
75 if (!setting.name) return
77 return setting.name + '-wrapper'
80 private loadPlugin (npmName: string) {
81 this.pluginAPIService.getPlugin(npmName)
82 .pipe(switchMap(plugin => {
83 return this.pluginAPIService.getPluginRegisteredSettings(plugin.name, plugin.type)
84 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
87 next: async ({ plugin, registeredSettings }) => {
90 this.registeredSettings = await this.translateSettings(registeredSettings)
92 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
94 this.buildSettingsForm()
97 error: err => this.notifier.error(err.message)
101 private buildSettingsForm () {
102 const buildOptions: BuildFormArgument = {}
103 const settingsValues: any = {}
105 for (const setting of this.registeredSettings) {
106 buildOptions[setting.name] = null
107 settingsValues[setting.name] = this.getSetting(setting.name)
110 this.buildForm(buildOptions)
112 this.form.patchValue(settingsValues)
114 setTimeout(() => this.hooks.runAction('action:admin-plugin-settings.init', 'admin-plugin', { npmName: this.npmName }))
117 private getSetting (name: string) {
118 const settings = this.plugin.settings
120 if (settings?.[name] !== undefined) return settings[name]
122 const registered = this.registeredSettings.find(r => r.name === name)
124 return registered.default
127 private async translateSettings (settings: RegisterServerSettingOptions[]) {
128 for (const setting of settings) {
129 await this.pluginService.translateSetting(this.npmName, setting)