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 { environment } from '../../../environments/environment'
11 export class ServerService {
12 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
13 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
14 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
16 configLoaded = new ReplaySubject<boolean>(1)
17 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
18 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
19 videoLicencesLoaded = new ReplaySubject<boolean>(1)
20 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
22 private config: ServerConfig = {
25 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
26 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
27 defaultClientRoute: '',
33 serverVersion: 'Unknown',
38 enabledResolutions: []
56 private videoCategories: Array<{ id: number, label: string }> = []
57 private videoLicences: Array<{ id: number, label: string }> = []
58 private videoLanguages: Array<{ id: number, label: string }> = []
59 private videoPrivacies: Array<{ id: number, label: string }> = []
61 constructor (private http: HttpClient) {
62 this.loadConfigLocally()
66 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
67 .do(this.saveConfigLocally)
71 this.configLoaded.next(true)
75 loadVideoCategories () {
76 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
79 loadVideoLicences () {
80 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
83 loadVideoLanguages () {
84 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
87 loadVideoPrivacies () {
88 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
95 getVideoCategories () {
96 return this.videoCategories
100 return this.videoLicences
103 getVideoLanguages () {
104 return this.videoLanguages
107 getVideoPrivacies () {
108 return this.videoPrivacies
112 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
115 private loadVideoAttributeEnum (
116 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
117 hashToPopulate: { id: number, label: string }[],
118 notifier: ReplaySubject<boolean>,
121 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
124 .forEach(dataKey => {
125 hashToPopulate.push({
126 id: parseInt(dataKey, 10),
132 hashToPopulate.sort((a, b) => {
133 if (a.label < b.label) return -1
134 if (a.label === b.label) return 0
143 private saveConfigLocally (config: ServerConfig) {
144 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
147 private loadConfigLocally () {
148 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
152 const parsed = JSON.parse(configString)
153 Object.assign(this.config, parsed)
155 console.error('Cannot parse config saved in local storage.', err)