]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-main/video-caption/video-caption.service.ts
Display latest uploaded date for captions
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video-caption / video-caption.service.ts
index d45fb837ae6beba969fab8c1a92a1e403e97aff9..0f3afd116eb6dd11a84455aab045f009c0930947 100644 (file)
@@ -5,7 +5,9 @@ import { Injectable } from '@angular/core'
 import { RestExtractor, ServerService } from '@app/core'
 import { objectToFormData, sortBy } from '@app/helpers'
 import { VideoService } from '@app/shared/shared-main/video'
-import { peertubeTranslate, ResultList, VideoCaption } from '@shared/models'
+import { peertubeTranslate } from '@shared/core-utils/i18n'
+import { ResultList, VideoCaption } from '@shared/models'
+import { environment } from '../../../../environments/environment'
 import { VideoCaptionEdit } from './video-caption-edit.model'
 
 @Injectable()
@@ -16,8 +18,8 @@ export class VideoCaptionService {
     private restExtractor: RestExtractor
   ) {}
 
-  listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> {
-    return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions')
+  listCaptions (videoId: string): Observable<ResultList<VideoCaption>> {
+    return this.authHttp.get<ResultList<VideoCaption>>(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions`)
                .pipe(
                  switchMap(captionsResult => {
                    return this.serverService.getServerLocale()
@@ -40,29 +42,23 @@ export class VideoCaptionService {
   }
 
   removeCaption (videoId: number | string, language: string) {
-    return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(res => this.restExtractor.handleError(res))
-               )
+    return this.authHttp.delete(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/${language}`)
+               .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   addCaption (videoId: number | string, language: string, captionfile: File) {
     const body = { captionfile }
     const data = objectToFormData(body)
 
-    return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(res => this.restExtractor.handleError(res))
-               )
+    return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/${language}`, data)
+               .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
-    let obs = of(true)
+    let obs: Observable<any> = of(undefined)
 
     for (const videoCaption of videoCaptions) {
-      if (videoCaption.action === 'CREATE') {
+      if (videoCaption.action === 'CREATE' || videoCaption.action === 'UPDATE') {
         obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)))
       } else if (videoCaption.action === 'REMOVE') {
         obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id)))
@@ -71,4 +67,8 @@ export class VideoCaptionService {
 
     return obs
   }
+
+  getCaptionContent ({ captionPath }: Pick<VideoCaption, 'captionPath'>) {
+    return this.authHttp.get(environment.originServerUrl + captionPath, { responseType: 'text' })
+  }
 }