]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/+videos/video-list/overview/overview.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / video-list / overview / overview.service.ts
... / ...
CommitLineData
1import { forkJoin, Observable, of } from 'rxjs'
2import { catchError, map, switchMap, tap } from 'rxjs/operators'
3import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core'
5import { RestExtractor, ServerService } from '@app/core'
6import { immutableAssign } from '@app/helpers'
7import { VideoService } from '@app/shared/shared-main'
8import { objectKeysTyped } from '@shared/core-utils'
9import { peertubeTranslate } from '@shared/core-utils/i18n'
10import { VideosOverview as VideosOverviewServer } from '@shared/models'
11import { environment } from '../../../../environments/environment'
12import { VideosOverview } from './videos-overview.model'
13
14@Injectable()
15export 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}