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,
68 availableProfiles: [ 'default' ],
69 enabledResolutions: [],
86 availableProfiles: [ 'default' ],
87 enabledResolutions: []
138 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
139 default: 'most-viewed'
156 indexUrl: 'https://instances.joinpeertube.org'
174 disableLocalSearch: false,
175 isDefaultSearch: false
184 private http: HttpClient,
185 @Inject(LOCALE_ID) private localeId: string
187 this.loadConfigLocally()
190 getServerVersionAndCommit () {
191 const serverVersion = this.config.serverVersion
192 const commit = this.config.serverCommit || ''
194 let result = serverVersion
195 if (commit) result += '...' + commit
201 this.configLoaded = false
202 this.configReset = true
204 // Notify config update
205 return this.getConfig()
209 if (this.configLoaded) return of(this.config)
211 if (!this.configObservable) {
212 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
216 this.configLoaded = true
219 if (this.configReset) {
220 this.configReloaded.next(config)
221 this.configReset = false
228 return this.configObservable
235 getVideoCategories () {
236 if (!this.videoCategoriesObservable) {
237 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
240 return this.videoCategoriesObservable.pipe(first())
243 getVideoLicences () {
244 if (!this.videoLicensesObservable) {
245 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
248 return this.videoLicensesObservable.pipe(first())
251 getVideoLanguages () {
252 if (!this.videoLanguagesObservable) {
253 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
256 return this.videoLanguagesObservable.pipe(first())
259 getVideoPrivacies () {
260 if (!this.videoPrivaciesObservable) {
261 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
264 return this.videoPrivaciesObservable.pipe(first())
267 getVideoPlaylistPrivacies () {
268 if (!this.videoPlaylistPrivaciesObservable) {
269 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
272 return this.videoPlaylistPrivaciesObservable.pipe(first())
276 if (!this.localeObservable) {
277 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
279 // Default locale, nothing to translate
280 if (isDefaultLocale(completeLocale)) {
281 this.localeObservable = of({}).pipe(shareReplay())
283 this.localeObservable = this.http
284 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
289 return this.localeObservable.pipe(first())
293 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
296 getDefaultSearchTarget (): Promise<SearchTargetType> {
297 return this.getConfig().pipe(
299 const searchIndexConfig = config.search.searchIndex
301 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
302 return 'search-index'
310 private loadAttributeEnum <T extends string | number> (
312 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
315 return this.getServerLocale()
317 switchMap(translations => {
318 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
319 .pipe(map(data => ({ data, translations })))
321 map(({ data, translations }) => {
322 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
324 const label = data[ dataKey ]
326 const id = attributeName === 'languages'
328 : parseInt(dataKey, 10) as T
332 label: peertubeTranslate(label, translations)
336 if (sort === true) sortBy(hashToPopulate, 'label')
338 return hashToPopulate
344 private loadConfigLocally () {
345 const configString = window['PeerTubeServerConfig']
346 if (!configString) return
349 const parsed = JSON.parse(configString)
350 Object.assign(this.config, parsed)
352 console.error('Cannot parse config saved in from index.html.', err)