Sort video categories/languages
[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 import { ServerConfig } from '../../../../../shared'
6 import { About } from '../../../../../shared/models/config/about.model'
7 import { environment } from '../../../environments/environment'
8
9 @Injectable()
10 export class ServerService {
11   private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
12   private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
13   private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
14
15   videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
16   videoCategoriesLoaded = new ReplaySubject<boolean>(1)
17   videoLicencesLoaded = new ReplaySubject<boolean>(1)
18   videoLanguagesLoaded = new ReplaySubject<boolean>(1)
19
20   private config: ServerConfig = {
21     instance: {
22       name: 'PeerTube'
23     },
24     serverVersion: 'Unknown',
25     signup: {
26       allowed: false
27     },
28     transcoding: {
29       enabledResolutions: []
30     },
31     avatar: {
32       file: {
33         size: { max: 0 },
34         extensions: []
35       }
36     },
37     video: {
38       image: {
39         size: { max: 0 },
40         extensions: []
41       },
42       file: {
43         extensions: []
44       }
45     }
46   }
47   private videoCategories: Array<{ id: number, label: string }> = []
48   private videoLicences: Array<{ id: number, label: string }> = []
49   private videoLanguages: Array<{ id: number, label: string }> = []
50   private videoPrivacies: Array<{ id: number, label: string }> = []
51
52   constructor (private http: HttpClient) {
53     this.loadConfigLocally()
54   }
55
56   loadConfig () {
57     this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
58       .do(this.saveConfigLocally)
59       .subscribe(data => this.config = data)
60   }
61
62   loadVideoCategories () {
63     return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
64   }
65
66   loadVideoLicences () {
67     return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
68   }
69
70   loadVideoLanguages () {
71     return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
72   }
73
74   loadVideoPrivacies () {
75     return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
76   }
77
78   getConfig () {
79     return this.config
80   }
81
82   getVideoCategories () {
83     return this.videoCategories
84   }
85
86   getVideoLicences () {
87     return this.videoLicences
88   }
89
90   getVideoLanguages () {
91     return this.videoLanguages
92   }
93
94   getVideoPrivacies () {
95     return this.videoPrivacies
96   }
97
98   getAbout () {
99     return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
100   }
101
102   private loadVideoAttributeEnum (
103     attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
104     hashToPopulate: { id: number, label: string }[],
105     notifier: ReplaySubject<boolean>,
106     sort = false
107   ) {
108     return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
109        .subscribe(data => {
110          Object.keys(data)
111                .forEach(dataKey => {
112                  hashToPopulate.push({
113                    id: parseInt(dataKey, 10),
114                    label: data[dataKey]
115                  })
116                })
117
118          if (sort === true) {
119            hashToPopulate.sort((a, b) => {
120              if (a.label < b.label) return -1
121              if (a.label === b.label) return 0
122              return 1
123            })
124          }
125
126          notifier.next(true)
127        })
128   }
129
130   private saveConfigLocally (config: ServerConfig) {
131     localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
132   }
133
134   private loadConfigLocally () {
135     const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
136
137     if (configString) {
138       try {
139         const parsed = JSON.parse(configString)
140         Object.assign(this.config, parsed)
141       } catch (err) {
142         console.error('Cannot parse config saved in local storage.', err)
143       }
144     }
145   }
146 }