1 import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
5 import { Observable, of, Subject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { environment } from '../../../environments/environment'
8 import { VideoConstant } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12 import { ServerStats } from '@shared/models/server'
15 export class ServerService {
16 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
17 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
18 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
19 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
20 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
24 configReloaded = new Subject<void>()
26 private localeObservable: Observable<any>
27 private videoLicensesObservable: Observable<VideoConstant<number>[]>
28 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
29 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
30 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
31 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
32 private configObservable: Observable<ServerConfig>
34 private configReset = false
36 private configLoaded = false
37 private config: ServerConfig = {
40 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
41 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
42 defaultClientRoute: '',
44 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
63 serverVersion: 'Unknown',
66 allowedForCurrentIP: false,
67 requiresEmailVerification: false
70 enabledResolutions: [],
131 indexUrl: 'https://instances.joinpeertube.org'
138 private http: HttpClient,
139 @Inject(LOCALE_ID) private localeId: string
141 this.loadConfigLocally()
144 getServerVersionAndCommit () {
145 const serverVersion = this.config.serverVersion
146 const commit = this.config.serverCommit || ''
148 let result = serverVersion
149 if (commit) result += '...' + commit
155 this.configLoaded = false
156 this.configReset = true
160 if (this.configLoaded) return of(this.config)
162 if (!this.configObservable) {
163 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
165 tap(this.saveConfigLocally),
166 tap(() => this.configLoaded = true),
168 if (this.configReset) {
169 this.configReloaded.next()
170 this.configReset = false
177 return this.configObservable
184 getVideoCategories () {
185 if (!this.videoCategoriesObservable) {
186 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
189 return this.videoCategoriesObservable.pipe(first())
192 getVideoLicences () {
193 if (!this.videoLicensesObservable) {
194 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
197 return this.videoLicensesObservable.pipe(first())
200 getVideoLanguages () {
201 if (!this.videoLanguagesObservable) {
202 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
205 return this.videoLanguagesObservable.pipe(first())
208 getVideoPrivacies () {
209 if (!this.videoPrivaciesObservable) {
210 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
213 return this.videoPrivaciesObservable.pipe(first())
216 getVideoPlaylistPrivacies () {
217 if (!this.videoPlaylistPrivaciesObservable) {
218 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
221 return this.videoPlaylistPrivaciesObservable.pipe(first())
225 if (!this.localeObservable) {
226 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
228 // Default locale, nothing to translate
229 if (isDefaultLocale(completeLocale)) {
230 this.localeObservable = of({}).pipe(shareReplay())
232 this.localeObservable = this.http
233 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
238 return this.localeObservable.pipe(first())
242 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
245 private loadAttributeEnum <T extends string | number> (
247 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
250 return this.getServerLocale()
252 switchMap(translations => {
253 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
254 .pipe(map(data => ({ data, translations })))
256 map(({ data, translations }) => {
257 const hashToPopulate: VideoConstant<T>[] = []
260 .forEach(dataKey => {
261 const label = data[ dataKey ]
263 hashToPopulate.push({
264 id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T,
265 label: peertubeTranslate(label, translations)
269 if (sort === true) sortBy(hashToPopulate, 'label')
271 return hashToPopulate
277 private saveConfigLocally (config: ServerConfig) {
278 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
281 private loadConfigLocally () {
282 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
286 const parsed = JSON.parse(configString)
287 Object.assign(this.config, parsed)
289 console.error('Cannot parse config saved in local storage.', err)