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 { 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 = {
34 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
35 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
37 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
38 defaultClientRoute: '',
46 registeredExternalAuths: [],
47 registeredIdAndPassAuths: []
59 serverVersion: 'Unknown',
62 allowedForCurrentIP: false,
63 requiresEmailVerification: false
67 availableProfiles: [ 'default' ],
68 enabledResolutions: [],
85 availableProfiles: [ 'default' ],
86 enabledResolutions: []
137 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
138 default: 'most-viewed'
155 indexUrl: 'https://instances.joinpeertube.org'
173 disableLocalSearch: false,
174 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 return this.getConfig()
208 if (this.configLoaded) return of(this.config)
210 if (!this.configObservable) {
211 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
215 this.configLoaded = true
218 if (this.configReset) {
219 this.configReloaded.next(config)
220 this.configReset = false
227 return this.configObservable
234 getVideoCategories () {
235 if (!this.videoCategoriesObservable) {
236 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
239 return this.videoCategoriesObservable.pipe(first())
242 getVideoLicences () {
243 if (!this.videoLicensesObservable) {
244 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
247 return this.videoLicensesObservable.pipe(first())
250 getVideoLanguages () {
251 if (!this.videoLanguagesObservable) {
252 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
255 return this.videoLanguagesObservable.pipe(first())
258 getVideoPrivacies () {
259 if (!this.videoPrivaciesObservable) {
260 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
263 return this.videoPrivaciesObservable.pipe(first())
266 getVideoPlaylistPrivacies () {
267 if (!this.videoPlaylistPrivaciesObservable) {
268 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
271 return this.videoPlaylistPrivaciesObservable.pipe(first())
275 if (!this.localeObservable) {
276 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
278 // Default locale, nothing to translate
279 if (isDefaultLocale(completeLocale)) {
280 this.localeObservable = of({}).pipe(shareReplay())
282 this.localeObservable = this.http
283 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
288 return this.localeObservable.pipe(first())
292 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
295 getDefaultSearchTarget (): Promise<SearchTargetType> {
296 return this.getConfig().pipe(
298 const searchIndexConfig = config.search.searchIndex
300 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
301 return 'search-index'
309 private loadAttributeEnum <T extends string | number> (
311 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
314 return this.getServerLocale()
316 switchMap(translations => {
317 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
318 .pipe(map(data => ({ data, translations })))
320 map(({ data, translations }) => {
321 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
323 const label = data[ dataKey ]
325 const id = attributeName === 'languages'
327 : parseInt(dataKey, 10) as T
331 label: peertubeTranslate(label, translations)
335 if (sort === true) sortBy(hashToPopulate, 'label')
337 return hashToPopulate
343 private loadConfigLocally () {
344 const configString = window['PeerTubeServerConfig']
345 if (!configString) return
348 const parsed = JSON.parse(configString)
349 Object.assign(this.config, parsed)
351 console.error('Cannot parse config saved in from index.html.', err)