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 { environment } from '../../../environments/environment'
7 import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model'
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'
14 interface HookStructValue extends RegisterHookOptions {
15 plugin: ServerConfigPlugin
16 clientScript: ClientScript
20 plugin: ServerConfigPlugin
21 clientScript: ClientScript
26 export class PluginService implements ClientHook {
27 pluginsBuilt = new ReplaySubject<boolean>(1)
29 pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
30 common: new ReplaySubject<boolean>(1),
31 search: new ReplaySubject<boolean>(1),
32 'video-watch': new ReplaySubject<boolean>(1)
35 private plugins: ServerConfigPlugin[] = []
36 private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
37 private loadedScripts: { [ script: string ]: boolean } = {}
38 private loadedScopes: PluginClientScope[] = []
39 private loadingScopes: { [id in PluginClientScope]?: boolean } = {}
41 private hooks: { [ name: string ]: HookStructValue[] } = {}
44 private router: Router,
45 private server: ServerService
49 initializePlugins () {
50 this.server.configLoaded
52 this.plugins = this.server.getConfig().plugin.registered
54 this.buildScopeStruct()
56 this.pluginsBuilt.next(true)
60 ensurePluginsAreBuilt () {
61 return this.pluginsBuilt.asObservable()
62 .pipe(first(), shareReplay())
66 ensurePluginsAreLoaded (scope: PluginClientScope) {
67 this.loadPluginsByScope(scope)
69 return this.pluginsLoaded[scope].asObservable()
70 .pipe(first(), shareReplay())
74 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
75 const pathPrefix = this.getPluginPathPrefix(isTheme)
77 for (const key of Object.keys(plugin.clientScripts)) {
78 const clientScript = plugin.clientScripts[key]
80 for (const scope of clientScript.scopes) {
81 if (!this.scopes[scope]) this.scopes[scope] = []
83 this.scopes[scope].push({
86 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
87 scopes: clientScript.scopes
92 this.loadedScripts[clientScript.script] = false
97 removePlugin (plugin: ServerConfigPlugin) {
98 for (const key of Object.keys(this.scopes)) {
99 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
103 async reloadLoadedScopes () {
104 for (const scope of this.loadedScopes) {
105 await this.loadPluginsByScope(scope, true)
109 async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
110 if (this.loadingScopes[scope]) return
111 if (!isReload && this.loadedScopes.includes(scope)) return
113 this.loadingScopes[scope] = true
116 await this.ensurePluginsAreBuilt()
118 if (!isReload) this.loadedScopes.push(scope)
120 const toLoad = this.scopes[ scope ]
121 if (!Array.isArray(toLoad)) {
122 this.loadingScopes[scope] = false
123 this.pluginsLoaded[scope].next(true)
128 const promises: Promise<any>[] = []
129 for (const pluginInfo of toLoad) {
130 const clientScript = pluginInfo.clientScript
132 if (this.loadedScripts[ clientScript.script ]) continue
134 promises.push(this.loadPlugin(pluginInfo))
136 this.loadedScripts[ clientScript.script ] = true
139 await Promise.all(promises)
141 this.pluginsLoaded[scope].next(true)
142 this.loadingScopes[scope] = false
144 console.error('Cannot load plugins by scope %s.', scope, err)
148 async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
149 if (!this.hooks[hookName]) return Promise.resolve(result)
151 const hookType = getHookType(hookName)
153 for (const hook of this.hooks[hookName]) {
154 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
156 result = await internalRunHook(hook.handler, hookType, result, params, err => {
157 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
164 private loadPlugin (pluginInfo: PluginInfo) {
165 const { plugin, clientScript } = pluginInfo
167 const registerHook = (options: RegisterHookOptions) => {
168 if (clientHookObject[options.target] !== true) {
169 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
173 if (!this.hooks[options.target]) this.hooks[options.target] = []
175 this.hooks[options.target].push({
178 target: options.target,
179 handler: options.handler,
180 priority: options.priority || 0
184 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
186 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
188 return import(/* webpackIgnore: true */ clientScript.script)
189 .then(script => script.register({ registerHook, peertubeHelpers }))
190 .then(() => this.sortHooksByPriority())
193 private buildScopeStruct () {
194 for (const plugin of this.plugins) {
195 this.addPlugin(plugin)
199 private sortHooksByPriority () {
200 for (const hookName of Object.keys(this.hooks)) {
201 this.hooks[hookName].sort((a, b) => {
202 return b.priority - a.priority
207 private buildPeerTubeHelpers (pluginInfo: PluginInfo) {
208 const { plugin } = pluginInfo
211 getBaseStaticRoute: () => {
212 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
213 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
218 private getPluginPathPrefix (isTheme: boolean) {
219 return isTheme ? '/themes' : '/plugins'