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, SearchTargetType, 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.loadConfigLocally()
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.configLoaded = true
82 if (this.configReset) {
83 this.configReloaded.next(config)
84 this.configReset = false
91 return this.configObservable
98 getVideoCategories () {
99 if (!this.videoCategoriesObservable) {
100 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
103 return this.videoCategoriesObservable.pipe(first())
106 getVideoLicences () {
107 if (!this.videoLicensesObservable) {
108 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
111 return this.videoLicensesObservable.pipe(first())
114 getVideoLanguages () {
115 if (!this.videoLanguagesObservable) {
116 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
119 return this.videoLanguagesObservable.pipe(first())
122 getVideoPrivacies () {
123 if (!this.videoPrivaciesObservable) {
124 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
127 return this.videoPrivaciesObservable.pipe(first())
130 getVideoPlaylistPrivacies () {
131 if (!this.videoPlaylistPrivaciesObservable) {
132 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
135 return this.videoPlaylistPrivaciesObservable.pipe(first())
139 if (!this.localeObservable) {
140 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
142 // Default locale, nothing to translate
143 if (isDefaultLocale(completeLocale)) {
144 this.localeObservable = of({}).pipe(shareReplay())
146 this.localeObservable = this.http
147 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
152 return this.localeObservable.pipe(first())
156 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
159 getDefaultSearchTarget (): Promise<SearchTargetType> {
160 return this.getConfig().pipe(
162 const searchIndexConfig = config.search.searchIndex
164 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
165 return 'search-index'
173 private loadAttributeEnum <T extends string | number> (
175 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
178 return this.getServerLocale()
180 switchMap(translations => {
181 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
182 .pipe(map(data => ({ data, translations })))
184 map(({ data, translations }) => {
185 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
187 const label = data[ dataKey ]
189 const id = attributeName === 'languages'
191 : parseInt(dataKey, 10) as T
195 label: peertubeTranslate(label, translations)
199 if (sort === true) sortBy(hashToPopulate, 'label')
201 return hashToPopulate
207 private loadConfigLocally () {
208 const configString = window['PeerTubeServerConfig']
210 throw new Error('Could not find PeerTubeServerConfig in HTML')
213 this.config = JSON.parse(configString)