]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/video/video.service.ts
Add server localization
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
index d1e32faebd55719de1a4131ba7bb0f149bb302aa..c607b7d6a70ae0d69fb0d38a81718a2351da22d8 100644 (file)
@@ -1,4 +1,4 @@
-import { catchError, map } from 'rxjs/operators'
+import { catchError, map, switchMap } from 'rxjs/operators'
 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { Observable } from 'rxjs'
@@ -24,6 +24,7 @@ import { Account } from '@app/shared/account/account.model'
 import { AccountService } from '@app/shared/account/account.service'
 import { VideoChannel } from '../../../../../shared/models/videos'
 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
+import { ServerService } from '@app/core'
 
 @Injectable()
 export class VideoService {
@@ -33,7 +34,8 @@ export class VideoService {
   constructor (
     private authHttp: HttpClient,
     private restExtractor: RestExtractor,
-    private restService: RestService
+    private restService: RestService,
+    private serverService: ServerService
   ) {}
 
   getVideoViewUrl (uuid: string) {
@@ -41,9 +43,13 @@ export class VideoService {
   }
 
   getVideo (uuid: string): Observable<VideoDetails> {
-    return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
+    return this.serverService.localeObservable
                .pipe(
-                 map(videoHash => new VideoDetails(videoHash)),
+                 switchMap(translations => {
+                  return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
+                     .pipe(map(videoHash => ({ videoHash, translations })))
+                 }),
+                 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -102,9 +108,10 @@ export class VideoService {
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination, sort)
 
-    return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
+    return this.authHttp
+               .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
                .pipe(
-                 map(this.extractVideos),
+                 switchMap(res => this.extractVideos(res)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -120,9 +127,9 @@ export class VideoService {
     params = this.restService.addRestGetParams(params, pagination, sort)
 
     return this.authHttp
-               .get(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
+               .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
                .pipe(
-                 map(this.extractVideos),
+                 switchMap(res => this.extractVideos(res)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -138,9 +145,9 @@ export class VideoService {
     params = this.restService.addRestGetParams(params, pagination, sort)
 
     return this.authHttp
-               .get(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
+               .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
                .pipe(
-                 map(this.extractVideos),
+                 switchMap(res => this.extractVideos(res)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -160,9 +167,9 @@ export class VideoService {
     }
 
     return this.authHttp
-               .get(VideoService.BASE_VIDEO_URL, { params })
+               .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
                .pipe(
-                 map(this.extractVideos),
+                 switchMap(res => this.extractVideos(res)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -230,7 +237,7 @@ export class VideoService {
     return this.authHttp
                .get<ResultList<VideoServerModel>>(url, { params })
                .pipe(
-                 map(this.extractVideos),
+                 switchMap(res => this.extractVideos(res)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -287,14 +294,19 @@ export class VideoService {
   }
 
   private extractVideos (result: ResultList<VideoServerModel>) {
-    const videosJson = result.data
-    const totalVideos = result.total
-    const videos = []
-
-    for (const videoJson of videosJson) {
-      videos.push(new Video(videoJson))
-    }
-
-    return { videos, totalVideos }
+    return this.serverService.localeObservable
+      .pipe(
+        map(translations => {
+          const videosJson = result.data
+          const totalVideos = result.total
+          const videos: Video[] = []
+
+          for (const videoJson of videosJson) {
+            videos.push(new Video(videoJson, translations))
+          }
+
+          return { videos, totalVideos }
+        })
+      )
   }
 }