]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/video-caption/video-caption.service.ts
Remove unnecessary function
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video-caption / video-caption.service.ts
CommitLineData
67ed6552 1import { Observable, of } from 'rxjs'
3dfa8494 2import { catchError, map, switchMap } from 'rxjs/operators'
40e87e9e
C
3import { HttpClient } from '@angular/common/http'
4import { Injectable } from '@angular/core'
67ed6552
C
5import { RestExtractor, ServerService } from '@app/core'
6import { objectToFormData, sortBy } from '@app/helpers'
7import { VideoService } from '@app/shared/shared-main/video'
bd45d503
C
8import { peertubeTranslate } from '@shared/core-utils/i18n'
9import { ResultList, VideoCaption } from '@shared/models'
67ed6552 10import { VideoCaptionEdit } from './video-caption-edit.model'
40e87e9e
C
11
12@Injectable()
13export class VideoCaptionService {
14 constructor (
15 private authHttp: HttpClient,
3dfa8494 16 private serverService: ServerService,
40e87e9e
C
17 private restExtractor: RestExtractor
18 ) {}
19
20 listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> {
231ff4af 21 return this.authHttp.get<ResultList<VideoCaption>>(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions`)
3dfa8494
C
22 .pipe(
23 switchMap(captionsResult => {
ba430d75 24 return this.serverService.getServerLocale()
3dfa8494
C
25 .pipe(map(translations => ({ captionsResult, translations })))
26 }),
27 map(({ captionsResult, translations }) => {
28 for (const c of captionsResult.data) {
29 c.language.label = peertubeTranslate(c.language.label, translations)
30 }
31
32 return captionsResult
33 }),
34 map(captionsResult => {
35 sortBy(captionsResult.data, 'language', 'label')
ad774752 36
3dfa8494
C
37 return captionsResult
38 })
39 )
40e87e9e
C
40 .pipe(catchError(res => this.restExtractor.handleError(res)))
41 }
42
43 removeCaption (videoId: number | string, language: string) {
231ff4af 44 return this.authHttp.delete(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/${language}`)
e8bffe96 45 .pipe(catchError(res => this.restExtractor.handleError(res)))
40e87e9e
C
46 }
47
48 addCaption (videoId: number | string, language: string, captionfile: File) {
49 const body = { captionfile }
50 const data = objectToFormData(body)
51
231ff4af 52 return this.authHttp.put(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/${language}`, data)
e8bffe96 53 .pipe(catchError(res => this.restExtractor.handleError(res)))
40e87e9e
C
54 }
55
56 updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
e8bffe96 57 let obs: Observable<any> = of(undefined)
40e87e9e
C
58
59 for (const videoCaption of videoCaptions) {
60 if (videoCaption.action === 'CREATE') {
ed4c3c09 61 obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)))
40e87e9e 62 } else if (videoCaption.action === 'REMOVE') {
ed4c3c09 63 obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id)))
40e87e9e
C
64 }
65 }
66
ed4c3c09 67 return obs
40e87e9e
C
68 }
69}