diff options
Diffstat (limited to 'client/src/app')
-rw-r--r-- | client/src/app/app.component.ts | 12 | ||||
-rw-r--r-- | client/src/app/core/core.module.ts | 3 | ||||
-rw-r--r-- | client/src/app/core/plugins/plugin.service.ts | 137 | ||||
-rw-r--r-- | client/src/app/core/server/server.service.ts | 1 | ||||
-rw-r--r-- | client/src/app/videos/+video-watch/video-watch.component.ts | 8 |
5 files changed, 160 insertions, 1 deletions
diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index 915466af7..548173f61 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts | |||
@@ -9,6 +9,7 @@ import { Hotkey, HotkeysService } from 'angular2-hotkeys' | |||
9 | import { I18n } from '@ngx-translate/i18n-polyfill' | 9 | import { I18n } from '@ngx-translate/i18n-polyfill' |
10 | import { fromEvent } from 'rxjs' | 10 | import { fromEvent } from 'rxjs' |
11 | import { ViewportScroller } from '@angular/common' | 11 | import { ViewportScroller } from '@angular/common' |
12 | import { PluginService } from '@app/core/plugins/plugin.service' | ||
12 | 13 | ||
13 | @Component({ | 14 | @Component({ |
14 | selector: 'my-app', | 15 | selector: 'my-app', |
@@ -27,6 +28,7 @@ export class AppComponent implements OnInit { | |||
27 | private router: Router, | 28 | private router: Router, |
28 | private authService: AuthService, | 29 | private authService: AuthService, |
29 | private serverService: ServerService, | 30 | private serverService: ServerService, |
31 | private pluginService: PluginService, | ||
30 | private domSanitizer: DomSanitizer, | 32 | private domSanitizer: DomSanitizer, |
31 | private redirectService: RedirectService, | 33 | private redirectService: RedirectService, |
32 | private screenService: ScreenService, | 34 | private screenService: ScreenService, |
@@ -69,6 +71,8 @@ export class AppComponent implements OnInit { | |||
69 | this.serverService.loadVideoPrivacies() | 71 | this.serverService.loadVideoPrivacies() |
70 | this.serverService.loadVideoPlaylistPrivacies() | 72 | this.serverService.loadVideoPlaylistPrivacies() |
71 | 73 | ||
74 | this.loadPlugins() | ||
75 | |||
72 | // Do not display menu on small screens | 76 | // Do not display menu on small screens |
73 | if (this.screenService.isInSmallView()) { | 77 | if (this.screenService.isInSmallView()) { |
74 | this.isMenuDisplayed = false | 78 | this.isMenuDisplayed = false |
@@ -196,6 +200,14 @@ export class AppComponent implements OnInit { | |||
196 | }) | 200 | }) |
197 | } | 201 | } |
198 | 202 | ||
203 | private async loadPlugins () { | ||
204 | this.pluginService.initializePlugins() | ||
205 | |||
206 | await this.pluginService.loadPluginsByScope('common') | ||
207 | |||
208 | this.pluginService.runHook('action:application.loaded') | ||
209 | } | ||
210 | |||
199 | private initHotkeys () { | 211 | private initHotkeys () { |
200 | this.hotkeysService.add([ | 212 | this.hotkeysService.add([ |
201 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { | 213 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts index 06fa8fcf1..436c0dfb8 100644 --- a/client/src/app/core/core.module.ts +++ b/client/src/app/core/core.module.ts | |||
@@ -21,6 +21,7 @@ import { MessageService } from 'primeng/api' | |||
21 | import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service' | 21 | import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service' |
22 | import { ServerConfigResolver } from './routing/server-config-resolver.service' | 22 | import { ServerConfigResolver } from './routing/server-config-resolver.service' |
23 | import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' | 23 | import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' |
24 | import { PluginService } from '@app/core/plugins/plugin.service' | ||
24 | 25 | ||
25 | @NgModule({ | 26 | @NgModule({ |
26 | imports: [ | 27 | imports: [ |
@@ -61,6 +62,8 @@ import { UnloggedGuard } from '@app/core/routing/unlogged-guard.service' | |||
61 | UserRightGuard, | 62 | UserRightGuard, |
62 | UnloggedGuard, | 63 | UnloggedGuard, |
63 | 64 | ||
65 | PluginService, | ||
66 | |||
64 | RedirectService, | 67 | RedirectService, |
65 | Notifier, | 68 | Notifier, |
66 | MessageService, | 69 | MessageService, |
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts new file mode 100644 index 000000000..6c567d3ca --- /dev/null +++ b/client/src/app/core/plugins/plugin.service.ts | |||
@@ -0,0 +1,137 @@ | |||
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.model' | ||
9 | import { ReplaySubject } from 'rxjs' | ||
10 | import { first } from 'rxjs/operators' | ||
11 | |||
12 | interface HookStructValue extends RegisterHookOptions { | ||
13 | plugin: ServerConfigPlugin | ||
14 | clientScript: ClientScript | ||
15 | } | ||
16 | |||
17 | @Injectable() | ||
18 | export 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 } = {} | ||
24 | |||
25 | private hooks: { [ name: string ]: HookStructValue[] } = {} | ||
26 | |||
27 | constructor ( | ||
28 | private router: Router, | ||
29 | private server: ServerService | ||
30 | ) { | ||
31 | } | ||
32 | |||
33 | initializePlugins () { | ||
34 | this.server.configLoaded | ||
35 | .subscribe(() => { | ||
36 | this.plugins = this.server.getConfig().plugins | ||
37 | |||
38 | this.buildScopeStruct() | ||
39 | |||
40 | this.pluginsLoaded.next(true) | ||
41 | }) | ||
42 | } | ||
43 | |||
44 | ensurePluginsAreLoaded () { | ||
45 | return this.pluginsLoaded.asObservable() | ||
46 | .pipe(first()) | ||
47 | .toPromise() | ||
48 | } | ||
49 | |||
50 | async loadPluginsByScope (scope: PluginScope) { | ||
51 | try { | ||
52 | await this.ensurePluginsAreLoaded() | ||
53 | |||
54 | const toLoad = this.scopes[ scope ] | ||
55 | if (!Array.isArray(toLoad)) return | ||
56 | |||
57 | const promises: Promise<any>[] = [] | ||
58 | for (const { plugin, clientScript } of toLoad) { | ||
59 | if (this.loadedScripts[ clientScript.script ]) continue | ||
60 | |||
61 | promises.push(this.loadPlugin(plugin, clientScript)) | ||
62 | |||
63 | this.loadedScripts[ clientScript.script ] = true | ||
64 | } | ||
65 | |||
66 | return Promise.all(promises) | ||
67 | } catch (err) { | ||
68 | console.error('Cannot load plugins by scope %s.', scope, err) | ||
69 | } | ||
70 | } | ||
71 | |||
72 | async runHook (hookName: string, param?: any) { | ||
73 | let result = param | ||
74 | |||
75 | const wait = hookName.startsWith('static:') | ||
76 | |||
77 | for (const hook of this.hooks[hookName]) { | ||
78 | try { | ||
79 | if (wait) result = await hook.handler(param) | ||
80 | else result = hook.handler() | ||
81 | } catch (err) { | ||
82 | console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return result | ||
87 | } | ||
88 | |||
89 | private loadPlugin (plugin: ServerConfigPlugin, clientScript: ClientScript) { | ||
90 | const registerHook = (options: RegisterHookOptions) => { | ||
91 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | ||
92 | |||
93 | this.hooks[options.target].push({ | ||
94 | plugin, | ||
95 | clientScript, | ||
96 | target: options.target, | ||
97 | handler: options.handler, | ||
98 | priority: options.priority || 0 | ||
99 | }) | ||
100 | } | ||
101 | |||
102 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | ||
103 | |||
104 | const url = environment.apiUrl + `/plugins/${plugin.name}/${plugin.version}/client-scripts/${clientScript.script}` | ||
105 | |||
106 | return import(/* webpackIgnore: true */ url) | ||
107 | .then(script => script.register({ registerHook })) | ||
108 | .then(() => this.sortHooksByPriority()) | ||
109 | } | ||
110 | |||
111 | private buildScopeStruct () { | ||
112 | for (const plugin of this.plugins) { | ||
113 | for (const key of Object.keys(plugin.clientScripts)) { | ||
114 | const clientScript = plugin.clientScripts[key] | ||
115 | |||
116 | for (const scope of clientScript.scopes) { | ||
117 | if (!this.scopes[scope]) this.scopes[scope] = [] | ||
118 | |||
119 | this.scopes[scope].push({ | ||
120 | plugin, | ||
121 | clientScript | ||
122 | }) | ||
123 | |||
124 | this.loadedScripts[clientScript.script] = false | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | |||
130 | private sortHooksByPriority () { | ||
131 | for (const hookName of Object.keys(this.hooks)) { | ||
132 | this.hooks[hookName].sort((a, b) => { | ||
133 | return b.priority - a.priority | ||
134 | }) | ||
135 | } | ||
136 | } | ||
137 | } | ||
diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts index 689f25a40..80c52164d 100644 --- a/client/src/app/core/server/server.service.ts +++ b/client/src/app/core/server/server.service.ts | |||
@@ -42,6 +42,7 @@ export class ServerService { | |||
42 | css: '' | 42 | css: '' |
43 | } | 43 | } |
44 | }, | 44 | }, |
45 | plugins: [], | ||
45 | email: { | 46 | email: { |
46 | enabled: false | 47 | enabled: false |
47 | }, | 48 | }, |
diff --git a/client/src/app/videos/+video-watch/video-watch.component.ts b/client/src/app/videos/+video-watch/video-watch.component.ts index 3f1a98f89..6d8bb4b3f 100644 --- a/client/src/app/videos/+video-watch/video-watch.component.ts +++ b/client/src/app/videos/+video-watch/video-watch.component.ts | |||
@@ -32,6 +32,7 @@ import { Video } from '@app/shared/video/video.model' | |||
32 | import { isWebRTCDisabled, timeToInt } from '../../../assets/player/utils' | 32 | import { isWebRTCDisabled, timeToInt } from '../../../assets/player/utils' |
33 | import { VideoWatchPlaylistComponent } from '@app/videos/+video-watch/video-watch-playlist.component' | 33 | import { VideoWatchPlaylistComponent } from '@app/videos/+video-watch/video-watch-playlist.component' |
34 | import { getStoredTheater } from '../../../assets/player/peertube-player-local-storage' | 34 | import { getStoredTheater } from '../../../assets/player/peertube-player-local-storage' |
35 | import { PluginService } from '@app/core/plugins/plugin.service' | ||
35 | 36 | ||
36 | @Component({ | 37 | @Component({ |
37 | selector: 'my-video-watch', | 38 | selector: 'my-video-watch', |
@@ -85,6 +86,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
85 | private serverService: ServerService, | 86 | private serverService: ServerService, |
86 | private restExtractor: RestExtractor, | 87 | private restExtractor: RestExtractor, |
87 | private notifier: Notifier, | 88 | private notifier: Notifier, |
89 | private pluginService: PluginService, | ||
88 | private markdownService: MarkdownService, | 90 | private markdownService: MarkdownService, |
89 | private zone: NgZone, | 91 | private zone: NgZone, |
90 | private redirectService: RedirectService, | 92 | private redirectService: RedirectService, |
@@ -98,7 +100,9 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
98 | return this.authService.getUser() | 100 | return this.authService.getUser() |
99 | } | 101 | } |
100 | 102 | ||
101 | ngOnInit () { | 103 | async ngOnInit () { |
104 | await this.pluginService.loadPluginsByScope('video-watch') | ||
105 | |||
102 | this.configSub = this.serverService.configLoaded | 106 | this.configSub = this.serverService.configLoaded |
103 | .subscribe(() => { | 107 | .subscribe(() => { |
104 | if ( | 108 | if ( |
@@ -126,6 +130,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy { | |||
126 | this.initHotkeys() | 130 | this.initHotkeys() |
127 | 131 | ||
128 | this.theaterEnabled = getStoredTheater() | 132 | this.theaterEnabled = getStoredTheater() |
133 | |||
134 | this.pluginService.runHook('action:video-watch.loaded') | ||
129 | } | 135 | } |
130 | 136 | ||
131 | ngOnDestroy () { | 137 | ngOnDestroy () { |