diff options
author | Chocobozzz <me@florianbigard.com> | 2019-12-18 15:31:54 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-12-18 15:40:59 +0100 |
commit | ba430d7516bc5b1324b60571ba7594460969b7fb (patch) | |
tree | df5c6952c82f49a94c0a884bbc97d4a0cbd9f867 /client/src/app/core | |
parent | 5dfb7c1dec8222b0bbccac5b56ad46da1438747e (diff) | |
download | PeerTube-ba430d7516bc5b1324b60571ba7594460969b7fb.tar.gz PeerTube-ba430d7516bc5b1324b60571ba7594460969b7fb.tar.zst PeerTube-ba430d7516bc5b1324b60571ba7594460969b7fb.zip |
Lazy load static objects
Diffstat (limited to 'client/src/app/core')
-rw-r--r-- | client/src/app/core/plugins/plugin.service.ts | 6 | ||||
-rw-r--r-- | client/src/app/core/routing/redirect.service.ts | 12 | ||||
-rw-r--r-- | client/src/app/core/routing/server-config-resolver.service.ts | 12 | ||||
-rw-r--r-- | client/src/app/core/server/server.service.ts | 205 | ||||
-rw-r--r-- | client/src/app/core/theme/theme.service.ts | 15 |
5 files changed, 131 insertions, 119 deletions
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index e24468da5..da5114048 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts | |||
@@ -70,9 +70,9 @@ export class PluginService implements ClientHook { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | initializePlugins () { | 72 | initializePlugins () { |
73 | this.server.configLoaded | 73 | this.server.getConfig() |
74 | .subscribe(() => { | 74 | .subscribe(config => { |
75 | this.plugins = this.server.getConfig().plugin.registered | 75 | this.plugins = config.plugin.registered |
76 | 76 | ||
77 | this.buildScopeStruct() | 77 | this.buildScopeStruct() |
78 | 78 | ||
diff --git a/client/src/app/core/routing/redirect.service.ts b/client/src/app/core/routing/redirect.service.ts index 43b89f08d..3982cf36f 100644 --- a/client/src/app/core/routing/redirect.service.ts +++ b/client/src/app/core/routing/redirect.service.ts | |||
@@ -16,15 +16,15 @@ export class RedirectService { | |||
16 | private serverService: ServerService | 16 | private serverService: ServerService |
17 | ) { | 17 | ) { |
18 | // The config is first loaded from the cache so try to get the default route | 18 | // The config is first loaded from the cache so try to get the default route |
19 | const config = this.serverService.getConfig() | 19 | const tmpConfig = this.serverService.getTmpConfig() |
20 | if (config && config.instance && config.instance.defaultClientRoute) { | 20 | if (tmpConfig && tmpConfig.instance && tmpConfig.instance.defaultClientRoute) { |
21 | RedirectService.DEFAULT_ROUTE = config.instance.defaultClientRoute | 21 | RedirectService.DEFAULT_ROUTE = tmpConfig.instance.defaultClientRoute |
22 | } | 22 | } |
23 | 23 | ||
24 | // Load default route | 24 | // Load default route |
25 | this.serverService.configLoaded | 25 | this.serverService.getConfig() |
26 | .subscribe(() => { | 26 | .subscribe(config => { |
27 | const defaultRouteConfig = this.serverService.getConfig().instance.defaultClientRoute | 27 | const defaultRouteConfig = config.instance.defaultClientRoute |
28 | 28 | ||
29 | if (defaultRouteConfig) { | 29 | if (defaultRouteConfig) { |
30 | RedirectService.DEFAULT_ROUTE = defaultRouteConfig | 30 | RedirectService.DEFAULT_ROUTE = defaultRouteConfig |
diff --git a/client/src/app/core/routing/server-config-resolver.service.ts b/client/src/app/core/routing/server-config-resolver.service.ts index ec7d6428f..3b7ed99bf 100644 --- a/client/src/app/core/routing/server-config-resolver.service.ts +++ b/client/src/app/core/routing/server-config-resolver.service.ts | |||
@@ -1,17 +1,13 @@ | |||
1 | import { Injectable } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | import { Resolve } from '@angular/router' | 2 | import { Resolve } from '@angular/router' |
3 | import { ServerService } from '@app/core/server' | 3 | import { ServerService } from '@app/core/server' |
4 | import { ServerConfig } from '@shared/models' | ||
4 | 5 | ||
5 | @Injectable() | 6 | @Injectable() |
6 | export class ServerConfigResolver implements Resolve<boolean> { | 7 | export class ServerConfigResolver implements Resolve<ServerConfig> { |
7 | constructor ( | 8 | constructor (private server: ServerService) {} |
8 | private server: ServerService | ||
9 | ) {} | ||
10 | 9 | ||
11 | resolve () { | 10 | resolve () { |
12 | // FIXME: directly returning this.server.configLoaded does not seem to work | 11 | return this.server.getConfig() |
13 | return new Promise<boolean>(res => { | ||
14 | return this.server.configLoaded.subscribe(() => res(true)) | ||
15 | }) | ||
16 | } | 12 | } |
17 | } | 13 | } |
diff --git a/client/src/app/core/server/server.service.ts b/client/src/app/core/server/server.service.ts index fdcc51cc5..ec904bf57 100644 --- a/client/src/app/core/server/server.service.ts +++ b/client/src/app/core/server/server.service.ts | |||
@@ -1,34 +1,36 @@ | |||
1 | import { map, shareReplay, switchMap, tap } from 'rxjs/operators' | 1 | import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators' |
2 | import { HttpClient } from '@angular/common/http' | 2 | import { HttpClient } from '@angular/common/http' |
3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' | 3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' | 4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
5 | import { Observable, of, ReplaySubject } from 'rxjs' | 5 | import { Observable, of, Subject } from 'rxjs' |
6 | import { getCompleteLocale, ServerConfig } from '../../../../../shared' | 6 | import { getCompleteLocale, ServerConfig } from '../../../../../shared' |
7 | import { environment } from '../../../environments/environment' | 7 | import { environment } from '../../../environments/environment' |
8 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' | 8 | import { VideoConstant } from '../../../../../shared/models/videos' |
9 | import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n' | 9 | import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n' |
10 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' | 10 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' |
11 | import { sortBy } from '@app/shared/misc/utils' | 11 | import { sortBy } from '@app/shared/misc/utils' |
12 | import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model' | ||
13 | import { cloneDeep } from 'lodash-es' | ||
14 | 12 | ||
15 | @Injectable() | 13 | @Injectable() |
16 | export class ServerService { | 14 | export class ServerService { |
17 | private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/' | ||
18 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' | 15 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' |
19 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | 16 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' |
20 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | 17 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' |
21 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' | 18 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' |
22 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' | 19 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' |
23 | 20 | ||
24 | configLoaded = new ReplaySubject<boolean>(1) | 21 | configReloaded = new Subject<void>() |
25 | videoPrivaciesLoaded = new ReplaySubject<boolean>(1) | ||
26 | videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1) | ||
27 | videoCategoriesLoaded = new ReplaySubject<boolean>(1) | ||
28 | videoLicencesLoaded = new ReplaySubject<boolean>(1) | ||
29 | videoLanguagesLoaded = new ReplaySubject<boolean>(1) | ||
30 | localeObservable: Observable<any> | ||
31 | 22 | ||
23 | private localeObservable: Observable<any> | ||
24 | private videoLicensesObservable: Observable<VideoConstant<number>[]> | ||
25 | private videoCategoriesObservable: Observable<VideoConstant<number>[]> | ||
26 | private videoPrivaciesObservable: Observable<VideoConstant<number>[]> | ||
27 | private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]> | ||
28 | private videoLanguagesObservable: Observable<VideoConstant<string>[]> | ||
29 | private configObservable: Observable<ServerConfig> | ||
30 | |||
31 | private configReset = false | ||
32 | |||
33 | private configLoaded = false | ||
32 | private config: ServerConfig = { | 34 | private config: ServerConfig = { |
33 | instance: { | 35 | instance: { |
34 | name: 'PeerTube', | 36 | name: 'PeerTube', |
@@ -121,132 +123,141 @@ export class ServerService { | |||
121 | enabled: true | 123 | enabled: true |
122 | } | 124 | } |
123 | } | 125 | } |
124 | private videoCategories: Array<VideoConstant<number>> = [] | ||
125 | private videoLicences: Array<VideoConstant<number>> = [] | ||
126 | private videoLanguages: Array<VideoConstant<string>> = [] | ||
127 | private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = [] | ||
128 | private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = [] | ||
129 | 126 | ||
130 | constructor ( | 127 | constructor ( |
131 | private http: HttpClient, | 128 | private http: HttpClient, |
132 | @Inject(LOCALE_ID) private localeId: string | 129 | @Inject(LOCALE_ID) private localeId: string |
133 | ) { | 130 | ) { |
134 | this.loadServerLocale() | ||
135 | this.loadConfigLocally() | 131 | this.loadConfigLocally() |
136 | } | 132 | } |
137 | 133 | ||
138 | loadConfig () { | 134 | getServerVersionAndCommit () { |
139 | this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | 135 | const serverVersion = this.config.serverVersion |
140 | .pipe(tap(this.saveConfigLocally)) | 136 | const commit = this.config.serverCommit || '' |
141 | .subscribe(data => { | ||
142 | this.config = data | ||
143 | 137 | ||
144 | this.configLoaded.next(true) | 138 | let result = serverVersion |
145 | }) | 139 | if (commit) result += '...' + commit |
146 | } | ||
147 | 140 | ||
148 | loadVideoCategories () { | 141 | return result |
149 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true) | ||
150 | } | 142 | } |
151 | 143 | ||
152 | loadVideoLicences () { | 144 | resetConfig () { |
153 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded) | 145 | this.configLoaded = false |
146 | this.configReset = true | ||
154 | } | 147 | } |
155 | 148 | ||
156 | loadVideoLanguages () { | 149 | getConfig () { |
157 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true) | 150 | if (this.configLoaded) return of(this.config) |
158 | } | ||
159 | 151 | ||
160 | loadVideoPrivacies () { | 152 | if (!this.configObservable) { |
161 | return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded) | 153 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) |
162 | } | 154 | .pipe( |
155 | tap(this.saveConfigLocally), | ||
156 | tap(() => this.configLoaded = true), | ||
157 | tap(() => { | ||
158 | if (this.configReset) { | ||
159 | this.configReloaded.next() | ||
160 | this.configReset = false | ||
161 | } | ||
162 | }), | ||
163 | share() | ||
164 | ) | ||
165 | } | ||
163 | 166 | ||
164 | loadVideoPlaylistPrivacies () { | 167 | return this.configObservable |
165 | return this.loadAttributeEnum( | ||
166 | ServerService.BASE_VIDEO_PLAYLIST_URL, | ||
167 | 'privacies', | ||
168 | this.videoPlaylistPrivacies, | ||
169 | this.videoPlaylistPrivaciesLoaded | ||
170 | ) | ||
171 | } | 168 | } |
172 | 169 | ||
173 | getConfig () { | 170 | getTmpConfig () { |
174 | return cloneDeep(this.config) | 171 | return this.config |
175 | } | ||
176 | |||
177 | getServerVersionAndCommit () { | ||
178 | const serverVersion = this.config.serverVersion | ||
179 | const commit = this.config.serverCommit || '' | ||
180 | |||
181 | let result = `v${serverVersion}` | ||
182 | if (commit) result += '...' + commit | ||
183 | |||
184 | return result | ||
185 | } | 172 | } |
186 | 173 | ||
187 | getVideoCategories () { | 174 | getVideoCategories () { |
188 | return cloneDeep(this.videoCategories) | 175 | if (!this.videoCategoriesObservable) { |
176 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | ||
177 | } | ||
178 | |||
179 | return this.videoCategoriesObservable.pipe(first()) | ||
189 | } | 180 | } |
190 | 181 | ||
191 | getVideoLicences () { | 182 | getVideoLicences () { |
192 | return cloneDeep(this.videoLicences) | 183 | if (!this.videoLicensesObservable) { |
184 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | ||
185 | } | ||
186 | |||
187 | return this.videoLicensesObservable.pipe(first()) | ||
193 | } | 188 | } |
194 | 189 | ||
195 | getVideoLanguages () { | 190 | getVideoLanguages () { |
196 | return cloneDeep(this.videoLanguages) | 191 | if (!this.videoLanguagesObservable) { |
192 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | ||
193 | } | ||
194 | |||
195 | return this.videoLanguagesObservable.pipe(first()) | ||
197 | } | 196 | } |
198 | 197 | ||
199 | getVideoPrivacies () { | 198 | getVideoPrivacies () { |
200 | return cloneDeep(this.videoPrivacies) | 199 | if (!this.videoPrivaciesObservable) { |
200 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | ||
201 | } | ||
202 | |||
203 | return this.videoPrivaciesObservable.pipe(first()) | ||
201 | } | 204 | } |
202 | 205 | ||
203 | getVideoPlaylistPrivacies () { | 206 | getVideoPlaylistPrivacies () { |
204 | return cloneDeep(this.videoPlaylistPrivacies) | 207 | if (!this.videoPlaylistPrivaciesObservable) { |
208 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | ||
209 | } | ||
210 | |||
211 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | ||
212 | } | ||
213 | |||
214 | getServerLocale () { | ||
215 | if (!this.localeObservable) { | ||
216 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | ||
217 | |||
218 | // Default locale, nothing to translate | ||
219 | if (isDefaultLocale(completeLocale)) { | ||
220 | this.localeObservable = of({}).pipe(shareReplay()) | ||
221 | } else { | ||
222 | this.localeObservable = this.http | ||
223 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | ||
224 | .pipe(shareReplay()) | ||
225 | } | ||
226 | } | ||
227 | |||
228 | return this.localeObservable.pipe(first()) | ||
205 | } | 229 | } |
206 | 230 | ||
207 | private loadAttributeEnum ( | 231 | private loadAttributeEnum <T extends string | number> ( |
208 | baseUrl: string, | 232 | baseUrl: string, |
209 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | 233 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
210 | hashToPopulate: VideoConstant<string | number>[], | ||
211 | notifier: ReplaySubject<boolean>, | ||
212 | sort = false | 234 | sort = false |
213 | ) { | 235 | ) { |
214 | this.localeObservable | 236 | return this.getServerLocale() |
215 | .pipe( | 237 | .pipe( |
216 | switchMap(translations => { | 238 | switchMap(translations => { |
217 | return this.http.get<{ [id: string]: string }>(baseUrl + attributeName) | 239 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) |
218 | .pipe(map(data => ({ data, translations }))) | 240 | .pipe(map(data => ({ data, translations }))) |
219 | }) | 241 | }), |
220 | ) | 242 | map(({ data, translations }) => { |
221 | .subscribe(({ data, translations }) => { | 243 | const hashToPopulate: VideoConstant<T>[] = [] |
222 | Object.keys(data) | ||
223 | .forEach(dataKey => { | ||
224 | const label = data[ dataKey ] | ||
225 | |||
226 | hashToPopulate.push({ | ||
227 | id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10), | ||
228 | label: peertubeTranslate(label, translations) | ||
229 | }) | ||
230 | }) | ||
231 | |||
232 | if (sort === true) sortBy(hashToPopulate, 'label') | ||
233 | |||
234 | notifier.next(true) | ||
235 | }) | ||
236 | } | ||
237 | 244 | ||
238 | private loadServerLocale () { | 245 | Object.keys(data) |
239 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | 246 | .forEach(dataKey => { |
247 | const label = data[ dataKey ] | ||
240 | 248 | ||
241 | // Default locale, nothing to translate | 249 | hashToPopulate.push({ |
242 | if (isDefaultLocale(completeLocale)) { | 250 | id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T, |
243 | this.localeObservable = of({}).pipe(shareReplay()) | 251 | label: peertubeTranslate(label, translations) |
244 | return | 252 | }) |
245 | } | 253 | }) |
254 | |||
255 | if (sort === true) sortBy(hashToPopulate, 'label') | ||
246 | 256 | ||
247 | this.localeObservable = this.http | 257 | return hashToPopulate |
248 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | 258 | }), |
249 | .pipe(shareReplay()) | 259 | shareReplay() |
260 | ) | ||
250 | } | 261 | } |
251 | 262 | ||
252 | private saveConfigLocally (config: ServerConfig) { | 263 | private saveConfigLocally (config: ServerConfig) { |
diff --git a/client/src/app/core/theme/theme.service.ts b/client/src/app/core/theme/theme.service.ts index 3eebc1acc..2c5873cb3 100644 --- a/client/src/app/core/theme/theme.service.ts +++ b/client/src/app/core/theme/theme.service.ts | |||
@@ -3,7 +3,7 @@ import { AuthService } from '@app/core/auth' | |||
3 | import { ServerService } from '@app/core/server' | 3 | import { ServerService } from '@app/core/server' |
4 | import { environment } from '../../../environments/environment' | 4 | import { environment } from '../../../environments/environment' |
5 | import { PluginService } from '@app/core/plugins/plugin.service' | 5 | import { PluginService } from '@app/core/plugins/plugin.service' |
6 | import { ServerConfigTheme } from '@shared/models' | 6 | import { ServerConfig, ServerConfigTheme } from '@shared/models' |
7 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' | 7 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
8 | import { first } from 'rxjs/operators' | 8 | import { first } from 'rxjs/operators' |
9 | 9 | ||
@@ -20,6 +20,8 @@ export class ThemeService { | |||
20 | private themeFromLocalStorage: ServerConfigTheme | 20 | private themeFromLocalStorage: ServerConfigTheme |
21 | private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] | 21 | private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = [] |
22 | 22 | ||
23 | private serverConfig: ServerConfig | ||
24 | |||
23 | constructor ( | 25 | constructor ( |
24 | private auth: AuthService, | 26 | private auth: AuthService, |
25 | private pluginService: PluginService, | 27 | private pluginService: PluginService, |
@@ -30,9 +32,12 @@ export class ThemeService { | |||
30 | // Try to load from local storage first, so we don't have to wait network requests | 32 | // Try to load from local storage first, so we don't have to wait network requests |
31 | this.loadAndSetFromLocalStorage() | 33 | this.loadAndSetFromLocalStorage() |
32 | 34 | ||
33 | this.server.configLoaded | 35 | this.serverConfig = this.server.getTmpConfig() |
34 | .subscribe(() => { | 36 | this.server.getConfig() |
35 | const themes = this.server.getConfig().theme.registered | 37 | .subscribe(config => { |
38 | this.serverConfig = config | ||
39 | |||
40 | const themes = this.serverConfig.theme.registered | ||
36 | 41 | ||
37 | this.removeThemeFromLocalStorageIfNeeded(themes) | 42 | this.removeThemeFromLocalStorageIfNeeded(themes) |
38 | this.injectThemes(themes) | 43 | this.injectThemes(themes) |
@@ -77,7 +82,7 @@ export class ThemeService { | |||
77 | if (theme !== 'instance-default') return theme | 82 | if (theme !== 'instance-default') return theme |
78 | } | 83 | } |
79 | 84 | ||
80 | return this.server.getConfig().theme.default | 85 | return this.serverConfig.theme.default |
81 | } | 86 | } |
82 | 87 | ||
83 | private loadTheme (name: string) { | 88 | private loadTheme (name: string) { |