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: []
134 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
135 default: 'most-viewed'
152 indexUrl: 'https://instances.joinpeertube.org'
170 disableLocalSearch: false,
171 isDefaultSearch: false
177 private http: HttpClient,
178 @Inject(LOCALE_ID) private localeId: string
180 this.loadConfigLocally()
183 getServerVersionAndCommit () {
184 const serverVersion = this.config.serverVersion
185 const commit = this.config.serverCommit || ''
187 let result = serverVersion
188 if (commit) result += '...' + commit
194 this.configLoaded = false
195 this.configReset = true
197 // Notify config update
198 this.getConfig().subscribe(() => {
199 // empty, to fire a reset config event
204 if (this.configLoaded) return of(this.config)
206 if (!this.configObservable) {
207 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
209 tap(config => this.saveConfigLocally(config)),
212 this.configLoaded = true
215 if (this.configReset) {
216 this.configReloaded.next(config)
217 this.configReset = false
224 return this.configObservable
231 getVideoCategories () {
232 if (!this.videoCategoriesObservable) {
233 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
236 return this.videoCategoriesObservable.pipe(first())
239 getVideoLicences () {
240 if (!this.videoLicensesObservable) {
241 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
244 return this.videoLicensesObservable.pipe(first())
247 getVideoLanguages () {
248 if (!this.videoLanguagesObservable) {
249 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
252 return this.videoLanguagesObservable.pipe(first())
255 getVideoPrivacies () {
256 if (!this.videoPrivaciesObservable) {
257 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
260 return this.videoPrivaciesObservable.pipe(first())
263 getVideoPlaylistPrivacies () {
264 if (!this.videoPlaylistPrivaciesObservable) {
265 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
268 return this.videoPlaylistPrivaciesObservable.pipe(first())
272 if (!this.localeObservable) {
273 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
275 // Default locale, nothing to translate
276 if (isDefaultLocale(completeLocale)) {
277 this.localeObservable = of({}).pipe(shareReplay())
279 this.localeObservable = this.http
280 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
285 return this.localeObservable.pipe(first())
289 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
292 getDefaultSearchTarget (): Promise<SearchTargetType> {
293 return this.getConfig().pipe(
295 const searchIndexConfig = config.search.searchIndex
297 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
298 return 'search-index'
306 private loadAttributeEnum <T extends string | number> (
308 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
311 return this.getServerLocale()
313 switchMap(translations => {
314 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
315 .pipe(map(data => ({ data, translations })))
317 map(({ data, translations }) => {
318 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
320 const label = data[ dataKey ]
322 const id = attributeName === 'languages'
324 : parseInt(dataKey, 10) as T
328 label: peertubeTranslate(label, translations)
332 if (sort === true) sortBy(hashToPopulate, 'label')
334 return hashToPopulate
340 private saveConfigLocally (config: ServerConfig) {
341 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
344 private loadConfigLocally () {
345 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
349 const parsed = JSON.parse(configString)
350 Object.assign(this.config, parsed)
352 console.error('Cannot parse config saved in local storage.', err)