1 import { tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
5 import { ReplaySubject } from 'rxjs'
6 import { ServerConfig } from '../../../../../shared'
7 import { About } from '../../../../../shared/models/server/about.model'
8 import { environment } from '../../../environments/environment'
9 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
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: '',
29 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
35 serverVersion: 'Unknown',
38 allowedForCurrentIP: false
41 enabledResolutions: []
62 private videoCategories: Array<VideoConstant<number>> = []
63 private videoLicences: Array<VideoConstant<number>> = []
64 private videoLanguages: Array<VideoConstant<string>> = []
65 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
67 constructor (private http: HttpClient) {
68 this.loadConfigLocally()
72 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
73 .pipe(tap(this.saveConfigLocally))
77 this.configLoaded.next(true)
81 loadVideoCategories () {
82 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
85 loadVideoLicences () {
86 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
89 loadVideoLanguages () {
90 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
93 loadVideoPrivacies () {
94 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
101 getVideoCategories () {
102 return this.videoCategories
105 getVideoLicences () {
106 return this.videoLicences
109 getVideoLanguages () {
110 return this.videoLanguages
113 getVideoPrivacies () {
114 return this.videoPrivacies
118 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
121 private loadVideoAttributeEnum (
122 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
123 hashToPopulate: VideoConstant<number | string>[],
124 notifier: ReplaySubject<boolean>,
127 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
130 .forEach(dataKey => {
131 hashToPopulate.push({
138 hashToPopulate.sort((a, b) => {
139 if (a.label < b.label) return -1
140 if (a.label === b.label) return 0
149 private saveConfigLocally (config: ServerConfig) {
150 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
153 private loadConfigLocally () {
154 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
158 const parsed = JSON.parse(configString)
159 Object.assign(this.config, parsed)
161 console.error('Cannot parse config saved in local storage.', err)