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 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
25 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
26 defaultClientRoute: '',
32 serverVersion: 'Unknown',
37 enabledResolutions: []
55 private videoCategories: Array<{ id: number, label: string }> = []
56 private videoLicences: Array<{ id: number, label: string }> = []
57 private videoLanguages: Array<{ id: number, label: string }> = []
58 private videoPrivacies: Array<{ id: number, label: string }> = []
60 constructor (private http: HttpClient) {
61 this.loadConfigLocally()
65 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
66 .do(this.saveConfigLocally)
70 this.configLoaded.next(true)
74 loadVideoCategories () {
75 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
78 loadVideoLicences () {
79 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
82 loadVideoLanguages () {
83 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
86 loadVideoPrivacies () {
87 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
94 getVideoCategories () {
95 return this.videoCategories
99 return this.videoLicences
102 getVideoLanguages () {
103 return this.videoLanguages
106 getVideoPrivacies () {
107 return this.videoPrivacies
111 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
114 private loadVideoAttributeEnum (
115 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
116 hashToPopulate: { id: number, label: string }[],
117 notifier: ReplaySubject<boolean>,
120 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
123 .forEach(dataKey => {
124 hashToPopulate.push({
125 id: parseInt(dataKey, 10),
131 hashToPopulate.sort((a, b) => {
132 if (a.label < b.label) return -1
133 if (a.label === b.label) return 0
142 private saveConfigLocally (config: ServerConfig) {
143 localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
146 private loadConfigLocally () {
147 const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
151 const parsed = JSON.parse(configString)
152 Object.assign(this.config, parsed)
154 console.error('Cannot parse config saved in local storage.', err)