]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { forkJoin, Observable, of } from 'rxjs' |
2d3741d6 | 2 | import { catchError, map, switchMap, tap } from 'rxjs/operators' |
111fdc26 | 3 | import { HttpClient, HttpParams } from '@angular/common/http' |
2d3741d6 | 4 | import { Injectable } from '@angular/core' |
67ed6552 C |
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' | |
2d3741d6 C |
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, | |
2d3741d6 C |
19 | private videosService: VideoService, |
20 | private serverService: ServerService | |
21 | ) {} | |
22 | ||
111fdc26 C |
23 | getVideosOverview (page: number): Observable<VideosOverview> { |
24 | let params = new HttpParams() | |
25 | params = params.append('page', page + '') | |
26 | ||
2d3741d6 | 27 | return this.authHttp |
111fdc26 | 28 | .get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos', { params }) |
2d3741d6 C |
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 })), | |
93cae479 | 50 | map(result => result.data), |
2d3741d6 C |
51 | tap(videos => { |
52 | videosOverviewResult[key].push(immutableAssign(object, { videos })) | |
53 | }) | |
54 | ) | |
55 | ) | |
56 | } | |
57 | } | |
58 | ||
860cfb31 C |
59 | if (observables.length === 0) return of(videosOverviewResult) |
60 | ||
2d3741d6 C |
61 | return forkJoin(observables) |
62 | .pipe( | |
63 | // Translate categories | |
64 | switchMap(() => { | |
ba430d75 | 65 | return this.serverService.getServerLocale() |
2d3741d6 C |
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 | } |