1 import { HttpClient } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
4 import 'rxjs/add/operator/do'
5 import { ReplaySubject } from 'rxjs/ReplaySubject'
6 import { ServerConfig } from '../../../../../shared'
7 import { About } from '../../../../../shared/models/server/about.model'
8 import { ServerStats } from '../../../../../shared/models/server/server-stats.model'
9 import { environment } from '../../../environments/environment'
12 export class ServerService {
13 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
14 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
15 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
17 configLoaded = new ReplaySubject<boolean>(1)
18 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
19 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
20 videoLicencesLoaded = new ReplaySubject<boolean>(1)
21 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
23 private config: ServerConfig = {
26 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
27 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
28 defaultClientRoute: '',
34 serverVersion: 'Unknown',
39 enabledResolutions: []
60 private videoCategories: Array<{ id: number, label: string }> = []
61 private videoLicences: Array<{ id: number, label: string }> = []
62 private videoLanguages: Array<{ id: number, label: string }> = []
63 private videoPrivacies: Array<{ id: number, label: string }> = []
65 constructor (private http: HttpClient) {
66 this.loadConfigLocally()
70 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
71 .do(this.saveConfigLocally)
75 this.configLoaded.next(true)
79 loadVideoCategories () {
80 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
83 loadVideoLicences () {
84 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
87 loadVideoLanguages () {
88 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
91 loadVideoPrivacies () {
92 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
99 getVideoCategories () {
100 return this.videoCategories
103 getVideoLicences () {
104 return this.videoLicences
107 getVideoLanguages () {
108 return this.videoLanguages
111 getVideoPrivacies () {
112 return this.videoPrivacies
116 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
119 private loadVideoAttributeEnum (
120 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
121 hashToPopulate: { id: number, label: string }[],
122 notifier: ReplaySubject<boolean>,
125 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
128 .forEach(dataKey => {
129 hashToPopulate.push({
130 id: parseInt(dataKey, 10),
136 hashToPopulate.sort((a, b) => {
137 if (a.label < b.label) return -1
138 if (a.label === b.label) return 0
147 private saveConfigLocally (config: ServerConfig) {
148 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
151 private loadConfigLocally () {
152 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
156 const parsed = JSON.parse(configString)
157 Object.assign(this.config, parsed)
159 console.error('Cannot parse config saved in local storage.', err)