aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/overview
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/overview')
-rw-r--r--client/src/app/shared/overview/index.ts1
-rw-r--r--client/src/app/shared/overview/overview.service.ts76
-rw-r--r--client/src/app/shared/overview/videos-overview.model.ts19
3 files changed, 96 insertions, 0 deletions
diff --git a/client/src/app/shared/overview/index.ts b/client/src/app/shared/overview/index.ts
new file mode 100644
index 000000000..2f7e41298
--- /dev/null
+++ b/client/src/app/shared/overview/index.ts
@@ -0,0 +1 @@
export * from './overview.service'
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}
diff --git a/client/src/app/shared/overview/videos-overview.model.ts b/client/src/app/shared/overview/videos-overview.model.ts
new file mode 100644
index 000000000..cf02bdb3d
--- /dev/null
+++ b/client/src/app/shared/overview/videos-overview.model.ts
@@ -0,0 +1,19 @@
1import { VideoChannelAttribute, VideoConstant, VideosOverview as VideosOverviewServer } from '../../../../../shared/models'
2import { Video } from '@app/shared/video/video.model'
3
4export class VideosOverview implements VideosOverviewServer {
5 channels: {
6 channel: VideoChannelAttribute
7 videos: Video[]
8 }[]
9
10 categories: {
11 category: VideoConstant<number>
12 videos: Video[]
13 }[]
14
15 tags: {
16 tag: string
17 videos: Video[]
18 }[]
19}