]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/overview/overview.service.ts
Fix "no results" on overview page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / overview / overview.service.ts
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 if (observables.length === 0) return of(videosOverviewResult)
60
61 return forkJoin(observables)
62 .pipe(
63 // Translate categories
64 switchMap(() => {
65 return this.serverService.localeObservable
66 .pipe(
67 tap(translations => {
68 for (const c of videosOverviewResult.categories) {
69 c.category.label = peertubeTranslate(c.category.label, translations)
70 }
71 })
72 )
73 }),
74 map(() => videosOverviewResult)
75 )
76 }
77
78 }