]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/plugins/plugin.service.ts
WIP plugins: update plugin
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
CommitLineData
18a6f04c
C
1import { Injectable } from '@angular/core'
2import { Router } from '@angular/router'
3import { ServerConfigPlugin } from '@shared/models'
4import { ServerService } from '@app/core/server/server.service'
5import { ClientScript } from '@shared/models/plugins/plugin-package-json.model'
6import { PluginScope } from '@shared/models/plugins/plugin-scope.type'
7import { environment } from '../../../environments/environment'
d00dc28d 8import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model'
18a6f04c 9import { ReplaySubject } from 'rxjs'
ffb321be 10import { first, shareReplay } from 'rxjs/operators'
18a6f04c
C
11
12interface HookStructValue extends RegisterHookOptions {
13 plugin: ServerConfigPlugin
14 clientScript: ClientScript
15}
16
17@Injectable()
18export class PluginService {
19 pluginsLoaded = new ReplaySubject<boolean>(1)
20
21 private plugins: ServerConfigPlugin[] = []
22 private scopes: { [ scopeName: string ]: { plugin: ServerConfigPlugin, clientScript: ClientScript }[] } = {}
23 private loadedScripts: { [ script: string ]: boolean } = {}
ffb321be 24 private loadedScopes: PluginScope[] = []
18a6f04c
C
25
26 private hooks: { [ name: string ]: HookStructValue[] } = {}
27
28 constructor (
29 private router: Router,
30 private server: ServerService
31 ) {
32 }
33
34 initializePlugins () {
35 this.server.configLoaded
36 .subscribe(() => {
7cd4d2ba 37 this.plugins = this.server.getConfig().plugin.registered
18a6f04c
C
38
39 this.buildScopeStruct()
40
41 this.pluginsLoaded.next(true)
42 })
43 }
44
45 ensurePluginsAreLoaded () {
46 return this.pluginsLoaded.asObservable()
ffb321be 47 .pipe(first(), shareReplay())
18a6f04c
C
48 .toPromise()
49 }
50
b5f919ac
C
51 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
52 const pathPrefix = isTheme ? '/themes' : '/plugins'
53
ffb321be
C
54 for (const key of Object.keys(plugin.clientScripts)) {
55 const clientScript = plugin.clientScripts[key]
56
57 for (const scope of clientScript.scopes) {
58 if (!this.scopes[scope]) this.scopes[scope] = []
59
60 this.scopes[scope].push({
61 plugin,
62 clientScript: {
b5f919ac 63 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
ffb321be
C
64 scopes: clientScript.scopes
65 }
66 })
67
68 this.loadedScripts[clientScript.script] = false
69 }
70 }
71 }
72
73 removePlugin (plugin: ServerConfigPlugin) {
74 for (const key of Object.keys(this.scopes)) {
75 this.scopes[key] = this.scopes[key].filter(o => o.plugin.name !== plugin.name)
76 }
77 }
78
79 async reloadLoadedScopes () {
80 for (const scope of this.loadedScopes) {
81 await this.loadPluginsByScope(scope)
82 }
83 }
84
18a6f04c
C
85 async loadPluginsByScope (scope: PluginScope) {
86 try {
87 await this.ensurePluginsAreLoaded()
88
ffb321be
C
89 this.loadedScopes.push(scope)
90
18a6f04c
C
91 const toLoad = this.scopes[ scope ]
92 if (!Array.isArray(toLoad)) return
93
94 const promises: Promise<any>[] = []
95 for (const { plugin, clientScript } of toLoad) {
96 if (this.loadedScripts[ clientScript.script ]) continue
97
98 promises.push(this.loadPlugin(plugin, clientScript))
99
100 this.loadedScripts[ clientScript.script ] = true
101 }
102
ffb321be 103 await Promise.all(promises)
18a6f04c
C
104 } catch (err) {
105 console.error('Cannot load plugins by scope %s.', scope, err)
106 }
107 }
108
109 async runHook (hookName: string, param?: any) {
110 let result = param
111
112 const wait = hookName.startsWith('static:')
113
114 for (const hook of this.hooks[hookName]) {
115 try {
116 if (wait) result = await hook.handler(param)
117 else result = hook.handler()
118 } catch (err) {
119 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
120 }
121 }
122
123 return result
124 }
125
126 private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) {
127 const registerHook = (options: RegisterHookOptions) => {
128 if (!this.hooks[options.target]) this.hooks[options.target] = []
129
130 this.hooks[options.target].push({
131 plugin,
132 clientScript,
133 target: options.target,
134 handler: options.handler,
135 priority: options.priority || 0
136 })
137 }
138
139 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
140
ffb321be 141 return import(/* webpackIgnore: true */ clientScript.script)
18a6f04c
C
142 .then(script => script.register({ registerHook }))
143 .then(() => this.sortHooksByPriority())
144 }
145
146 private buildScopeStruct () {
147 for (const plugin of this.plugins) {
ffb321be 148 this.addPlugin(plugin)
18a6f04c
C
149 }
150 }
151
152 private sortHooksByPriority () {
153 for (const hookName of Object.keys(this.hooks)) {
154 this.hooks[hookName].sort((a, b) => {
155 return b.priority - a.priority
156 })
157 }
158 }
159}