]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, map, switchMap, tap } from 'rxjs/operators' | |
2 | import { HttpClient } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { forkJoin, Observable, of } from 'rxjs' | |
5 | import { VideosOverview as VideosOverviewServer, peertubeTranslate } from '../../../../../shared/models' | |
6 | import { environment } from '../../../environments/environment' | |
7 | import { RestExtractor } from '../rest/rest-extractor.service' | |
8 | import { RestService } from '../rest/rest.service' | |
9 | import { VideosOverview } from '@app/shared/overview/videos-overview.model' | |
10 | import { VideoService } from '@app/shared/video/video.service' | |
11 | import { ServerService } from '@app/core' | |
12 | import { immutableAssign } from '@app/shared/misc/utils' | |
13 | ||
14 | @Injectable() | |
15 | export class OverviewService { | |
16 | static BASE_OVERVIEW_URL = environment.apiUrl + '/api/v1/overviews/' | |
17 | ||
18 | constructor ( | |
19 | private authHttp: HttpClient, | |
20 | private restExtractor: RestExtractor, | |
21 | private restService: RestService, | |
22 | private videosService: VideoService, | |
23 | private serverService: ServerService | |
24 | ) {} | |
25 | ||
26 | getVideosOverview (): Observable<VideosOverview> { | |
27 | return this.authHttp | |
28 | .get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos') | |
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.videos), | |
51 | tap(videos => { | |
52 | videosOverviewResult[key].push(immutableAssign(object, { videos })) | |
53 | }) | |
54 | ) | |
55 | ) | |
56 | } | |
57 | } | |
58 | ||
59 | return forkJoin(observables) | |
60 | .pipe( | |
61 | // Translate categories | |
62 | switchMap(() => { | |
63 | return this.serverService.localeObservable | |
64 | .pipe( | |
65 | tap(translations => { | |
66 | for (const c of videosOverviewResult.categories) { | |
67 | c.category.label = peertubeTranslate(c.category.label, translations) | |
68 | } | |
69 | }) | |
70 | ) | |
71 | }), | |
72 | map(() => videosOverviewResult) | |
73 | ) | |
74 | } | |
75 | ||
76 | } |