diff options
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.ts | 78 |
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 @@ | |||
1 | import { forkJoin, Observable, of } from 'rxjs' | ||
2 | import { catchError, map, switchMap, tap } from 'rxjs/operators' | ||
3 | import { HttpClient, HttpParams } from '@angular/common/http' | ||
4 | import { Injectable } from '@angular/core' | ||
5 | import { RestExtractor, ServerService } from '@app/core' | ||
6 | import { immutableAssign } from '@app/helpers' | ||
7 | import { VideoService } from '@app/shared/shared-main' | ||
8 | import { peertubeTranslate, VideosOverview as VideosOverviewServer } from '@shared/models' | ||
9 | import { environment } from '../../../../environments/environment' | ||
10 | import { VideosOverview } from './videos-overview.model' | ||
11 | |||
12 | @Injectable() | ||
13 | export 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 | } | ||