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'
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 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
16 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
17 videoLicencesLoaded = new ReplaySubject<boolean>(1)
18 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
20 private config: ServerConfig = {
24 serverVersion: 'Unknown',
29 enabledResolutions: []
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 }> = []
52 constructor (private http: HttpClient) {
53 this.loadConfigLocally()
57 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
58 .do(this.saveConfigLocally)
59 .subscribe(data => this.config = data)
62 loadVideoCategories () {
63 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
66 loadVideoLicences () {
67 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
70 loadVideoLanguages () {
71 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
74 loadVideoPrivacies () {
75 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
82 getVideoCategories () {
83 return this.videoCategories
87 return this.videoLicences
90 getVideoLanguages () {
91 return this.videoLanguages
94 getVideoPrivacies () {
95 return this.videoPrivacies
99 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
102 private loadVideoAttributeEnum (
103 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
104 hashToPopulate: { id: number, label: string }[],
105 notifier: ReplaySubject<boolean>
107 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
110 .forEach(dataKey => {
111 hashToPopulate.push({
112 id: parseInt(dataKey, 10),
121 private saveConfigLocally (config: ServerConfig) {
122 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
125 private loadConfigLocally () {
126 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
130 const parsed = JSON.parse(configString)
131 Object.assign(this.config, parsed)
133 console.error('Cannot parse config saved in local storage.', err)