]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/plugins/plugin.service.ts
Add client hooks
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
CommitLineData
18a6f04c
C
1import { Injectable } from '@angular/core'
2import { Router } from '@angular/router'
3import { ServerConfigPlugin } from '@shared/models'
4import { ServerService } from '@app/core/server/server.service'
5import { ClientScript } from '@shared/models/plugins/plugin-package-json.model'
18a6f04c 6import { environment } from '../../../environments/environment'
d00dc28d 7import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model'
18a6f04c 8import { ReplaySubject } from 'rxjs'
ffb321be 9import { first, shareReplay } from 'rxjs/operators'
93cae479
C
10import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
11import { ClientHook, ClientHookName } from '@shared/models/plugins/client-hook.model'
12import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
18a6f04c
C
13
14interface HookStructValue extends RegisterHookOptions {
15 plugin: ServerConfigPlugin
16 clientScript: ClientScript
17}
18
f0c5e8b6
C
19type PluginInfo = {
20 plugin: ServerConfigPlugin
21 clientScript: ClientScript
22 isTheme: boolean
23}
24
18a6f04c 25@Injectable()
93cae479
C
26export class PluginService implements ClientHook {
27 pluginsBuilt = new ReplaySubject<boolean>(1)
28
29 pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
30 common: new ReplaySubject<boolean>(1),
31 'video-watch': new ReplaySubject<boolean>(1)
32 }
18a6f04c
C
33
34 private plugins: ServerConfigPlugin[] = []
f0c5e8b6 35 private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
18a6f04c 36 private loadedScripts: { [ script: string ]: boolean } = {}
93cae479 37 private loadedScopes: PluginClientScope[] = []
18a6f04c
C
38
39 private hooks: { [ name: string ]: HookStructValue[] } = {}
40
41 constructor (
42 private router: Router,
43 private server: ServerService
44 ) {
45 }
46
47 initializePlugins () {
48 this.server.configLoaded
49 .subscribe(() => {
7cd4d2ba 50 this.plugins = this.server.getConfig().plugin.registered
18a6f04c
C
51
52 this.buildScopeStruct()
53
93cae479 54 this.pluginsBuilt.next(true)
18a6f04c
C
55 })
56 }
57
93cae479
C
58 ensurePluginsAreBuilt () {
59 return this.pluginsBuilt.asObservable()
60 .pipe(first(), shareReplay())
61 .toPromise()
62 }
63
64 ensurePluginsAreLoaded (scope: PluginClientScope) {
65 return this.pluginsLoaded[scope].asObservable()
ffb321be 66 .pipe(first(), shareReplay())
18a6f04c
C
67 .toPromise()
68 }
69
b5f919ac 70 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
f0c5e8b6 71 const pathPrefix = this.getPluginPathPrefix(isTheme)
b5f919ac 72
ffb321be
C
73 for (const key of Object.keys(plugin.clientScripts)) {
74 const clientScript = plugin.clientScripts[key]
75
76 for (const scope of clientScript.scopes) {
77 if (!this.scopes[scope]) this.scopes[scope] = []
78
79 this.scopes[scope].push({
80 plugin,
81 clientScript: {
b5f919ac 82 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
ffb321be 83 scopes: clientScript.scopes
f0c5e8b6
C
84 },
85 isTheme
ffb321be
C
86 })
87
88 this.loadedScripts[clientScript.script] = false
89 }
90 }
91 }
92
93 removePlugin (plugin: ServerConfigPlugin) {
94 for (const key of Object.keys(this.scopes)) {
95 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
96 }
97 }
98
99 async reloadLoadedScopes () {
100 for (const scope of this.loadedScopes) {
f0c5e8b6 101 await this.loadPluginsByScope(scope, true)
ffb321be
C
102 }
103 }
104
93cae479 105 async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
18a6f04c 106 try {
93cae479 107 await this.ensurePluginsAreBuilt()
18a6f04c 108
f0c5e8b6 109 if (!isReload) this.loadedScopes.push(scope)
ffb321be 110
18a6f04c
C
111 const toLoad = this.scopes[ scope ]
112 if (!Array.isArray(toLoad)) return
113
114 const promises: Promise<any>[] = []
f0c5e8b6
C
115 for (const pluginInfo of toLoad) {
116 const clientScript = pluginInfo.clientScript
117
18a6f04c
C
118 if (this.loadedScripts[ clientScript.script ]) continue
119
f0c5e8b6 120 promises.push(this.loadPlugin(pluginInfo))
18a6f04c
C
121
122 this.loadedScripts[ clientScript.script ] = true
123 }
124
ffb321be 125 await Promise.all(promises)
93cae479
C
126
127 this.pluginsLoaded[scope].next(true)
18a6f04c
C
128 } catch (err) {
129 console.error('Cannot load plugins by scope %s.', scope, err)
130 }
131 }
132
93cae479
C
133 async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
134 if (!this.hooks[hookName]) return Promise.resolve(result)
f0c5e8b6 135
93cae479 136 const hookType = getHookType(hookName)
18a6f04c
C
137
138 for (const hook of this.hooks[hookName]) {
93cae479
C
139 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
140
141 result = await internalRunHook(hook.handler, hookType, result, params, err => {
142 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
143 })
18a6f04c
C
144 }
145
146 return result
147 }
148
f0c5e8b6
C
149 private loadPlugin (pluginInfo: PluginInfo) {
150 const { plugin, clientScript } = pluginInfo
151
18a6f04c
C
152 const registerHook = (options: RegisterHookOptions) => {
153 if (!this.hooks[options.target]) this.hooks[options.target] = []
154
155 this.hooks[options.target].push({
156 plugin,
157 clientScript,
158 target: options.target,
159 handler: options.handler,
160 priority: options.priority || 0
161 })
162 }
163
f0c5e8b6
C
164 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
165
18a6f04c
C
166 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
167
ffb321be 168 return import(/* webpackIgnore: true */ clientScript.script)
f0c5e8b6 169 .then(script => script.register({ registerHook, peertubeHelpers }))
18a6f04c
C
170 .then(() => this.sortHooksByPriority())
171 }
172
173 private buildScopeStruct () {
174 for (const plugin of this.plugins) {
ffb321be 175 this.addPlugin(plugin)
18a6f04c
C
176 }
177 }
178
179 private sortHooksByPriority () {
180 for (const hookName of Object.keys(this.hooks)) {
181 this.hooks[hookName].sort((a, b) => {
182 return b.priority - a.priority
183 })
184 }
185 }
f0c5e8b6
C
186
187 private buildPeerTubeHelpers (pluginInfo: PluginInfo) {
188 const { plugin } = pluginInfo
189
190 return {
191 getBaseStaticRoute: () => {
192 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
193 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
194 }
195 }
196 }
197
198 private getPluginPathPrefix (isTheme: boolean) {
199 return isTheme ? '/themes' : '/plugins'
200 }
18a6f04c 201}