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'
12 interface HookStructValue extends RegisterHookOptions {
13 plugin: ServerConfigPlugin
14 clientScript: ClientScript
18 plugin: ServerConfigPlugin
19 clientScript: ClientScript
24 export class PluginService {
25 pluginsLoaded = new ReplaySubject<boolean>(1)
27 private plugins: ServerConfigPlugin[] = []
28 private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
29 private loadedPlugins: { [ name: string ]: boolean } = {}
30 private loadedScripts: { [ script: string ]: boolean } = {}
31 private loadedScopes: PluginScope[] = []
33 private hooks: { [ name: string ]: HookStructValue[] } = {}
36 private router: Router,
37 private server: ServerService
41 initializePlugins () {
42 this.server.configLoaded
44 this.plugins = this.server.getConfig().plugin.registered
46 this.buildScopeStruct()
48 this.pluginsLoaded.next(true)
52 ensurePluginsAreLoaded () {
53 return this.pluginsLoaded.asObservable()
54 .pipe(first(), shareReplay())
58 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
59 const pathPrefix = this.getPluginPathPrefix(isTheme)
61 for (const key of Object.keys(plugin.clientScripts)) {
62 const clientScript = plugin.clientScripts[key]
64 for (const scope of clientScript.scopes) {
65 if (!this.scopes[scope]) this.scopes[scope] = []
67 this.scopes[scope].push({
70 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
71 scopes: clientScript.scopes
76 this.loadedScripts[clientScript.script] = false
81 removePlugin (plugin: ServerConfigPlugin) {
82 for (const key of Object.keys(this.scopes)) {
83 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
87 async reloadLoadedScopes () {
88 for (const scope of this.loadedScopes) {
89 await this.loadPluginsByScope(scope, true)
93 async loadPluginsByScope (scope: PluginScope, isReload = false) {
95 await this.ensurePluginsAreLoaded()
97 if (!isReload) this.loadedScopes.push(scope)
99 const toLoad = this.scopes[ scope ]
100 if (!Array.isArray(toLoad)) return
102 const promises: Promise<any>[] = []
103 for (const pluginInfo of toLoad) {
104 const clientScript = pluginInfo.clientScript
106 if (this.loadedScripts[ clientScript.script ]) continue
108 promises.push(this.loadPlugin(pluginInfo))
110 this.loadedScripts[ clientScript.script ] = true
113 await Promise.all(promises)
115 console.error('Cannot load plugins by scope %s.', scope, err)
119 async runHook (hookName: string, param?: any) {
122 if (!this.hooks[hookName]) return result
124 const wait = hookName.startsWith('static:')
126 for (const hook of this.hooks[hookName]) {
128 const p = hook.handler(param)
132 } else if (p.catch) {
133 p.catch((err: Error) => {
134 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
138 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
145 private loadPlugin (pluginInfo: PluginInfo) {
146 const { plugin, clientScript } = pluginInfo
148 const registerHook = (options: RegisterHookOptions) => {
149 if (!this.hooks[options.target]) this.hooks[options.target] = []
151 this.hooks[options.target].push({
154 target: options.target,
155 handler: options.handler,
156 priority: options.priority || 0
160 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
162 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
164 return import(/* webpackIgnore: true */ clientScript.script)
165 .then(script => script.register({ registerHook, peertubeHelpers }))
166 .then(() => this.sortHooksByPriority())
169 private buildScopeStruct () {
170 for (const plugin of this.plugins) {
171 this.addPlugin(plugin)
175 private sortHooksByPriority () {
176 for (const hookName of Object.keys(this.hooks)) {
177 this.hooks[hookName].sort((a, b) => {
178 return b.priority - a.priority
183 private buildPeerTubeHelpers (pluginInfo: PluginInfo) {
184 const { plugin } = pluginInfo
187 getBaseStaticRoute: () => {
188 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
189 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
194 private getPluginPathPrefix (isTheme: boolean) {
195 return isTheme ? '/themes' : '/plugins'