From 2d3741d6d92e9bd1f41694c7442a6d1da434e1f2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 30 Aug 2018 14:58:00 +0200 Subject: Videos overview page: first version --- client/src/app/shared/overview/index.ts | 1 + client/src/app/shared/overview/overview.service.ts | 76 ++++++++++++++++++++++ .../app/shared/overview/videos-overview.model.ts | 19 ++++++ 3 files changed, 96 insertions(+) create mode 100644 client/src/app/shared/overview/index.ts create mode 100644 client/src/app/shared/overview/overview.service.ts create mode 100644 client/src/app/shared/overview/videos-overview.model.ts (limited to 'client/src/app/shared/overview') 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 @@ +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 { RestService } from '../rest/rest.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 restService: RestService, + private videosService: VideoService, + private serverService: ServerService + ) {} + + getVideosOverview (): Observable { + return this.authHttp + .get(OverviewService.BASE_OVERVIEW_URL + 'videos') + .pipe( + switchMap(serverVideosOverview => this.updateVideosOverview(serverVideosOverview)), + catchError(err => this.restExtractor.handleError(err)) + ) + } + + private updateVideosOverview (serverVideosOverview: VideosOverviewServer): Observable { + const observables: Observable[] = [] + 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 })) + }) + ) + ) + } + } + + 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) + ) + } + +} 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 @@ +import { VideoChannelAttribute, VideoConstant, VideosOverview as VideosOverviewServer } from '../../../../../shared/models' +import { Video } from '@app/shared/video/video.model' + +export class VideosOverview implements VideosOverviewServer { + channels: { + channel: VideoChannelAttribute + videos: Video[] + }[] + + categories: { + category: VideoConstant + videos: Video[] + }[] + + tags: { + tag: string + videos: Video[] + }[] +} -- cgit v1.2.3