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 = {
25 defaultClientRoute: '',
31 serverVersion: 'Unknown',
36 enabledResolutions: []
54 private videoCategories: Array<{ id: number, label: string }> = []
55 private videoLicences: Array<{ id: number, label: string }> = []
56 private videoLanguages: Array<{ id: number, label: string }> = []
57 private videoPrivacies: Array<{ id: number, label: string }> = []
59 constructor (private http: HttpClient) {
60 this.loadConfigLocally()
64 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
65 .do(this.saveConfigLocally)
69 this.configLoaded.next(true)
73 loadVideoCategories () {
74 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
77 loadVideoLicences () {
78 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
81 loadVideoLanguages () {
82 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
85 loadVideoPrivacies () {
86 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
93 getVideoCategories () {
94 return this.videoCategories
98 return this.videoLicences
101 getVideoLanguages () {
102 return this.videoLanguages
105 getVideoPrivacies () {
106 return this.videoPrivacies
110 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
113 private loadVideoAttributeEnum (
114 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
115 hashToPopulate: { id: number, label: string }[],
116 notifier: ReplaySubject<boolean>,
119 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
122 .forEach(dataKey => {
123 hashToPopulate.push({
124 id: parseInt(dataKey, 10),
130 hashToPopulate.sort((a, b) => {
131 if (a.label < b.label) return -1
132 if (a.label === b.label) return 0
141 private saveConfigLocally (config: ServerConfig) {
142 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
145 private loadConfigLocally () {
146 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
150 const parsed = JSON.parse(configString)
151 Object.assign(this.config, parsed)
153 console.error('Cannot parse config saved in local storage.', err)