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',
40 enabledResolutions: []
61 private videoCategories: Array<VideoConstant<number>> = []
62 private videoLicences: Array<VideoConstant<number>> = []
63 private videoLanguages: Array<VideoConstant<string>> = []
64 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
66 constructor (private http: HttpClient) {
67 this.loadConfigLocally()
71 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
72 .pipe(tap(this.saveConfigLocally))
76 this.configLoaded.next(true)
80 loadVideoCategories () {
81 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
84 loadVideoLicences () {
85 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
88 loadVideoLanguages () {
89 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
92 loadVideoPrivacies () {
93 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
100 getVideoCategories () {
101 return this.videoCategories
104 getVideoLicences () {
105 return this.videoLicences
108 getVideoLanguages () {
109 return this.videoLanguages
112 getVideoPrivacies () {
113 return this.videoPrivacies
117 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
120 private loadVideoAttributeEnum (
121 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
122 hashToPopulate: VideoConstant<number | string>[],
123 notifier: ReplaySubject<boolean>,
126 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
129 .forEach(dataKey => {
130 hashToPopulate.push({
137 hashToPopulate.sort((a, b) => {
138 if (a.label < b.label) return -1
139 if (a.label === b.label) return 0
148 private saveConfigLocally (config: ServerConfig) {
149 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
152 private loadConfigLocally () {
153 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
157 const parsed = JSON.parse(configString)
158 Object.assign(this.config, parsed)
160 console.error('Cannot parse config saved in local storage.', err)