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
180 private http: HttpClient,
181 @Inject(LOCALE_ID) private localeId: string
183 this.loadConfigLocally()
186 getServerVersionAndCommit () {
187 const serverVersion = this.config.serverVersion
188 const commit = this.config.serverCommit || ''
190 let result = serverVersion
191 if (commit) result += '...' + commit
197 this.configLoaded = false
198 this.configReset = true
200 // Notify config update
201 this.getConfig().subscribe(() => {
202 // empty, to fire a reset config event
207 if (this.configLoaded) return of(this.config)
209 if (!this.configObservable) {
210 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
214 this.configLoaded = true
217 if (this.configReset) {
218 this.configReloaded.next(config)
219 this.configReset = false
226 return this.configObservable
233 getVideoCategories () {
234 if (!this.videoCategoriesObservable) {
235 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
238 return this.videoCategoriesObservable.pipe(first())
241 getVideoLicences () {
242 if (!this.videoLicensesObservable) {
243 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
246 return this.videoLicensesObservable.pipe(first())
249 getVideoLanguages () {
250 if (!this.videoLanguagesObservable) {
251 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
254 return this.videoLanguagesObservable.pipe(first())
257 getVideoPrivacies () {
258 if (!this.videoPrivaciesObservable) {
259 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
262 return this.videoPrivaciesObservable.pipe(first())
265 getVideoPlaylistPrivacies () {
266 if (!this.videoPlaylistPrivaciesObservable) {
267 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
270 return this.videoPlaylistPrivaciesObservable.pipe(first())
274 if (!this.localeObservable) {
275 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
277 // Default locale, nothing to translate
278 if (isDefaultLocale(completeLocale)) {
279 this.localeObservable = of({}).pipe(shareReplay())
281 this.localeObservable = this.http
282 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
287 return this.localeObservable.pipe(first())
291 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
294 getDefaultSearchTarget (): Promise<SearchTargetType> {
295 return this.getConfig().pipe(
297 const searchIndexConfig = config.search.searchIndex
299 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
300 return 'search-index'
308 private loadAttributeEnum <T extends string | number> (
310 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
313 return this.getServerLocale()
315 switchMap(translations => {
316 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
317 .pipe(map(data => ({ data, translations })))
319 map(({ data, translations }) => {
320 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
322 const label = data[ dataKey ]
324 const id = attributeName === 'languages'
326 : parseInt(dataKey, 10) as T
330 label: peertubeTranslate(label, translations)
334 if (sort === true) sortBy(hashToPopulate, 'label')
336 return hashToPopulate
342 private loadConfigLocally () {
343 const configString = window['PeerTubeServerConfig']
344 if (!configString) return
347 const parsed = JSON.parse(configString)
348 Object.assign(this.config, parsed)
350 console.error('Cannot parse config saved in from index.html.', err)