]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/video-caption/video-caption.service.ts
Fix accessibility action buttons and display on imports and followers list
[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'
8import { peertubeTranslate, ResultList, VideoCaption } from '@shared/models'
9import { VideoCaptionEdit } from './video-caption-edit.model'
40e87e9e
C
10
11@Injectable()
12export class VideoCaptionService {
13 constructor (
14 private authHttp: HttpClient,
3dfa8494 15 private serverService: ServerService,
40e87e9e
C
16 private restExtractor: RestExtractor
17 ) {}
18
19 listCaptions (videoId: number | string): Observable<ResultList<VideoCaption>> {
20 return this.authHttp.get<ResultList<VideoCaption>>(VideoService.BASE_VIDEO_URL + videoId + '/captions')
3dfa8494
C
21 .pipe(
22 switchMap(captionsResult => {
ba430d75 23 return this.serverService.getServerLocale()
3dfa8494
C
24 .pipe(map(translations => ({ captionsResult, translations })))
25 }),
26 map(({ captionsResult, translations }) => {
27 for (const c of captionsResult.data) {
28 c.language.label = peertubeTranslate(c.language.label, translations)
29 }
30
31 return captionsResult
32 }),
33 map(captionsResult => {
34 sortBy(captionsResult.data, 'language', 'label')
ad774752 35
3dfa8494
C
36 return captionsResult
37 })
38 )
40e87e9e
C
39 .pipe(catchError(res => this.restExtractor.handleError(res)))
40 }
41
42 removeCaption (videoId: number | string, language: string) {
43 return this.authHttp.delete(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language)
44 .pipe(
45 map(this.restExtractor.extractDataBool),
46 catchError(res => this.restExtractor.handleError(res))
47 )
48 }
49
50 addCaption (videoId: number | string, language: string, captionfile: File) {
51 const body = { captionfile }
52 const data = objectToFormData(body)
53
54 return this.authHttp.put(VideoService.BASE_VIDEO_URL + videoId + '/captions/' + language, data)
55 .pipe(
56 map(this.restExtractor.extractDataBool),
57 catchError(res => this.restExtractor.handleError(res))
58 )
59 }
60
61 updateCaptions (videoId: number | string, videoCaptions: VideoCaptionEdit[]) {
ed4c3c09 62 let obs = of(true)
40e87e9e
C
63
64 for (const videoCaption of videoCaptions) {
65 if (videoCaption.action === 'CREATE') {
ed4c3c09 66 obs = obs.pipe(switchMap(() => this.addCaption(videoId, videoCaption.language.id, videoCaption.captionfile)))
40e87e9e 67 } else if (videoCaption.action === 'REMOVE') {
ed4c3c09 68 obs = obs.pipe(switchMap(() => this.removeCaption(videoId, videoCaption.language.id)))
40e87e9e
C
69 }
70 }
71
ed4c3c09 72 return obs
40e87e9e
C
73 }
74}