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 { logger } from '@root-helpers/logger'
7 import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
8 import { HTMLServerConfig, ServerConfig, ServerStats, VideoConstant } from '@shared/models'
9 import { environment } from '../../../environments/environment'
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 BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
16 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
17 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
19 configReloaded = new Subject<ServerConfig>()
21 private localeObservable: Observable<any>
22 private videoLicensesObservable: Observable<VideoConstant<number>[]>
23 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
24 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
25 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
26 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
27 private configObservable: Observable<ServerConfig>
29 private configReset = false
31 private configLoaded = false
32 private config: ServerConfig
33 private htmlConfig: HTMLServerConfig
36 private http: HttpClient,
37 @Inject(LOCALE_ID) private localeId: string
43 return this.loadHTMLConfigLocally()
45 // Expected in dev mode since we can't inject the config in the HTML
46 if (environment.production !== false) {
47 logger.error('Cannot load config locally. Fallback to API.')
50 return this.getConfig()
54 getServerVersionAndCommit () {
55 const serverVersion = this.config.serverVersion
56 const commit = this.config.serverCommit || ''
58 let result = serverVersion
59 if (commit) result += '...' + commit
65 this.configLoaded = false
66 this.configReset = true
68 // Notify config update
69 return this.getConfig()
73 if (this.configLoaded) return of(this.config)
75 if (!this.configObservable) {
76 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
80 this.htmlConfig = config
81 this.configLoaded = true
84 if (this.configReset) {
85 this.configReloaded.next(config)
86 this.configReset = false
93 return this.configObservable
97 return this.htmlConfig
100 getVideoCategories () {
101 if (!this.videoCategoriesObservable) {
102 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
105 return this.videoCategoriesObservable.pipe(first())
108 getVideoLicences () {
109 if (!this.videoLicensesObservable) {
110 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
113 return this.videoLicensesObservable.pipe(first())
116 getVideoLanguages () {
117 if (!this.videoLanguagesObservable) {
118 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
121 return this.videoLanguagesObservable.pipe(first())
124 getVideoPrivacies () {
125 if (!this.videoPrivaciesObservable) {
126 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
129 return this.videoPrivaciesObservable.pipe(first())
132 getVideoPlaylistPrivacies () {
133 if (!this.videoPlaylistPrivaciesObservable) {
134 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
137 return this.videoPlaylistPrivaciesObservable.pipe(first())
141 if (!this.localeObservable) {
142 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
144 // Default locale, nothing to translate
145 if (isDefaultLocale(completeLocale)) {
146 this.localeObservable = of({}).pipe(shareReplay())
148 this.localeObservable = this.http
149 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
154 return this.localeObservable.pipe(first())
158 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
161 private loadAttributeEnum <T extends string | number> (
163 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
166 return this.getServerLocale()
168 switchMap(translations => {
169 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
170 .pipe(map(data => ({ data, translations })))
172 map(({ data, translations }) => {
173 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
175 const label = data[dataKey]
177 const id = attributeName === 'languages'
179 : parseInt(dataKey, 10) as T
183 label: peertubeTranslate(label, translations)
187 if (sort === true) sortBy(hashToPopulate, 'label')
189 return hashToPopulate
195 private loadHTMLConfigLocally () {
196 const configString = window['PeerTubeServerConfig']
198 throw new Error('Could not find PeerTubeServerConfig in HTML')
201 this.htmlConfig = JSON.parse(configString)