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 } from '@app/shared/i18n/i18n-utils'
6 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
7 import { sortBy } from '@app/shared/misc/utils'
8 import { SearchTargetType } from '@shared/models/search/search-target-query.model'
9 import { ServerStats } from '@shared/models/server'
10 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
11 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
12 import { VideoConstant } from '../../../../../shared/models/videos'
13 import { environment } from '../../../environments/environment'
16 export class ServerService {
17 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
18 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
19 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
20 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
21 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
23 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
25 configReloaded = new Subject<ServerConfig>()
27 private localeObservable: Observable<any>
28 private videoLicensesObservable: Observable<VideoConstant<number>[]>
29 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
30 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
31 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
32 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
33 private configObservable: Observable<ServerConfig>
35 private configReset = false
37 private configLoaded = false
38 private config: ServerConfig = {
41 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
42 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
43 defaultClientRoute: '',
45 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
53 registeredExternalAuths: [],
54 registeredIdAndPassAuths: []
66 serverVersion: 'Unknown',
69 allowedForCurrentIP: false,
70 requiresEmailVerification: false
73 enabledResolutions: [],
134 indexUrl: 'https://instances.joinpeertube.org'
152 disableLocalSearch: false,
153 isDefaultSearch: false
159 private http: HttpClient,
160 @Inject(LOCALE_ID) private localeId: string
162 this.loadConfigLocally()
165 getServerVersionAndCommit () {
166 const serverVersion = this.config.serverVersion
167 const commit = this.config.serverCommit || ''
169 let result = serverVersion
170 if (commit) result += '...' + commit
176 this.configLoaded = false
177 this.configReset = true
179 // Notify config update
180 this.getConfig().subscribe(() => {
181 // empty, to fire a reset config event
186 if (this.configLoaded) return of(this.config)
188 if (!this.configObservable) {
189 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
191 tap(config => this.saveConfigLocally(config)),
194 this.configLoaded = true
197 if (this.configReset) {
198 this.configReloaded.next(config)
199 this.configReset = false
206 return this.configObservable
213 getVideoCategories () {
214 if (!this.videoCategoriesObservable) {
215 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
218 return this.videoCategoriesObservable.pipe(first())
221 getVideoLicences () {
222 if (!this.videoLicensesObservable) {
223 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
226 return this.videoLicensesObservable.pipe(first())
229 getVideoLanguages () {
230 if (!this.videoLanguagesObservable) {
231 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
234 return this.videoLanguagesObservable.pipe(first())
237 getVideoPrivacies () {
238 if (!this.videoPrivaciesObservable) {
239 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
242 return this.videoPrivaciesObservable.pipe(first())
245 getVideoPlaylistPrivacies () {
246 if (!this.videoPlaylistPrivaciesObservable) {
247 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
250 return this.videoPlaylistPrivaciesObservable.pipe(first())
254 if (!this.localeObservable) {
255 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
257 // Default locale, nothing to translate
258 if (isDefaultLocale(completeLocale)) {
259 this.localeObservable = of({}).pipe(shareReplay())
261 this.localeObservable = this.http
262 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
267 return this.localeObservable.pipe(first())
271 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
274 getDefaultSearchTarget (): Promise<SearchTargetType> {
275 return this.getConfig().pipe(
277 const searchIndexConfig = config.search.searchIndex
279 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
280 return 'search-index'
288 private loadAttributeEnum <T extends string | number> (
290 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
293 return this.getServerLocale()
295 switchMap(translations => {
296 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
297 .pipe(map(data => ({ data, translations })))
299 map(({ data, translations }) => {
300 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
302 const label = data[ dataKey ]
304 const id = attributeName === 'languages'
306 : parseInt(dataKey, 10) as T
310 label: peertubeTranslate(label, translations)
314 if (sort === true) sortBy(hashToPopulate, 'label')
316 return hashToPopulate
322 private saveConfigLocally (config: ServerConfig) {
323 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
326 private loadConfigLocally () {
327 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
331 const parsed = JSON.parse(configString)
332 Object.assign(this.config, parsed)
334 console.error('Cannot parse config saved in local storage.', err)