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.',
40 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
41 defaultClientRoute: '',
49 registeredExternalAuths: [],
50 registeredIdAndPassAuths: []
62 serverVersion: 'Unknown',
65 allowedForCurrentIP: false,
66 requiresEmailVerification: false
70 availableProfiles: [ 'default' ],
71 enabledResolutions: [],
88 availableProfiles: [ 'default' ],
89 enabledResolutions: []
140 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
141 default: 'most-viewed'
158 indexUrl: 'https://instances.joinpeertube.org'
176 disableLocalSearch: false,
177 isDefaultSearch: false
183 private http: HttpClient,
184 @Inject(LOCALE_ID) private localeId: string
186 this.loadConfigLocally()
189 getServerVersionAndCommit () {
190 const serverVersion = this.config.serverVersion
191 const commit = this.config.serverCommit || ''
193 let result = serverVersion
194 if (commit) result += '...' + commit
200 this.configLoaded = false
201 this.configReset = true
203 // Notify config update
204 this.getConfig().subscribe(() => {
205 // empty, to fire a reset config event
210 if (this.configLoaded) return of(this.config)
212 if (!this.configObservable) {
213 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
215 tap(config => this.saveConfigLocally(config)),
218 this.configLoaded = true
221 if (this.configReset) {
222 this.configReloaded.next(config)
223 this.configReset = false
230 return this.configObservable
237 getVideoCategories () {
238 if (!this.videoCategoriesObservable) {
239 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
242 return this.videoCategoriesObservable.pipe(first())
245 getVideoLicences () {
246 if (!this.videoLicensesObservable) {
247 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
250 return this.videoLicensesObservable.pipe(first())
253 getVideoLanguages () {
254 if (!this.videoLanguagesObservable) {
255 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
258 return this.videoLanguagesObservable.pipe(first())
261 getVideoPrivacies () {
262 if (!this.videoPrivaciesObservable) {
263 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
266 return this.videoPrivaciesObservable.pipe(first())
269 getVideoPlaylistPrivacies () {
270 if (!this.videoPlaylistPrivaciesObservable) {
271 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
274 return this.videoPlaylistPrivaciesObservable.pipe(first())
278 if (!this.localeObservable) {
279 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
281 // Default locale, nothing to translate
282 if (isDefaultLocale(completeLocale)) {
283 this.localeObservable = of({}).pipe(shareReplay())
285 this.localeObservable = this.http
286 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
291 return this.localeObservable.pipe(first())
295 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
298 getDefaultSearchTarget (): Promise<SearchTargetType> {
299 return this.getConfig().pipe(
301 const searchIndexConfig = config.search.searchIndex
303 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
304 return 'search-index'
312 private loadAttributeEnum <T extends string | number> (
314 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
317 return this.getServerLocale()
319 switchMap(translations => {
320 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
321 .pipe(map(data => ({ data, translations })))
323 map(({ data, translations }) => {
324 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
326 const label = data[ dataKey ]
328 const id = attributeName === 'languages'
330 : parseInt(dataKey, 10) as T
334 label: peertubeTranslate(label, translations)
338 if (sort === true) sortBy(hashToPopulate, 'label')
340 return hashToPopulate
346 private saveConfigLocally (config: ServerConfig) {
347 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
350 private loadConfigLocally () {
351 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
355 const parsed = JSON.parse(configString)
356 Object.assign(this.config, parsed)
358 console.error('Cannot parse config saved in local storage.', err)