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/server/about.model'
7 import { environment } from '../../../environments/environment'
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'
15 configLoaded = new ReplaySubject<boolean>(1)
16 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
17 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
18 videoLicencesLoaded = new ReplaySubject<boolean>(1)
19 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
21 private config: ServerConfig = {
29 serverVersion: 'Unknown',
34 enabledResolutions: []
52 private videoCategories: Array<{ id: number, label: string }> = []
53 private videoLicences: Array<{ id: number, label: string }> = []
54 private videoLanguages: Array<{ id: number, label: string }> = []
55 private videoPrivacies: Array<{ id: number, label: string }> = []
57 constructor (private http: HttpClient) {
58 this.loadConfigLocally()
62 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
63 .do(this.saveConfigLocally)
67 this.configLoaded.next(true)
71 loadVideoCategories () {
72 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
75 loadVideoLicences () {
76 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
79 loadVideoLanguages () {
80 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
83 loadVideoPrivacies () {
84 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
91 getVideoCategories () {
92 return this.videoCategories
96 return this.videoLicences
99 getVideoLanguages () {
100 return this.videoLanguages
103 getVideoPrivacies () {
104 return this.videoPrivacies
108 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
111 private loadVideoAttributeEnum (
112 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
113 hashToPopulate: { id: number, label: string }[],
114 notifier: ReplaySubject<boolean>,
117 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
120 .forEach(dataKey => {
121 hashToPopulate.push({
122 id: parseInt(dataKey, 10),
128 hashToPopulate.sort((a, b) => {
129 if (a.label < b.label) return -1
130 if (a.label === b.label) return 0
139 private saveConfigLocally (config: ServerConfig) {
140 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
143 private loadConfigLocally () {
144 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
148 const parsed = JSON.parse(configString)
149 Object.assign(this.config, parsed)
151 console.error('Cannot parse config saved in local storage.', err)