]>
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' | |
54909304 | 8 | import { objectKeysTyped } from '@shared/core-utils' |
bd45d503 C |
9 | import { peertubeTranslate } from '@shared/core-utils/i18n' |
10 | import { VideosOverview as VideosOverviewServer } from '@shared/models' | |
67ed6552 C |
11 | import { environment } from '../../../../environments/environment' |
12 | import { VideosOverview } from './videos-overview.model' | |
2d3741d6 C |
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, | |
2d3741d6 C |
21 | private videosService: VideoService, |
22 | private serverService: ServerService | |
23 | ) {} | |
24 | ||
111fdc26 C |
25 | getVideosOverview (page: number): Observable<VideosOverview> { |
26 | let params = new HttpParams() | |
27 | params = params.append('page', page + '') | |
28 | ||
2d3741d6 | 29 | return this.authHttp |
111fdc26 | 30 | .get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos', { params }) |
2d3741d6 C |
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 | |
54909304 | 46 | for (const key of objectKeysTyped(serverVideosOverview)) { |
9df52d66 | 47 | for (const object of serverVideosOverview[key]) { |
2d3741d6 C |
48 | observables.push( |
49 | of(object.videos) | |
50 | .pipe( | |
51 | switchMap(videos => this.videosService.extractVideos({ total: 0, data: videos })), | |
93cae479 | 52 | map(result => result.data), |
2d3741d6 | 53 | tap(videos => { |
54909304 C |
54 | // FIXME: typings & lint |
55 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | |
56 | videosOverviewResult[key].push(immutableAssign(object, { videos }) as any) | |
2d3741d6 C |
57 | }) |
58 | ) | |
59 | ) | |
60 | } | |
61 | } | |
62 | ||
860cfb31 C |
63 | if (observables.length === 0) return of(videosOverviewResult) |
64 | ||
2d3741d6 C |
65 | return forkJoin(observables) |
66 | .pipe( | |
67 | // Translate categories | |
68 | switchMap(() => { | |
ba430d75 | 69 | return this.serverService.getServerLocale() |
2d3741d6 C |
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 | } |