1 import { Observable, of, Subject } from 'rxjs'
2 import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
3 import { HttpClient } from '@angular/common/http'
4 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
5 import { getDevLocale, isOnDevLocale, sortBy } from '@app/helpers'
6 import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
7 import { HTMLServerConfig, ServerConfig, ServerStats, VideoConstant } from '@shared/models'
8 import { environment } from '../../../environments/environment'
11 export class ServerService {
12 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
13 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
14 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
15 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
16 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
18 configReloaded = new Subject<ServerConfig>()
20 private localeObservable: Observable<any>
21 private videoLicensesObservable: Observable<VideoConstant<number>[]>
22 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
23 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
24 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
25 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
26 private configObservable: Observable<ServerConfig>
28 private configReset = false
30 private configLoaded = false
31 private config: ServerConfig
32 private htmlConfig: HTMLServerConfig
35 private http: HttpClient,
36 @Inject(LOCALE_ID) private localeId: string
42 return this.loadHTMLConfigLocally()
44 // Expected in dev mode since we can't inject the config in the HTML
45 if (environment.production !== false) {
46 console.error('Cannot load config locally. Fallback to API.')
49 return this.getConfig()
53 getServerVersionAndCommit () {
54 const serverVersion = this.config.serverVersion
55 const commit = this.config.serverCommit || ''
57 let result = serverVersion
58 if (commit) result += '...' + commit
64 this.configLoaded = false
65 this.configReset = true
67 // Notify config update
68 return this.getConfig()
72 if (this.configLoaded) return of(this.config)
74 if (!this.configObservable) {
75 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
79 this.htmlConfig = config
80 this.configLoaded = true
83 if (this.configReset) {
84 this.configReloaded.next(config)
85 this.configReset = false
92 return this.configObservable
96 return this.htmlConfig
99 getVideoCategories () {
100 if (!this.videoCategoriesObservable) {
101 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
104 return this.videoCategoriesObservable.pipe(first())
107 getVideoLicences () {
108 if (!this.videoLicensesObservable) {
109 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
112 return this.videoLicensesObservable.pipe(first())
115 getVideoLanguages () {
116 if (!this.videoLanguagesObservable) {
117 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
120 return this.videoLanguagesObservable.pipe(first())
123 getVideoPrivacies () {
124 if (!this.videoPrivaciesObservable) {
125 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
128 return this.videoPrivaciesObservable.pipe(first())
131 getVideoPlaylistPrivacies () {
132 if (!this.videoPlaylistPrivaciesObservable) {
133 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
136 return this.videoPlaylistPrivaciesObservable.pipe(first())
140 if (!this.localeObservable) {
141 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
143 // Default locale, nothing to translate
144 if (isDefaultLocale(completeLocale)) {
145 this.localeObservable = of({}).pipe(shareReplay())
147 this.localeObservable = this.http
148 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
153 return this.localeObservable.pipe(first())
157 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
160 private loadAttributeEnum <T extends string | number> (
162 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
165 return this.getServerLocale()
167 switchMap(translations => {
168 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
169 .pipe(map(data => ({ data, translations })))
171 map(({ data, translations }) => {
172 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
174 const label = data[dataKey]
176 const id = attributeName === 'languages'
178 : parseInt(dataKey, 10) as T
182 label: peertubeTranslate(label, translations)
186 if (sort === true) sortBy(hashToPopulate, 'label')
188 return hashToPopulate
194 private loadHTMLConfigLocally () {
195 const configString = window['PeerTubeServerConfig']
197 throw new Error('Could not find PeerTubeServerConfig in HTML')
200 this.htmlConfig = JSON.parse(configString)