1 import { Observable, of, ReplaySubject } from 'rxjs'
2 import { catchError, first, map, shareReplay } from 'rxjs/operators'
3 import { HttpClient } from '@angular/common/http'
4 import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core'
5 import { AuthService } from '@app/core/auth'
6 import { Notifier } from '@app/core/notification'
7 import { MarkdownService } from '@app/core/renderer'
8 import { RestExtractor } from '@app/core/rest'
9 import { ServerService } from '@app/core/server/server.service'
10 import { getDevLocale, importModule, isOnDevLocale } from '@app/helpers'
11 import { CustomModalComponent } from '@app/modal/custom-modal.component'
12 import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
25 RegisterClientHookOptions,
27 } from '@shared/models'
28 import { environment } from '../../../environments/environment'
29 import { ClientScript as ClientScriptModule } from '../../../types/client-script.model'
30 import { RegisterClientHelpers } from '../../../types/register-client-option.model'
32 interface HookStructValue extends RegisterClientHookOptions {
33 plugin: ServerConfigPlugin
34 clientScript: ClientScript
38 plugin: ServerConfigPlugin
39 clientScript: ClientScript
40 pluginType: PluginType
45 export class PluginService implements ClientHook {
46 private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins'
47 private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins'
49 pluginsBuilt = new ReplaySubject<boolean>(1)
51 pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
52 common: new ReplaySubject<boolean>(1),
53 search: new ReplaySubject<boolean>(1),
54 'video-watch': new ReplaySubject<boolean>(1),
55 signup: new ReplaySubject<boolean>(1),
56 login: new ReplaySubject<boolean>(1)
59 translationsObservable: Observable<PluginTranslation>
61 customModal: CustomModalComponent
63 private plugins: ServerConfigPlugin[] = []
64 private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
65 private loadedScripts: { [ script: string ]: boolean } = {}
66 private loadedScopes: PluginClientScope[] = []
67 private loadingScopes: { [id in PluginClientScope]?: boolean } = {}
69 private hooks: { [ name: string ]: HookStructValue[] } = {}
72 private authService: AuthService,
73 private notifier: Notifier,
74 private markdownRenderer: MarkdownService,
75 private server: ServerService,
77 private authHttp: HttpClient,
78 private restExtractor: RestExtractor,
79 @Inject(LOCALE_ID) private localeId: string
81 this.loadTranslations()
84 initializePlugins () {
85 this.server.getConfig()
86 .subscribe(config => {
87 this.plugins = config.plugin.registered
89 this.buildScopeStruct()
91 this.pluginsBuilt.next(true)
95 initializeCustomModal (customModal: CustomModalComponent) {
96 this.customModal = customModal
99 ensurePluginsAreBuilt () {
100 return this.pluginsBuilt.asObservable()
101 .pipe(first(), shareReplay())
105 ensurePluginsAreLoaded (scope: PluginClientScope) {
106 this.loadPluginsByScope(scope)
108 return this.pluginsLoaded[scope].asObservable()
109 .pipe(first(), shareReplay())
113 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
114 const pathPrefix = this.getPluginPathPrefix(isTheme)
116 for (const key of Object.keys(plugin.clientScripts)) {
117 const clientScript = plugin.clientScripts[key]
119 for (const scope of clientScript.scopes) {
120 if (!this.scopes[scope]) this.scopes[scope] = []
122 this.scopes[scope].push({
125 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
126 scopes: clientScript.scopes
128 pluginType: isTheme ? PluginType.THEME : PluginType.PLUGIN,
132 this.loadedScripts[clientScript.script] = false
137 removePlugin (plugin: ServerConfigPlugin) {
138 for (const key of Object.keys(this.scopes)) {
139 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
143 async reloadLoadedScopes () {
144 for (const scope of this.loadedScopes) {
145 await this.loadPluginsByScope(scope, true)
149 async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
150 if (this.loadingScopes[scope]) return
151 if (!isReload && this.loadedScopes.includes(scope)) return
153 this.loadingScopes[scope] = true
156 await this.ensurePluginsAreBuilt()
158 if (!isReload) this.loadedScopes.push(scope)
160 const toLoad = this.scopes[ scope ]
161 if (!Array.isArray(toLoad)) {
162 this.loadingScopes[scope] = false
163 this.pluginsLoaded[scope].next(true)
168 const promises: Promise<any>[] = []
169 for (const pluginInfo of toLoad) {
170 const clientScript = pluginInfo.clientScript
172 if (this.loadedScripts[ clientScript.script ]) continue
174 promises.push(this.loadPlugin(pluginInfo))
176 this.loadedScripts[ clientScript.script ] = true
179 await Promise.all(promises)
181 this.pluginsLoaded[scope].next(true)
182 this.loadingScopes[scope] = false
184 console.error('Cannot load plugins by scope %s.', scope, err)
188 runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
189 return this.zone.runOutsideAngular(async () => {
190 if (!this.hooks[ hookName ]) return result
192 const hookType = getHookType(hookName)
194 for (const hook of this.hooks[ hookName ]) {
195 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
197 result = await internalRunHook(hook.handler, hookType, result, params, err => {
198 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
206 nameToNpmName (name: string, type: PluginType) {
207 const prefix = type === PluginType.PLUGIN
214 pluginTypeFromNpmName (npmName: string) {
215 return npmName.startsWith('peertube-plugin-')
220 private loadPlugin (pluginInfo: PluginInfo) {
221 const { plugin, clientScript } = pluginInfo
223 const registerHook = (options: RegisterClientHookOptions) => {
224 if (clientHookObject[options.target] !== true) {
225 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
229 if (!this.hooks[options.target]) this.hooks[options.target] = []
231 this.hooks[options.target].push({
234 target: options.target,
235 handler: options.handler,
236 priority: options.priority || 0
240 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
242 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
244 return this.zone.runOutsideAngular(() => {
245 return importModule(clientScript.script)
246 .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
247 .then(() => this.sortHooksByPriority())
248 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
252 private buildScopeStruct () {
253 for (const plugin of this.plugins) {
254 this.addPlugin(plugin)
258 private sortHooksByPriority () {
259 for (const hookName of Object.keys(this.hooks)) {
260 this.hooks[hookName].sort((a, b) => {
261 return b.priority - a.priority
266 private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
267 const { plugin } = pluginInfo
268 const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
271 getBaseStaticRoute: () => {
272 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
273 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
277 const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings'
279 return this.authHttp.get<PublicServerSetting>(path)
281 map(p => p.publicSettings),
282 catchError(res => this.restExtractor.handleError(res))
288 return this.authService.isLoggedIn()
292 info: (text: string, title?: string, timeout?: number) => this.notifier.info(text, title, timeout),
293 error: (text: string, title?: string, timeout?: number) => this.notifier.error(text, title, timeout),
294 success: (text: string, title?: string, timeout?: number) => this.notifier.success(text, title, timeout)
301 cancel?: { value: string, action?: () => void },
302 confirm?: { value: string, action?: () => void }
304 this.customModal.show(input)
308 textMarkdownToHTML: (textMarkdown: string) => {
309 return this.markdownRenderer.textMarkdownToHTML(textMarkdown)
312 enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
313 return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown)
317 translate: (value: string) => {
318 return this.translationsObservable
319 .pipe(map(allTranslations => allTranslations[npmName]))
320 .pipe(map(translations => peertubeTranslate(value, translations)))
326 private loadTranslations () {
327 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
329 // Default locale, nothing to translate
330 if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay())
332 this.translationsObservable = this.authHttp
333 .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json')
337 private getPluginPathPrefix (isTheme: boolean) {
338 return isTheme ? '/themes' : '/plugins'