1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import { catchError, map, switchMap, tap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { forkJoin, Observable, of } from 'rxjs'
import { VideosOverview as VideosOverviewServer, peertubeTranslate } from '../../../../../shared/models'
import { environment } from '../../../environments/environment'
import { RestExtractor } from '../rest/rest-extractor.service'
import { VideosOverview } from '@app/shared/overview/videos-overview.model'
import { VideoService } from '@app/shared/video/video.service'
import { ServerService } from '@app/core'
import { immutableAssign } from '@app/shared/misc/utils'
@Injectable()
export class OverviewService {
static BASE_OVERVIEW_URL = environment.apiUrl + '/api/v1/overviews/'
constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor,
private videosService: VideoService,
private serverService: ServerService
) {}
getVideosOverview (): Observable<VideosOverview> {
return this.authHttp
.get<VideosOverviewServer>(OverviewService.BASE_OVERVIEW_URL + 'videos')
.pipe(
switchMap(serverVideosOverview => this.updateVideosOverview(serverVideosOverview)),
catchError(err => this.restExtractor.handleError(err))
)
}
private updateVideosOverview (serverVideosOverview: VideosOverviewServer): Observable<VideosOverview> {
const observables: Observable<any>[] = []
const videosOverviewResult: VideosOverview = {
tags: [],
categories: [],
channels: []
}
// Build videos objects
for (const key of Object.keys(serverVideosOverview)) {
for (const object of serverVideosOverview[ key ]) {
observables.push(
of(object.videos)
.pipe(
switchMap(videos => this.videosService.extractVideos({ total: 0, data: videos })),
map(result => result.videos),
tap(videos => {
videosOverviewResult[key].push(immutableAssign(object, { videos }))
})
)
)
}
}
if (observables.length === 0) return of(videosOverviewResult)
return forkJoin(observables)
.pipe(
// Translate categories
switchMap(() => {
return this.serverService.localeObservable
.pipe(
tap(translations => {
for (const c of videosOverviewResult.categories) {
c.category.label = peertubeTranslate(c.category.label, translations)
}
})
)
}),
map(() => videosOverviewResult)
)
}
}
|