Normalize modal close buttons, and cancel/submit button styles
[oweals/peertube.git] / client / src / app / shared / video / redundancy.service.ts
1 import { catchError, map, toArray } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { RestExtractor, RestPagination, RestService } from '@app/shared/rest'
5 import { SortMeta } from 'primeng/api'
6 import { ResultList, Video, VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
7 import { concat, Observable } from 'rxjs'
8 import { environment } from '../../../environments/environment'
9
10 @Injectable()
11 export class RedundancyService {
12   static BASE_REDUNDANCY_URL = environment.apiUrl + '/api/v1/server/redundancy'
13
14   constructor (
15     private authHttp: HttpClient,
16     private restService: RestService,
17     private restExtractor: RestExtractor
18   ) { }
19
20   updateRedundancy (host: string, redundancyAllowed: boolean) {
21     const url = RedundancyService.BASE_REDUNDANCY_URL + '/' + host
22
23     const body = { redundancyAllowed }
24
25     return this.authHttp.put(url, body)
26                .pipe(
27                  map(this.restExtractor.extractDataBool),
28                  catchError(err => this.restExtractor.handleError(err))
29                )
30   }
31
32   listVideoRedundancies (options: {
33     pagination: RestPagination,
34     sort: SortMeta,
35     target?: VideoRedundanciesTarget
36   }): Observable<ResultList<VideoRedundancy>> {
37     const { pagination, sort, target } = options
38
39     let params = new HttpParams()
40     params = this.restService.addRestGetParams(params, pagination, sort)
41
42     if (target) params = params.append('target', target)
43
44     return this.authHttp.get<ResultList<VideoRedundancy>>(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { params })
45                .pipe(
46                  catchError(res => this.restExtractor.handleError(res))
47                )
48   }
49
50   addVideoRedundancy (video: Video) {
51     return this.authHttp.post(RedundancyService.BASE_REDUNDANCY_URL + '/videos', { videoId: video.id })
52       .pipe(
53         catchError(res => this.restExtractor.handleError(res))
54       )
55   }
56
57   removeVideoRedundancies (redundancy: VideoRedundancy) {
58     const observables = redundancy.redundancies.streamingPlaylists.map(r => r.id)
59       .concat(redundancy.redundancies.files.map(r => r.id))
60       .map(id => this.removeRedundancy(id))
61
62     return concat(...observables)
63       .pipe(toArray())
64   }
65
66   private removeRedundancy (redundancyId: number) {
67     return this.authHttp.delete(RedundancyService.BASE_REDUNDANCY_URL + '/videos/' + redundancyId)
68                .pipe(
69                  map(this.restExtractor.extractDataBool),
70                  catchError(res => this.restExtractor.handleError(res))
71                )
72   }
73 }