Design second video upload step
[oweals/peertube.git] / client / src / app / core / server / server.service.ts
1 import { HttpClient } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import 'rxjs/add/operator/do'
4 import { ReplaySubject } from 'rxjs/ReplaySubject'
5
6 import { ServerConfig } from '../../../../../shared'
7
8 @Injectable()
9 export class ServerService {
10   private static BASE_CONFIG_URL = API_URL + '/api/v1/config/'
11   private static BASE_VIDEO_URL = API_URL + '/api/v1/videos/'
12
13   videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
14   videoCategoriesLoaded = new ReplaySubject<boolean>(1)
15   videoLicencesLoaded = new ReplaySubject<boolean>(1)
16   videoLanguagesLoaded = new ReplaySubject<boolean>(1)
17
18   private config: ServerConfig = {
19     signup: {
20       allowed: false
21     },
22     transcoding: {
23       enabledResolutions: []
24     }
25   }
26   private videoCategories: Array<{ id: number, label: string }> = []
27   private videoLicences: Array<{ id: number, label: string }> = []
28   private videoLanguages: Array<{ id: number, label: string }> = []
29   private videoPrivacies: Array<{ id: number, label: string }> = []
30
31   constructor (private http: HttpClient) {}
32
33   loadConfig () {
34     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
35              .subscribe(data => this.config = data)
36   }
37
38   loadVideoCategories () {
39     return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
40   }
41
42   loadVideoLicences () {
43     return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
44   }
45
46   loadVideoLanguages () {
47     return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
48   }
49
50   loadVideoPrivacies () {
51     return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
52   }
53
54   getConfig () {
55     return this.config
56   }
57
58   getVideoCategories () {
59     return this.videoCategories
60   }
61
62   getVideoLicences () {
63     return this.videoLicences
64   }
65
66   getVideoLanguages () {
67     return this.videoLanguages
68   }
69
70   getVideoPrivacies () {
71     return this.videoPrivacies
72   }
73
74   private loadVideoAttributeEnum (
75     attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
76     hashToPopulate: { id: number, label: string }[],
77     notifier: ReplaySubject<boolean>
78   ) {
79     return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
80        .subscribe(data => {
81          Object.keys(data)
82                .forEach(dataKey => {
83                  hashToPopulate.push({
84                    id: parseInt(dataKey, 10),
85                    label: data[dataKey]
86                  })
87                })
88
89          notifier.next(true)
90        })
91   }
92 }