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 { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
7 import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
8 import { SearchTargetType, 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 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
21 configReloaded = new Subject<ServerConfig>()
23 private localeObservable: Observable<any>
24 private videoLicensesObservable: Observable<VideoConstant<number>[]>
25 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
26 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
27 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
28 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
29 private configObservable: Observable<ServerConfig>
31 private configReset = false
33 private configLoaded = false
34 private config: ServerConfig = {
37 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
38 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
39 defaultClientRoute: '',
41 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
49 registeredExternalAuths: [],
50 registeredIdAndPassAuths: []
62 serverVersion: 'Unknown',
65 allowedForCurrentIP: false,
66 requiresEmailVerification: false
69 enabledResolutions: [],
85 enabledResolutions: []
141 indexUrl: 'https://instances.joinpeertube.org'
159 disableLocalSearch: false,
160 isDefaultSearch: false
166 private http: HttpClient,
167 @Inject(LOCALE_ID) private localeId: string
169 this.loadConfigLocally()
172 getServerVersionAndCommit () {
173 const serverVersion = this.config.serverVersion
174 const commit = this.config.serverCommit || ''
176 let result = serverVersion
177 if (commit) result += '...' + commit
183 this.configLoaded = false
184 this.configReset = true
186 // Notify config update
187 this.getConfig().subscribe(() => {
188 // empty, to fire a reset config event
193 if (this.configLoaded) return of(this.config)
195 if (!this.configObservable) {
196 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
198 tap(config => this.saveConfigLocally(config)),
201 this.configLoaded = true
204 if (this.configReset) {
205 this.configReloaded.next(config)
206 this.configReset = false
213 return this.configObservable
220 getVideoCategories () {
221 if (!this.videoCategoriesObservable) {
222 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
225 return this.videoCategoriesObservable.pipe(first())
228 getVideoLicences () {
229 if (!this.videoLicensesObservable) {
230 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
233 return this.videoLicensesObservable.pipe(first())
236 getVideoLanguages () {
237 if (!this.videoLanguagesObservable) {
238 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
241 return this.videoLanguagesObservable.pipe(first())
244 getVideoPrivacies () {
245 if (!this.videoPrivaciesObservable) {
246 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
249 return this.videoPrivaciesObservable.pipe(first())
252 getVideoPlaylistPrivacies () {
253 if (!this.videoPlaylistPrivaciesObservable) {
254 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
257 return this.videoPlaylistPrivaciesObservable.pipe(first())
261 if (!this.localeObservable) {
262 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
264 // Default locale, nothing to translate
265 if (isDefaultLocale(completeLocale)) {
266 this.localeObservable = of({}).pipe(shareReplay())
268 this.localeObservable = this.http
269 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
274 return this.localeObservable.pipe(first())
278 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
281 getDefaultSearchTarget (): Promise<SearchTargetType> {
282 return this.getConfig().pipe(
284 const searchIndexConfig = config.search.searchIndex
286 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
287 return 'search-index'
295 private loadAttributeEnum <T extends string | number> (
297 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
300 return this.getServerLocale()
302 switchMap(translations => {
303 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
304 .pipe(map(data => ({ data, translations })))
306 map(({ data, translations }) => {
307 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
309 const label = data[ dataKey ]
311 const id = attributeName === 'languages'
313 : parseInt(dataKey, 10) as T
317 label: peertubeTranslate(label, translations)
321 if (sort === true) sortBy(hashToPopulate, 'label')
323 return hashToPopulate
329 private saveConfigLocally (config: ServerConfig) {
330 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
333 private loadConfigLocally () {
334 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
338 const parsed = JSON.parse(configString)
339 Object.assign(this.config, parsed)
341 console.error('Cannot parse config saved in local storage.', err)