1 import { first, map, share, 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-web-storage'
5 import { Observable, of, Subject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { environment } from '../../../environments/environment'
8 import { VideoConstant } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12 import { ServerStats } from '@shared/models/server'
15 export class ServerService {
16 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
17 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
18 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
19 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
20 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
24 configReloaded = new Subject<void>()
26 private localeObservable: Observable<any>
27 private videoLicensesObservable: Observable<VideoConstant<number>[]>
28 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
29 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
30 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
31 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
32 private configObservable: Observable<ServerConfig>
34 private configReset = false
36 private configLoaded = false
37 private config: ServerConfig = {
40 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
41 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
42 defaultClientRoute: '',
44 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
58 registeredExternalAuths: [],
59 registeredIdAndPassAuths: []
71 serverVersion: 'Unknown',
74 allowedForCurrentIP: false,
75 requiresEmailVerification: false
78 enabledResolutions: [],
139 indexUrl: 'https://instances.joinpeertube.org'
146 private http: HttpClient,
147 @Inject(LOCALE_ID) private localeId: string
149 this.loadConfigLocally()
152 getServerVersionAndCommit () {
153 const serverVersion = this.config.serverVersion
154 const commit = this.config.serverCommit || ''
156 let result = serverVersion
157 if (commit) result += '...' + commit
163 this.configLoaded = false
164 this.configReset = true
168 if (this.configLoaded) return of(this.config)
170 if (!this.configObservable) {
171 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
173 tap(config => this.saveConfigLocally(config)),
176 this.configLoaded = true
179 if (this.configReset) {
180 this.configReloaded.next()
181 this.configReset = false
188 return this.configObservable
195 getVideoCategories () {
196 if (!this.videoCategoriesObservable) {
197 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
200 return this.videoCategoriesObservable.pipe(first())
203 getVideoLicences () {
204 if (!this.videoLicensesObservable) {
205 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
208 return this.videoLicensesObservable.pipe(first())
211 getVideoLanguages () {
212 if (!this.videoLanguagesObservable) {
213 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
216 return this.videoLanguagesObservable.pipe(first())
219 getVideoPrivacies () {
220 if (!this.videoPrivaciesObservable) {
221 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
224 return this.videoPrivaciesObservable.pipe(first())
227 getVideoPlaylistPrivacies () {
228 if (!this.videoPlaylistPrivaciesObservable) {
229 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
232 return this.videoPlaylistPrivaciesObservable.pipe(first())
236 if (!this.localeObservable) {
237 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
239 // Default locale, nothing to translate
240 if (isDefaultLocale(completeLocale)) {
241 this.localeObservable = of({}).pipe(shareReplay())
243 this.localeObservable = this.http
244 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
249 return this.localeObservable.pipe(first())
253 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
256 private loadAttributeEnum <T extends string | number> (
258 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
261 return this.getServerLocale()
263 switchMap(translations => {
264 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
265 .pipe(map(data => ({ data, translations })))
267 map(({ data, translations }) => {
268 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
270 const label = data[ dataKey ]
272 const id = attributeName === 'languages'
274 : parseInt(dataKey, 10) as T
278 label: peertubeTranslate(label, translations)
282 if (sort === true) sortBy(hashToPopulate, 'label')
284 return hashToPopulate
290 private saveConfigLocally (config: ServerConfig) {
291 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
294 private loadConfigLocally () {
295 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
299 const parsed = JSON.parse(configString)
300 Object.assign(this.config, parsed)
302 console.error('Cannot parse config saved in local storage.', err)