aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/overview/overview.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/overview/overview.service.ts')
-rw-r--r--client/src/app/shared/overview/overview.service.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/client/src/app/shared/overview/overview.service.ts b/client/src/app/shared/overview/overview.service.ts
new file mode 100644
index 000000000..4a4714af6
--- /dev/null
+++ b/client/src/app/shared/overview/overview.service.ts
@@ -0,0 +1,76 @@
1import { catchError, map, switchMap, tap } from 'rxjs/operators'
2import { HttpClient } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { forkJoin, Observable, of } from 'rxjs'
5import { VideosOverview as VideosOverviewServer, peertubeTranslate } from '../../../../../shared/models'
6import { environment } from '../../../environments/environment'
7import { RestExtractor } from '../rest/rest-extractor.service'
8import { RestService } from '../rest/rest.service'
9import { VideosOverview } from '@app/shared/overview/videos-overview.model'
10import { VideoService } from '@app/shared/video/video.service'
11import { ServerService } from '@app/core'
12import { immutableAssign } from '@app/shared/misc/utils'
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 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 return forkJoin(observables)
60 .pipe(
61 // Translate categories
62 switchMap(() => {
63 return this.serverService.localeObservable
64 .pipe(
65 tap(translations => {
66 for (const c of videosOverviewResult.categories) {
67 c.category.label = peertubeTranslate(c.category.label, translations)
68 }
69 })
70 )
71 }),
72 map(() => videosOverviewResult)
73 )
74 }
75
76}