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.',
39 defaultClientRoute: '',
41 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
49 registeredExternalAuths: [],
50 registeredIdAndPassAuths: []
62 serverVersion: 'Unknown',
65 allowedForCurrentIP: false,
66 requiresEmailVerification: false
69 enabledResolutions: [],
83 enabledResolutions: []
139 indexUrl: 'https://instances.joinpeertube.org'
157 disableLocalSearch: false,
158 isDefaultSearch: false
164 private http: HttpClient,
165 @Inject(LOCALE_ID) private localeId: string
167 this.loadConfigLocally()
170 getServerVersionAndCommit () {
171 const serverVersion = this.config.serverVersion
172 const commit = this.config.serverCommit || ''
174 let result = serverVersion
175 if (commit) result += '...' + commit
181 this.configLoaded = false
182 this.configReset = true
184 // Notify config update
185 this.getConfig().subscribe(() => {
186 // empty, to fire a reset config event
191 if (this.configLoaded) return of(this.config)
193 if (!this.configObservable) {
194 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
196 tap(config => this.saveConfigLocally(config)),
199 this.configLoaded = true
202 if (this.configReset) {
203 this.configReloaded.next(config)
204 this.configReset = false
211 return this.configObservable
218 getVideoCategories () {
219 if (!this.videoCategoriesObservable) {
220 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
223 return this.videoCategoriesObservable.pipe(first())
226 getVideoLicences () {
227 if (!this.videoLicensesObservable) {
228 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
231 return this.videoLicensesObservable.pipe(first())
234 getVideoLanguages () {
235 if (!this.videoLanguagesObservable) {
236 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
239 return this.videoLanguagesObservable.pipe(first())
242 getVideoPrivacies () {
243 if (!this.videoPrivaciesObservable) {
244 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
247 return this.videoPrivaciesObservable.pipe(first())
250 getVideoPlaylistPrivacies () {
251 if (!this.videoPlaylistPrivaciesObservable) {
252 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
255 return this.videoPlaylistPrivaciesObservable.pipe(first())
259 if (!this.localeObservable) {
260 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
262 // Default locale, nothing to translate
263 if (isDefaultLocale(completeLocale)) {
264 this.localeObservable = of({}).pipe(shareReplay())
266 this.localeObservable = this.http
267 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
272 return this.localeObservable.pipe(first())
276 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
279 getDefaultSearchTarget (): Promise<SearchTargetType> {
280 return this.getConfig().pipe(
282 const searchIndexConfig = config.search.searchIndex
284 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
285 return 'search-index'
293 private loadAttributeEnum <T extends string | number> (
295 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
298 return this.getServerLocale()
300 switchMap(translations => {
301 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
302 .pipe(map(data => ({ data, translations })))
304 map(({ data, translations }) => {
305 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
307 const label = data[ dataKey ]
309 const id = attributeName === 'languages'
311 : parseInt(dataKey, 10) as T
315 label: peertubeTranslate(label, translations)
319 if (sort === true) sortBy(hashToPopulate, 'label')
321 return hashToPopulate
327 private saveConfigLocally (config: ServerConfig) {
328 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
331 private loadConfigLocally () {
332 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
336 const parsed = JSON.parse(configString)
337 Object.assign(this.config, parsed)
339 console.error('Cannot parse config saved in local storage.', err)