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
69 enabledResolutions: [],
85 enabledResolutions: []
130 enabled: [ 'hot', 'most-viewed', 'most-liked' ],
131 default: 'most-viewed'
148 indexUrl: 'https://instances.joinpeertube.org'
166 disableLocalSearch: false,
167 isDefaultSearch: false
173 private http: HttpClient,
174 @Inject(LOCALE_ID) private localeId: string
176 this.loadConfigLocally()
179 getServerVersionAndCommit () {
180 const serverVersion = this.config.serverVersion
181 const commit = this.config.serverCommit || ''
183 let result = serverVersion
184 if (commit) result += '...' + commit
190 this.configLoaded = false
191 this.configReset = true
193 // Notify config update
194 this.getConfig().subscribe(() => {
195 // empty, to fire a reset config event
200 if (this.configLoaded) return of(this.config)
202 if (!this.configObservable) {
203 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
205 tap(config => this.saveConfigLocally(config)),
208 this.configLoaded = true
211 if (this.configReset) {
212 this.configReloaded.next(config)
213 this.configReset = false
220 return this.configObservable
227 getVideoCategories () {
228 if (!this.videoCategoriesObservable) {
229 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
232 return this.videoCategoriesObservable.pipe(first())
235 getVideoLicences () {
236 if (!this.videoLicensesObservable) {
237 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
240 return this.videoLicensesObservable.pipe(first())
243 getVideoLanguages () {
244 if (!this.videoLanguagesObservable) {
245 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
248 return this.videoLanguagesObservable.pipe(first())
251 getVideoPrivacies () {
252 if (!this.videoPrivaciesObservable) {
253 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
256 return this.videoPrivaciesObservable.pipe(first())
259 getVideoPlaylistPrivacies () {
260 if (!this.videoPlaylistPrivaciesObservable) {
261 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
264 return this.videoPlaylistPrivaciesObservable.pipe(first())
268 if (!this.localeObservable) {
269 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
271 // Default locale, nothing to translate
272 if (isDefaultLocale(completeLocale)) {
273 this.localeObservable = of({}).pipe(shareReplay())
275 this.localeObservable = this.http
276 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
281 return this.localeObservable.pipe(first())
285 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
288 getDefaultSearchTarget (): Promise<SearchTargetType> {
289 return this.getConfig().pipe(
291 const searchIndexConfig = config.search.searchIndex
293 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
294 return 'search-index'
302 private loadAttributeEnum <T extends string | number> (
304 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
307 return this.getServerLocale()
309 switchMap(translations => {
310 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
311 .pipe(map(data => ({ data, translations })))
313 map(({ data, translations }) => {
314 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
316 const label = data[ dataKey ]
318 const id = attributeName === 'languages'
320 : parseInt(dataKey, 10) as T
324 label: peertubeTranslate(label, translations)
328 if (sort === true) sortBy(hashToPopulate, 'label')
330 return hashToPopulate
336 private saveConfigLocally (config: ServerConfig) {
337 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
340 private loadConfigLocally () {
341 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
345 const parsed = JSON.parse(configString)
346 Object.assign(this.config, parsed)
348 console.error('Cannot parse config saved in local storage.', err)