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[] = []
40 private hooks: { [ name: string ]: HookStructValue[] } = {}
43 private router: Router,
44 private server: ServerService
48 initializePlugins () {
49 this.server.configLoaded
51 this.plugins = this.server.getConfig().plugin.registered
53 this.buildScopeStruct()
55 this.pluginsBuilt.next(true)
59 ensurePluginsAreBuilt () {
60 return this.pluginsBuilt.asObservable()
61 .pipe(first(), shareReplay())
65 ensurePluginsAreLoaded (scope: PluginClientScope) {
66 return this.pluginsLoaded[scope].asObservable()
67 .pipe(first(), shareReplay())
71 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
72 const pathPrefix = this.getPluginPathPrefix(isTheme)
74 for (const key of Object.keys(plugin.clientScripts)) {
75 const clientScript = plugin.clientScripts[key]
77 for (const scope of clientScript.scopes) {
78 if (!this.scopes[scope]) this.scopes[scope] = []
80 this.scopes[scope].push({
83 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
84 scopes: clientScript.scopes
89 this.loadedScripts[clientScript.script] = false
94 removePlugin (plugin: ServerConfigPlugin) {
95 for (const key of Object.keys(this.scopes)) {
96 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
100 async reloadLoadedScopes () {
101 for (const scope of this.loadedScopes) {
102 await this.loadPluginsByScope(scope, true)
106 async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
108 await this.ensurePluginsAreBuilt()
110 if (!isReload) this.loadedScopes.push(scope)
112 const toLoad = this.scopes[ scope ]
113 if (!Array.isArray(toLoad)) {
114 this.pluginsLoaded[scope].next(true)
119 const promises: Promise<any>[] = []
120 for (const pluginInfo of toLoad) {
121 const clientScript = pluginInfo.clientScript
123 if (this.loadedScripts[ clientScript.script ]) continue
125 promises.push(this.loadPlugin(pluginInfo))
127 this.loadedScripts[ clientScript.script ] = true
130 await Promise.all(promises)
132 this.pluginsLoaded[scope].next(true)
134 console.error('Cannot load plugins by scope %s.', scope, err)
138 async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
139 if (!this.hooks[hookName]) return Promise.resolve(result)
141 const hookType = getHookType(hookName)
143 for (const hook of this.hooks[hookName]) {
144 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
146 result = await internalRunHook(hook.handler, hookType, result, params, err => {
147 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
154 private loadPlugin (pluginInfo: PluginInfo) {
155 const { plugin, clientScript } = pluginInfo
157 const registerHook = (options: RegisterHookOptions) => {
158 if (clientHookObject[options.target] !== true) {
159 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
163 if (!this.hooks[options.target]) this.hooks[options.target] = []
165 this.hooks[options.target].push({
168 target: options.target,
169 handler: options.handler,
170 priority: options.priority || 0
174 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
176 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
178 return import(/* webpackIgnore: true */ clientScript.script)
179 .then(script => script.register({ registerHook, peertubeHelpers }))
180 .then(() => this.sortHooksByPriority())
183 private buildScopeStruct () {
184 for (const plugin of this.plugins) {
185 this.addPlugin(plugin)
189 private sortHooksByPriority () {
190 for (const hookName of Object.keys(this.hooks)) {
191 this.hooks[hookName].sort((a, b) => {
192 return b.priority - a.priority
197 private buildPeerTubeHelpers (pluginInfo: PluginInfo) {
198 const { plugin } = pluginInfo
201 getBaseStaticRoute: () => {
202 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
203 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
208 private getPluginPathPrefix (isTheme: boolean) {
209 return isTheme ? '/themes' : '/plugins'