]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/plugins/plugin.service.ts
c6ba3dd17bf59dca7adb5846a02dc4ab14217eb2
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
1 import { Injectable } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { ServerConfigPlugin } from '@shared/models'
4 import { ServerService } from '@app/core/server/server.service'
5 import { ClientScript } from '@shared/models/plugins/plugin-package-json.model'
6 import { PluginScope } from '@shared/models/plugins/plugin-scope.type'
7 import { environment } from '../../../environments/environment'
8 import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model'
9 import { ReplaySubject } from 'rxjs'
10 import { first, shareReplay } from 'rxjs/operators'
11
12 interface HookStructValue extends RegisterHookOptions {
13 plugin: ServerConfigPlugin
14 clientScript: ClientScript
15 }
16
17 @Injectable()
18 export class PluginService {
19 pluginsLoaded = new ReplaySubject<boolean>(1)
20
21 private plugins: ServerConfigPlugin[] = []
22 private scopes: { [ scopeName: string ]: { plugin: ServerConfigPlugin, clientScript: ClientScript }[] } = {}
23 private loadedScripts: { [ script: string ]: boolean } = {}
24 private loadedScopes: PluginScope[] = []
25
26 private hooks: { [ name: string ]: HookStructValue[] } = {}
27
28 constructor (
29 private router: Router,
30 private server: ServerService
31 ) {
32 }
33
34 initializePlugins () {
35 this.server.configLoaded
36 .subscribe(() => {
37 this.plugins = this.server.getConfig().plugin.registered
38
39 this.buildScopeStruct()
40
41 this.pluginsLoaded.next(true)
42 })
43 }
44
45 ensurePluginsAreLoaded () {
46 return this.pluginsLoaded.asObservable()
47 .pipe(first(), shareReplay())
48 .toPromise()
49 }
50
51 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
52 const pathPrefix = isTheme ? '/themes' : '/plugins'
53
54 for (const key of Object.keys(plugin.clientScripts)) {
55 const clientScript = plugin.clientScripts[key]
56
57 for (const scope of clientScript.scopes) {
58 if (!this.scopes[scope]) this.scopes[scope] = []
59
60 this.scopes[scope].push({
61 plugin,
62 clientScript: {
63 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
64 scopes: clientScript.scopes
65 }
66 })
67
68 this.loadedScripts[clientScript.script] = false
69 }
70 }
71 }
72
73 removePlugin (plugin: ServerConfigPlugin) {
74 for (const key of Object.keys(this.scopes)) {
75 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
76 }
77 }
78
79 async reloadLoadedScopes () {
80 for (const scope of this.loadedScopes) {
81 await this.loadPluginsByScope(scope)
82 }
83 }
84
85 async loadPluginsByScope (scope: PluginScope) {
86 try {
87 await this.ensurePluginsAreLoaded()
88
89 this.loadedScopes.push(scope)
90
91 const toLoad = this.scopes[ scope ]
92 if (!Array.isArray(toLoad)) return
93
94 const promises: Promise<any>[] = []
95 for (const { plugin, clientScript } of toLoad) {
96 if (this.loadedScripts[ clientScript.script ]) continue
97
98 promises.push(this.loadPlugin(plugin, clientScript))
99
100 this.loadedScripts[ clientScript.script ] = true
101 }
102
103 await Promise.all(promises)
104 } catch (err) {
105 console.error('Cannot load plugins by scope %s.', scope, err)
106 }
107 }
108
109 async runHook (hookName: string, param?: any) {
110 let result = param
111
112 const wait = hookName.startsWith('static:')
113
114 for (const hook of this.hooks[hookName]) {
115 try {
116 if (wait) result = await hook.handler(param)
117 else result = hook.handler()
118 } catch (err) {
119 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
120 }
121 }
122
123 return result
124 }
125
126 private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) {
127 const registerHook = (options: RegisterHookOptions) => {
128 if (!this.hooks[options.target]) this.hooks[options.target] = []
129
130 this.hooks[options.target].push({
131 plugin,
132 clientScript,
133 target: options.target,
134 handler: options.handler,
135 priority: options.priority || 0
136 })
137 }
138
139 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
140
141 return import(/* webpackIgnore: true */ clientScript.script)
142 .then(script => script.register({ registerHook }))
143 .then(() => this.sortHooksByPriority())
144 }
145
146 private buildScopeStruct () {
147 for (const plugin of this.plugins) {
148 this.addPlugin(plugin)
149 }
150 }
151
152 private sortHooksByPriority () {
153 for (const hookName of Object.keys(this.hooks)) {
154 this.hooks[hookName].sort((a, b) => {
155 return b.priority - a.priority
156 })
157 }
158 }
159 }