aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/server/server.service.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-12-18 15:31:54 +0100
committerChocobozzz <me@florianbigard.com>2019-12-18 15:40:59 +0100
commitba430d7516bc5b1324b60571ba7594460969b7fb (patch)
treedf5c6952c82f49a94c0a884bbc97d4a0cbd9f867 /client/src/app/core/server/server.service.ts
parent5dfb7c1dec8222b0bbccac5b56ad46da1438747e (diff)
downloadPeerTube-ba430d7516bc5b1324b60571ba7594460969b7fb.tar.gz
PeerTube-ba430d7516bc5b1324b60571ba7594460969b7fb.tar.zst
PeerTube-ba430d7516bc5b1324b60571ba7594460969b7fb.zip
Lazy load static objects
Diffstat (limited to 'client/src/app/core/server/server.service.ts')
-rw-r--r--client/src/app/core/server/server.service.ts205
1 files changed, 108 insertions, 97 deletions
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 @@
1import { map, shareReplay, switchMap, tap } from 'rxjs/operators' 1import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
2import { HttpClient } from '@angular/common/http' 2import { HttpClient } from '@angular/common/http'
3import { Inject, Injectable, LOCALE_ID } from '@angular/core' 3import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
5import { Observable, of, ReplaySubject } from 'rxjs' 5import { Observable, of, Subject } from 'rxjs'
6import { getCompleteLocale, ServerConfig } from '../../../../../shared' 6import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7import { environment } from '../../../environments/environment' 7import { environment } from '../../../environments/environment'
8import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' 8import { VideoConstant } from '../../../../../shared/models/videos'
9import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n' 9import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' 10import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11import { sortBy } from '@app/shared/misc/utils' 11import { sortBy } from '@app/shared/misc/utils'
12import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
13import { cloneDeep } from 'lodash-es'
14 12
15@Injectable() 13@Injectable()
16export class ServerService { 14export 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) {