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