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 = {
24 defaultClientRoute: '',
30 serverVersion: 'Unknown',
35 enabledResolutions: []
53 private videoCategories: Array<{ id: number, label: string }> = []
54 private videoLicences: Array<{ id: number, label: string }> = []
55 private videoLanguages: Array<{ id: number, label: string }> = []
56 private videoPrivacies: Array<{ id: number, label: string }> = []
58 constructor (private http: HttpClient) {
59 this.loadConfigLocally()
63 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
64 .do(this.saveConfigLocally)
68 this.configLoaded.next(true)
72 loadVideoCategories () {
73 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
76 loadVideoLicences () {
77 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
80 loadVideoLanguages () {
81 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
84 loadVideoPrivacies () {
85 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
92 getVideoCategories () {
93 return this.videoCategories
97 return this.videoLicences
100 getVideoLanguages () {
101 return this.videoLanguages
104 getVideoPrivacies () {
105 return this.videoPrivacies
109 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
112 private loadVideoAttributeEnum (
113 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
114 hashToPopulate: { id: number, label: string }[],
115 notifier: ReplaySubject<boolean>,
118 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
121 .forEach(dataKey => {
122 hashToPopulate.push({
123 id: parseInt(dataKey, 10),
129 hashToPopulate.sort((a, b) => {
130 if (a.label < b.label) return -1
131 if (a.label === b.label) return 0
140 private saveConfigLocally (config: ServerConfig) {
141 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
144 private loadConfigLocally () {
145 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
149 const parsed = JSON.parse(configString)
150 Object.assign(this.config, parsed)
152 console.error('Cannot parse config saved in local storage.', err)