aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+videos/video-list/overview/overview.service.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-06-23 14:49:20 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-06-23 16:00:49 +0200
commit1942f11d5ee6926ad93dc1b79fae18325ba5de18 (patch)
tree3f2a3cd9466a56c419d197ac832a3e9cbc86bec4 /client/src/app/+videos/video-list/overview/overview.service.ts
parent67ed6552b831df66713bac9e672738796128d33f (diff)
downloadPeerTube-1942f11d5ee6926ad93dc1b79fae18325ba5de18.tar.gz
PeerTube-1942f11d5ee6926ad93dc1b79fae18325ba5de18.tar.zst
PeerTube-1942f11d5ee6926ad93dc1b79fae18325ba5de18.zip
Lazy load all routes
Diffstat (limited to 'client/src/app/+videos/video-list/overview/overview.service.ts')
-rw-r--r--client/src/app/+videos/video-list/overview/overview.service.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/client/src/app/+videos/video-list/overview/overview.service.ts b/client/src/app/+videos/video-list/overview/overview.service.ts
new file mode 100644
index 000000000..4458454d5
--- /dev/null
+++ b/client/src/app/+videos/video-list/overview/overview.service.ts
@@ -0,0 +1,78 @@
1import { forkJoin, Observable, of } from 'rxjs'
2import { catchError, map, switchMap, tap } from 'rxjs/operators'
3import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core'
5import { RestExtractor, ServerService } from '@app/core'
6import { immutableAssign } from '@app/helpers'
7import { VideoService } from '@app/shared/shared-main'
8import { peertubeTranslate, VideosOverview as VideosOverviewServer } from '@shared/models'
9import { environment } from '../../../../environments/environment'
10import { VideosOverview } from './videos-overview.model'
11
12@Injectable()
13export class OverviewService {
14 static BASE_OVERVIEW_URL = environment.apiUrl + '/api/v1/overviews/'
15
16 constructor (
17 private authHttp: HttpClient,
18 private restExtractor: RestExtractor,
19 private videosService: VideoService,
20 private serverService: ServerService
21 ) {}
22
23 getVideosOverview (page: number): Observable<VideosOverview> {
24 let params = new HttpParams()
25 params = params.append('page', page + '')
26
27 return this.authHttp
28 .get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos', { params })
29 .pipe(
30 switchMap(serverVideosOverview => this.updateVideosOverview(serverVideosOverview)),
31 catchError(err => this.restExtractor.handleError(err))
32 )
33 }
34
35 private updateVideosOverview (serverVideosOverview: VideosOverviewServer): Observable<VideosOverview> {
36 const observables: Observable<any>[] = []
37 const videosOverviewResult: VideosOverview = {
38 tags: [],
39 categories: [],
40 channels: []
41 }
42
43 // Build videos objects
44 for (const key of Object.keys(serverVideosOverview)) {
45 for (const object of serverVideosOverview[ key ]) {
46 observables.push(
47 of(object.videos)
48 .pipe(
49 switchMap(videos => this.videosService.extractVideos({ total: 0, data: videos })),
50 map(result => result.data),
51 tap(videos => {
52 videosOverviewResult[key].push(immutableAssign(object, { videos }))
53 })
54 )
55 )
56 }
57 }
58
59 if (observables.length === 0) return of(videosOverviewResult)
60
61 return forkJoin(observables)
62 .pipe(
63 // Translate categories
64 switchMap(() => {
65 return this.serverService.getServerLocale()
66 .pipe(
67 tap(translations => {
68 for (const c of videosOverviewResult.categories) {
69 c.category.label = peertubeTranslate(c.category.label, translations)
70 }
71 })
72 )
73 }),
74 map(() => videosOverviewResult)
75 )
76 }
77
78}