-import { catchError, map } from 'rxjs/operators'
+import { catchError, map, switchMap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
-import { forkJoin, Observable, of } from 'rxjs'
-import { ResultList } from '../../../../../shared'
-import { RestExtractor, RestService } from '../rest'
-import { VideoCaption } from '../../../../../shared/models/videos/video-caption.model'
+import { Observable, of } from 'rxjs'
+import { peertubeTranslate, ResultList } from '../../../../../shared'
+import { RestExtractor } from '../rest'
import { VideoService } from '@app/shared/video/video.service'
-import { objectToFormData } from '@app/shared/misc/utils'
+import { objectToFormData, sortBy } from '@app/shared/misc/utils'
import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
+import { VideoCaption } from '../../../../../shared/models/videos/caption/video-caption.model'
+import { ServerService } from '@app/core'
@Injectable()
export class VideoCaptionService {
constructor (
private authHttp: HttpClient,
- private restService: RestService,
+ private serverService: ServerService,
private restExtractor: RestExtractor
) {}
listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> {
return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions')
+ .pipe(
+ switchMap(captionsResult => {
+ return this.serverService.getServerLocale()
+ .pipe(map(translations => ({ captionsResult, translations })))
+ }),
+ map(({ captionsResult, translations }) => {
+ for (const c of captionsResult.data) {
+ c.language.label = peertubeTranslate(c.language.label, translations)
+ }
+
+ return captionsResult
+ }),
+ map(captionsResult => {
+ sortBy(captionsResult.data, 'language', 'label')
+
+ return captionsResult
+ })
+ )
.pipe(catchError(res => this.restExtractor.handleError(res)))
}
}
updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
- if (videoCaptions.length === 0) return of(true)
-
- const observables: Observable<any>[] = []
+ let obs = of(true)
for (const videoCaption of videoCaptions) {
if (videoCaption.action === 'CREATE') {
- observables.push(
- this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)
- )
+ obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)))
} else if (videoCaption.action === 'REMOVE') {
- observables.push(
- this.removeCaption(videoId, videoCaption.language.id)
- )
+ obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id)))
}
}
- return forkJoin(observables)
+ return obs
}
}