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 { ClientScript as ClientScriptModule } from '../../../types/client-script.model'
7 import { environment } from '../../../environments/environment'
8 import { ReplaySubject } from 'rxjs'
9 import { first, shareReplay } from 'rxjs/operators'
10 import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
11 import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model'
12 import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
13 import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model'
15 interface HookStructValue extends RegisterClientHookOptions {
16 plugin: ServerConfigPlugin
17 clientScript: ClientScript
21 plugin: ServerConfigPlugin
22 clientScript: ClientScript
27 export class PluginService implements ClientHook {
28 pluginsBuilt = new ReplaySubject<boolean>(1)
30 pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
31 common: new ReplaySubject<boolean>(1),
32 search: new ReplaySubject<boolean>(1),
33 'video-watch': new ReplaySubject<boolean>(1)
36 private plugins: ServerConfigPlugin[] = []
37 private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
38 private loadedScripts: { [ script: string ]: boolean } = {}
39 private loadedScopes: PluginClientScope[] = []
40 private loadingScopes: { [id in PluginClientScope]?: boolean } = {}
42 private hooks: { [ name: string ]: HookStructValue[] } = {}
45 private router: Router,
46 private server: ServerService
50 initializePlugins () {
51 this.server.configLoaded
53 this.plugins = this.server.getConfig().plugin.registered
55 this.buildScopeStruct()
57 this.pluginsBuilt.next(true)
61 ensurePluginsAreBuilt () {
62 return this.pluginsBuilt.asObservable()
63 .pipe(first(), shareReplay())
67 ensurePluginsAreLoaded (scope: PluginClientScope) {
68 this.loadPluginsByScope(scope)
70 return this.pluginsLoaded[scope].asObservable()
71 .pipe(first(), shareReplay())
75 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
76 const pathPrefix = this.getPluginPathPrefix(isTheme)
78 for (const key of Object.keys(plugin.clientScripts)) {
79 const clientScript = plugin.clientScripts[key]
81 for (const scope of clientScript.scopes) {
82 if (!this.scopes[scope]) this.scopes[scope] = []
84 this.scopes[scope].push({
87 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
88 scopes: clientScript.scopes
93 this.loadedScripts[clientScript.script] = false
98 removePlugin (plugin: ServerConfigPlugin) {
99 for (const key of Object.keys(this.scopes)) {
100 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
104 async reloadLoadedScopes () {
105 for (const scope of this.loadedScopes) {
106 await this.loadPluginsByScope(scope, true)
110 async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
111 if (this.loadingScopes[scope]) return
112 if (!isReload && this.loadedScopes.includes(scope)) return
114 this.loadingScopes[scope] = true
117 await this.ensurePluginsAreBuilt()
119 if (!isReload) this.loadedScopes.push(scope)
121 const toLoad = this.scopes[ scope ]
122 if (!Array.isArray(toLoad)) {
123 this.loadingScopes[scope] = false
124 this.pluginsLoaded[scope].next(true)
129 const promises: Promise<any>[] = []
130 for (const pluginInfo of toLoad) {
131 const clientScript = pluginInfo.clientScript
133 if (this.loadedScripts[ clientScript.script ]) continue
135 promises.push(this.loadPlugin(pluginInfo))
137 this.loadedScripts[ clientScript.script ] = true
140 await Promise.all(promises)
142 this.pluginsLoaded[scope].next(true)
143 this.loadingScopes[scope] = false
145 console.error('Cannot load plugins by scope %s.', scope, err)
149 async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
150 if (!this.hooks[hookName]) return Promise.resolve(result)
152 const hookType = getHookType(hookName)
154 for (const hook of this.hooks[hookName]) {
155 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
157 result = await internalRunHook(hook.handler, hookType, result, params, err => {
158 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
165 private loadPlugin (pluginInfo: PluginInfo) {
166 const { plugin, clientScript } = pluginInfo
168 const registerHook = (options: RegisterClientHookOptions) => {
169 if (clientHookObject[options.target] !== true) {
170 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
174 if (!this.hooks[options.target]) this.hooks[options.target] = []
176 this.hooks[options.target].push({
179 target: options.target,
180 handler: options.handler,
181 priority: options.priority || 0
185 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
187 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
189 return import(/* webpackIgnore: true */ clientScript.script)
190 .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
191 .then(() => this.sortHooksByPriority())
194 private buildScopeStruct () {
195 for (const plugin of this.plugins) {
196 this.addPlugin(plugin)
200 private sortHooksByPriority () {
201 for (const hookName of Object.keys(this.hooks)) {
202 this.hooks[hookName].sort((a, b) => {
203 return b.priority - a.priority
208 private buildPeerTubeHelpers (pluginInfo: PluginInfo) {
209 const { plugin } = pluginInfo
212 getBaseStaticRoute: () => {
213 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
214 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
219 private getPluginPathPrefix (isTheme: boolean) {
220 return isTheme ? '/themes' : '/plugins'