1 import { map, shareReplay, 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, of, ReplaySubject } 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, peertubeTranslate } from '../../../../../shared/models/i18n'
11 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
12 import { sortBy } from '@app/shared/misc/utils'
15 export class ServerService {
16 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
17 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
18 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
19 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
20 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
22 configLoaded = new ReplaySubject<boolean>(1)
23 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
24 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
25 videoLicencesLoaded = new ReplaySubject<boolean>(1)
26 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
27 localeObservable: Observable<any>
29 private config: ServerConfig = {
32 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
33 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
34 defaultClientRoute: '',
35 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
47 serverVersion: 'Unknown',
50 allowedForCurrentIP: false,
51 requiresEmailVerification: false
54 enabledResolutions: [],
100 private videoCategories: Array<VideoConstant<number>> = []
101 private videoLicences: Array<VideoConstant<number>> = []
102 private videoLanguages: Array<VideoConstant<string>> = []
103 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
106 private http: HttpClient,
107 @Inject(LOCALE_ID) private localeId: string
109 this.loadServerLocale()
110 this.loadConfigLocally()
114 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
115 .pipe(tap(this.saveConfigLocally))
119 this.configLoaded.next(true)
123 loadVideoCategories () {
124 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
127 loadVideoLicences () {
128 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
131 loadVideoLanguages () {
132 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
135 loadVideoPrivacies () {
136 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
143 getVideoCategories () {
144 return this.videoCategories
147 getVideoLicences () {
148 return this.videoLicences
151 getVideoLanguages () {
152 return this.videoLanguages
155 getVideoPrivacies () {
156 return this.videoPrivacies
159 private loadVideoAttributeEnum (
160 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
161 hashToPopulate: VideoConstant<string | number>[],
162 notifier: ReplaySubject<boolean>,
165 this.localeObservable
167 switchMap(translations => {
168 return this.http.get<{ [id: string]: string }>(ServerService.BASE_VIDEO_URL + attributeName)
169 .pipe(map(data => ({ data, translations })))
172 .subscribe(({ data, translations }) => {
174 .forEach(dataKey => {
175 const label = data[ dataKey ]
177 hashToPopulate.push({
178 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
179 label: peertubeTranslate(label, translations)
183 if (sort === true) sortBy(hashToPopulate, 'label')
189 private loadServerLocale () {
190 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
192 // Default locale, nothing to translate
193 if (isDefaultLocale(completeLocale)) {
194 this.localeObservable = of({}).pipe(shareReplay())
198 this.localeObservable = this.http
199 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
203 private saveConfigLocally (config: ServerConfig) {
204 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
207 private loadConfigLocally () {
208 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
212 const parsed = JSON.parse(configString)
213 Object.assign(this.config, parsed)
215 console.error('Cannot parse config saved in local storage.', err)