1 import { map, share, switchMap, tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
5 import { Observable, ReplaySubject, of } from 'rxjs'
6 import { getCompleteLocale, 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'
10 import { isDefaultLocale } from '../../../../../shared/models/i18n'
11 import { getDevLocale, isOnDevLocale, peertubeTranslate } from '@app/shared/i18n/i18n-utils'
14 export class ServerService {
15 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
16 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
17 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
18 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
20 configLoaded = new ReplaySubject<boolean>(1)
21 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
22 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
23 videoLicencesLoaded = new ReplaySubject<boolean>(1)
24 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
25 localeObservable: Observable<any>
27 private config: ServerConfig = {
30 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
31 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
32 defaultClientRoute: '',
33 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
39 serverVersion: 'Unknown',
42 allowedForCurrentIP: false
45 enabledResolutions: []
66 private videoCategories: Array<VideoConstant<number>> = []
67 private videoLicences: Array<VideoConstant<number>> = []
68 private videoLanguages: Array<VideoConstant<string>> = []
69 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
72 private http: HttpClient,
73 @Inject(LOCALE_ID) private localeId: string
75 this.loadServerLocale()
76 this.loadConfigLocally()
80 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
81 .pipe(tap(this.saveConfigLocally))
85 this.configLoaded.next(true)
89 loadVideoCategories () {
90 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
93 loadVideoLicences () {
94 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
97 loadVideoLanguages () {
98 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
101 loadVideoPrivacies () {
102 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
109 getVideoCategories () {
110 return this.videoCategories
113 getVideoLicences () {
114 return this.videoLicences
117 getVideoLanguages () {
118 return this.videoLanguages
121 getVideoPrivacies () {
122 return this.videoPrivacies
126 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
129 private loadVideoAttributeEnum (
130 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
131 hashToPopulate: VideoConstant<number | string>[],
132 notifier: ReplaySubject<boolean>,
135 this.localeObservable
137 switchMap(translations => {
138 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
139 .pipe(map(data => ({ data, translations })))
142 .subscribe(({ data, translations }) => {
144 .map(dataKey => parseInt(dataKey, 10))
145 .forEach(dataKey => {
146 const label = data[ dataKey ]
148 hashToPopulate.push({
150 label: peertubeTranslate(label, translations)
155 hashToPopulate.sort((a, b) => {
156 if (a.label < b.label) return -1
157 if (a.label === b.label) return 0
166 private loadServerLocale () {
167 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
169 // Default locale, nothing to translate
170 if (isDefaultLocale(completeLocale)) {
171 this.localeObservable = of({}).pipe(share())
175 this.localeObservable = this.http
176 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
180 private saveConfigLocally (config: ServerConfig) {
181 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
184 private loadConfigLocally () {
185 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
189 const parsed = JSON.parse(configString)
190 Object.assign(this.config, parsed)
192 console.error('Cannot parse config saved in local storage.', err)