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 { peertubeTranslate } from '@shared/core-utils/i18n'
9 import { VideosOverview as VideosOverviewServer } from '@shared/models'
10 import { environment } from '../../../../environments/environment'
11 import { VideosOverview } from './videos-overview.model'
14 export class OverviewService {
15 static BASE_OVERVIEW_URL = environment.apiUrl + '/api/v1/overviews/'
18 private authHttp: HttpClient,
19 private restExtractor: RestExtractor,
20 private videosService: VideoService,
21 private serverService: ServerService
24 getVideosOverview (page: number): Observable<VideosOverview> {
25 let params = new HttpParams()
26 params = params.append('page', page + '')
29 .get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos', { params })
31 switchMap(serverVideosOverview => this.updateVideosOverview(serverVideosOverview)),
32 catchError(err => this.restExtractor.handleError(err))
36 private updateVideosOverview (serverVideosOverview: VideosOverviewServer): Observable<VideosOverview> {
37 const observables: Observable<any>[] = []
38 const videosOverviewResult: VideosOverview = {
44 // Build videos objects
45 for (const key of Object.keys(serverVideosOverview)) {
46 for (const object of serverVideosOverview[ key ]) {
50 switchMap(videos => this.videosService.extractVideos({ total: 0, data: videos })),
51 map(result => result.data),
53 videosOverviewResult[key].push(immutableAssign(object, { videos }))
60 if (observables.length === 0) return of(videosOverviewResult)
62 return forkJoin(observables)
64 // Translate categories
66 return this.serverService.getServerLocale()
69 for (const c of videosOverviewResult.categories) {
70 c.category.label = peertubeTranslate(c.category.label, translations)
75 map(() => videosOverviewResult)