]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Add channel hooks
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
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'
10
11 @Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15 })
16 export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
17 plugin: PeerTubePlugin
18 registeredSettings: RegisterServerSettingOptions[] = []
19 pluginTypeLabel: string
20
21 private sub: Subscription
22 private npmName: string
23
24 constructor (
25 protected formValidatorService: FormValidatorService,
26 private pluginService: PluginService,
27 private pluginAPIService: PluginApiService,
28 private notifier: Notifier,
29 private hooks: HooksService,
30 private route: ActivatedRoute
31 ) {
32 super()
33 }
34
35 ngOnInit () {
36 this.sub = this.route.params.subscribe(
37 routeParams => {
38 this.npmName = routeParams['npmName']
39
40 this.loadPlugin(this.npmName)
41 }
42 )
43 }
44
45 ngOnDestroy () {
46 if (this.sub) this.sub.unsubscribe()
47 }
48
49 formValidated () {
50 const settings = this.form.value
51
52 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
53 .subscribe({
54 next: () => {
55 this.notifier.success($localize`Settings updated.`)
56 },
57
58 error: err => this.notifier.error(err.message)
59 })
60 }
61
62 hasRegisteredSettings () {
63 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
64 }
65
66 isSettingHidden (setting: RegisterServerSettingOptions) {
67 const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
68
69 if (!script?.isSettingHidden) return false
70
71 return script.isSettingHidden({ setting, formValues: this.form.value })
72 }
73
74 getWrapperId (setting: RegisterServerSettingOptions) {
75 if (!setting.name) return
76
77 return setting.name + '-wrapper'
78 }
79
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 })))
85 }))
86 .subscribe({
87 next: async ({ plugin, registeredSettings }) => {
88 this.plugin = plugin
89
90 this.registeredSettings = await this.translateSettings(registeredSettings)
91
92 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
93
94 this.buildSettingsForm()
95 },
96
97 error: err => this.notifier.error(err.message)
98 })
99 }
100
101 private buildSettingsForm () {
102 const buildOptions: BuildFormArgument = {}
103 const settingsValues: any = {}
104
105 for (const setting of this.registeredSettings) {
106 buildOptions[setting.name] = null
107 settingsValues[setting.name] = this.getSetting(setting.name)
108 }
109
110 this.buildForm(buildOptions)
111
112 this.form.patchValue(settingsValues)
113
114 this.hooks.runAction('action:admin-plugin-settings.init', 'admin-plugin', { npmName: this.npmName })
115 }
116
117 private getSetting (name: string) {
118 const settings = this.plugin.settings
119
120 if (settings?.[name] !== undefined) return settings[name]
121
122 const registered = this.registeredSettings.find(r => r.name === name)
123
124 return registered.default
125 }
126
127 private async translateSettings (settings: RegisterServerSettingOptions[]) {
128 for (const setting of settings) {
129 await this.pluginService.translateSetting(this.npmName, setting)
130 }
131
132 return settings
133 }
134 }