]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/plugins/plugin.service.ts
Correctly notify on auto blacklist
[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'
18a6f04c 6import { environment } from '../../../environments/environment'
d00dc28d 7import { RegisterHookOptions } from '@shared/models/plugins/register-hook.model'
18a6f04c 8import { ReplaySubject } from 'rxjs'
ffb321be 9import { first, shareReplay } from 'rxjs/operators'
93cae479 10import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
7663e55a 11import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model'
93cae479 12import { PluginClientScope } from '@shared/models/plugins/plugin-client-scope.type'
18a6f04c
C
13
14interface HookStructValue extends RegisterHookOptions {
15 plugin: ServerConfigPlugin
16 clientScript: ClientScript
17}
18
f0c5e8b6
C
19type PluginInfo = {
20 plugin: ServerConfigPlugin
21 clientScript: ClientScript
22 isTheme: boolean
23}
24
18a6f04c 25@Injectable()
93cae479
C
26export class PluginService implements ClientHook {
27 pluginsBuilt = new ReplaySubject<boolean>(1)
28
29 pluginsLoaded: { [ scope in PluginClientScope ]: ReplaySubject<boolean> } = {
30 common: new ReplaySubject<boolean>(1),
e8f902c0 31 search: new ReplaySubject<boolean>(1),
93cae479
C
32 'video-watch': new ReplaySubject<boolean>(1)
33 }
18a6f04c
C
34
35 private plugins: ServerConfigPlugin[] = []
f0c5e8b6 36 private scopes: { [ scopeName: string ]: PluginInfo[] } = {}
18a6f04c 37 private loadedScripts: { [ script: string ]: boolean } = {}
93cae479 38 private loadedScopes: PluginClientScope[] = []
18a6f04c
C
39
40 private hooks: { [ name: string ]: HookStructValue[] } = {}
41
42 constructor (
43 private router: Router,
44 private server: ServerService
45 ) {
46 }
47
48 initializePlugins () {
49 this.server.configLoaded
50 .subscribe(() => {
7cd4d2ba 51 this.plugins = this.server.getConfig().plugin.registered
18a6f04c
C
52
53 this.buildScopeStruct()
54
93cae479 55 this.pluginsBuilt.next(true)
18a6f04c
C
56 })
57 }
58
93cae479
C
59 ensurePluginsAreBuilt () {
60 return this.pluginsBuilt.asObservable()
61 .pipe(first(), shareReplay())
62 .toPromise()
63 }
64
65 ensurePluginsAreLoaded (scope: PluginClientScope) {
66 return this.pluginsLoaded[scope].asObservable()
ffb321be 67 .pipe(first(), shareReplay())
18a6f04c
C
68 .toPromise()
69 }
70
b5f919ac 71 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
f0c5e8b6 72 const pathPrefix = this.getPluginPathPrefix(isTheme)
b5f919ac 73
ffb321be
C
74 for (const key of Object.keys(plugin.clientScripts)) {
75 const clientScript = plugin.clientScripts[key]
76
77 for (const scope of clientScript.scopes) {
78 if (!this.scopes[scope]) this.scopes[scope] = []
79
80 this.scopes[scope].push({
81 plugin,
82 clientScript: {
b5f919ac 83 script: environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}`,
ffb321be 84 scopes: clientScript.scopes
f0c5e8b6
C
85 },
86 isTheme
ffb321be
C
87 })
88
89 this.loadedScripts[clientScript.script] = false
90 }
91 }
92 }
93
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)
97 }
98 }
99
100 async reloadLoadedScopes () {
101 for (const scope of this.loadedScopes) {
f0c5e8b6 102 await this.loadPluginsByScope(scope, true)
ffb321be
C
103 }
104 }
105
93cae479 106 async loadPluginsByScope (scope: PluginClientScope, isReload = false) {
18a6f04c 107 try {
93cae479 108 await this.ensurePluginsAreBuilt()
18a6f04c 109
f0c5e8b6 110 if (!isReload) this.loadedScopes.push(scope)
ffb321be 111
18a6f04c 112 const toLoad = this.scopes[ scope ]
e8f902c0
C
113 if (!Array.isArray(toLoad)) {
114 this.pluginsLoaded[scope].next(true)
115
116 return
117 }
18a6f04c
C
118
119 const promises: Promise<any>[] = []
f0c5e8b6
C
120 for (const pluginInfo of toLoad) {
121 const clientScript = pluginInfo.clientScript
122
18a6f04c
C
123 if (this.loadedScripts[ clientScript.script ]) continue
124
f0c5e8b6 125 promises.push(this.loadPlugin(pluginInfo))
18a6f04c
C
126
127 this.loadedScripts[ clientScript.script ] = true
128 }
129
ffb321be 130 await Promise.all(promises)
93cae479
C
131
132 this.pluginsLoaded[scope].next(true)
18a6f04c
C
133 } catch (err) {
134 console.error('Cannot load plugins by scope %s.', scope, err)
135 }
136 }
137
93cae479
C
138 async runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
139 if (!this.hooks[hookName]) return Promise.resolve(result)
f0c5e8b6 140
93cae479 141 const hookType = getHookType(hookName)
18a6f04c
C
142
143 for (const hook of this.hooks[hookName]) {
93cae479
C
144 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
145
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)
148 })
18a6f04c
C
149 }
150
151 return result
152 }
153
f0c5e8b6
C
154 private loadPlugin (pluginInfo: PluginInfo) {
155 const { plugin, clientScript } = pluginInfo
156
18a6f04c 157 const registerHook = (options: RegisterHookOptions) => {
7663e55a
C
158 if (clientHookObject[options.target] !== true) {
159 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
160 return
161 }
162
18a6f04c
C
163 if (!this.hooks[options.target]) this.hooks[options.target] = []
164
165 this.hooks[options.target].push({
166 plugin,
167 clientScript,
168 target: options.target,
169 handler: options.handler,
170 priority: options.priority || 0
171 })
172 }
173
f0c5e8b6
C
174 const peertubeHelpers = this.buildPeerTubeHelpers(pluginInfo)
175
18a6f04c
C
176 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
177
ffb321be 178 return import(/* webpackIgnore: true */ clientScript.script)
f0c5e8b6 179 .then(script => script.register({ registerHook, peertubeHelpers }))
18a6f04c
C
180 .then(() => this.sortHooksByPriority())
181 }
182
183 private buildScopeStruct () {
184 for (const plugin of this.plugins) {
ffb321be 185 this.addPlugin(plugin)
18a6f04c
C
186 }
187 }
188
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
193 })
194 }
195 }
f0c5e8b6
C
196
197 private buildPeerTubeHelpers (pluginInfo: PluginInfo) {
198 const { plugin } = pluginInfo
199
200 return {
201 getBaseStaticRoute: () => {
202 const pathPrefix = this.getPluginPathPrefix(pluginInfo.isTheme)
203 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
204 }
205 }
206 }
207
208 private getPluginPathPrefix (isTheme: boolean) {
209 return isTheme ? '/themes' : '/plugins'
210 }
18a6f04c 211}