]>
Commit | Line | Data |
---|---|---|
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 { objectKeysTyped } from '@shared/core-utils' | |
9 | import { peertubeTranslate } from '@shared/core-utils/i18n' | |
10 | import { VideosOverview as VideosOverviewServer } from '@shared/models' | |
11 | import { environment } from '../../../../environments/environment' | |
12 | import { VideosOverview } from './videos-overview.model' | |
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 videosService: VideoService, | |
22 | private serverService: ServerService | |
23 | ) {} | |
24 | ||
25 | getVideosOverview (page: number): Observable<VideosOverview> { | |
26 | let params = new HttpParams() | |
27 | params = params.append('page', page + '') | |
28 | ||
29 | return this.authHttp | |
30 | .get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos', { params }) | |
31 | .pipe( | |
32 | switchMap(serverVideosOverview => this.updateVideosOverview(serverVideosOverview)), | |
33 | catchError(err => this.restExtractor.handleError(err)) | |
34 | ) | |
35 | } | |
36 | ||
37 | private updateVideosOverview (serverVideosOverview: VideosOverviewServer): Observable<VideosOverview> { | |
38 | const observables: Observable<any>[] = [] | |
39 | const videosOverviewResult: VideosOverview = { | |
40 | tags: [], | |
41 | categories: [], | |
42 | channels: [] | |
43 | } | |
44 | ||
45 | // Build videos objects | |
46 | for (const key of objectKeysTyped(serverVideosOverview)) { | |
47 | for (const object of serverVideosOverview[key]) { | |
48 | observables.push( | |
49 | of(object.videos) | |
50 | .pipe( | |
51 | switchMap(videos => this.videosService.extractVideos({ total: 0, data: videos })), | |
52 | map(result => result.data), | |
53 | tap(videos => { | |
54 | // FIXME: typings & lint | |
55 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | |
56 | videosOverviewResult[key].push(immutableAssign(object, { videos }) as any) | |
57 | }) | |
58 | ) | |
59 | ) | |
60 | } | |
61 | } | |
62 | ||
63 | if (observables.length === 0) return of(videosOverviewResult) | |
64 | ||
65 | return forkJoin(observables) | |
66 | .pipe( | |
67 | // Translate categories | |
68 | switchMap(() => { | |
69 | return this.serverService.getServerLocale() | |
70 | .pipe( | |
71 | tap(translations => { | |
72 | for (const c of videosOverviewResult.categories) { | |
73 | c.category.label = peertubeTranslate(c.category.label, translations) | |
74 | } | |
75 | }) | |
76 | ) | |
77 | }), | |
78 | map(() => videosOverviewResult) | |
79 | ) | |
80 | } | |
81 | ||
82 | } |