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: '',
42 defaultTrendingRoute: '',
55 registeredExternalAuths: [],
56 registeredIdAndPassAuths: []
68 serverVersion: 'Unknown',
71 allowedForCurrentIP: false,
72 requiresEmailVerification: false
75 enabledResolutions: [],
91 enabledResolutions: []
150 indexUrl: 'https://instances.joinpeertube.org'
168 disableLocalSearch: false,
169 isDefaultSearch: false
175 private http: HttpClient,
176 @Inject(LOCALE_ID) private localeId: string
178 this.loadConfigLocally()
181 getServerVersionAndCommit () {
182 const serverVersion = this.config.serverVersion
183 const commit = this.config.serverCommit || ''
185 let result = serverVersion
186 if (commit) result += '...' + commit
192 this.configLoaded = false
193 this.configReset = true
195 // Notify config update
196 this.getConfig().subscribe(() => {
197 // empty, to fire a reset config event
202 if (this.configLoaded) return of(this.config)
204 if (!this.configObservable) {
205 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
207 tap(config => this.saveConfigLocally(config)),
210 this.configLoaded = true
213 if (this.configReset) {
214 this.configReloaded.next(config)
215 this.configReset = false
222 return this.configObservable
229 getVideoCategories () {
230 if (!this.videoCategoriesObservable) {
231 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
234 return this.videoCategoriesObservable.pipe(first())
237 getVideoLicences () {
238 if (!this.videoLicensesObservable) {
239 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
242 return this.videoLicensesObservable.pipe(first())
245 getVideoLanguages () {
246 if (!this.videoLanguagesObservable) {
247 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
250 return this.videoLanguagesObservable.pipe(first())
253 getVideoPrivacies () {
254 if (!this.videoPrivaciesObservable) {
255 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
258 return this.videoPrivaciesObservable.pipe(first())
261 getVideoPlaylistPrivacies () {
262 if (!this.videoPlaylistPrivaciesObservable) {
263 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
266 return this.videoPlaylistPrivaciesObservable.pipe(first())
270 if (!this.localeObservable) {
271 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
273 // Default locale, nothing to translate
274 if (isDefaultLocale(completeLocale)) {
275 this.localeObservable = of({}).pipe(shareReplay())
277 this.localeObservable = this.http
278 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
283 return this.localeObservable.pipe(first())
287 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
290 getDefaultSearchTarget (): Promise<SearchTargetType> {
291 return this.getConfig().pipe(
293 const searchIndexConfig = config.search.searchIndex
295 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
296 return 'search-index'
304 private loadAttributeEnum <T extends string | number> (
306 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
309 return this.getServerLocale()
311 switchMap(translations => {
312 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
313 .pipe(map(data => ({ data, translations })))
315 map(({ data, translations }) => {
316 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
318 const label = data[ dataKey ]
320 const id = attributeName === 'languages'
322 : parseInt(dataKey, 10) as T
326 label: peertubeTranslate(label, translations)
330 if (sort === true) sortBy(hashToPopulate, 'label')
332 return hashToPopulate
338 private saveConfigLocally (config: ServerConfig) {
339 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
342 private loadConfigLocally () {
343 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
347 const parsed = JSON.parse(configString)
348 Object.assign(this.config, parsed)
350 console.error('Cannot parse config saved in local storage.', err)