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'
14 export class ServerService {
15 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
16 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
17 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
18 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
19 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
21 configReloaded = new Subject<void>()
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',
60 serverVersion: 'Unknown',
63 allowedForCurrentIP: false,
64 requiresEmailVerification: false
67 enabledResolutions: [],
128 indexUrl: 'https://instances.joinpeertube.org'
135 private http: HttpClient,
136 @Inject(LOCALE_ID) private localeId: string
138 this.loadConfigLocally()
141 getServerVersionAndCommit () {
142 const serverVersion = this.config.serverVersion
143 const commit = this.config.serverCommit || ''
145 let result = serverVersion
146 if (commit) result += '...' + commit
152 this.configLoaded = false
153 this.configReset = true
157 if (this.configLoaded) return of(this.config)
159 if (!this.configObservable) {
160 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
162 tap(this.saveConfigLocally),
163 tap(() => this.configLoaded = true),
165 if (this.configReset) {
166 this.configReloaded.next()
167 this.configReset = false
174 return this.configObservable
181 getVideoCategories () {
182 if (!this.videoCategoriesObservable) {
183 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
186 return this.videoCategoriesObservable.pipe(first())
189 getVideoLicences () {
190 if (!this.videoLicensesObservable) {
191 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
194 return this.videoLicensesObservable.pipe(first())
197 getVideoLanguages () {
198 if (!this.videoLanguagesObservable) {
199 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
202 return this.videoLanguagesObservable.pipe(first())
205 getVideoPrivacies () {
206 if (!this.videoPrivaciesObservable) {
207 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
210 return this.videoPrivaciesObservable.pipe(first())
213 getVideoPlaylistPrivacies () {
214 if (!this.videoPlaylistPrivaciesObservable) {
215 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
218 return this.videoPlaylistPrivaciesObservable.pipe(first())
222 if (!this.localeObservable) {
223 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
225 // Default locale, nothing to translate
226 if (isDefaultLocale(completeLocale)) {
227 this.localeObservable = of({}).pipe(shareReplay())
229 this.localeObservable = this.http
230 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
235 return this.localeObservable.pipe(first())
238 private loadAttributeEnum <T extends string | number> (
240 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
243 return this.getServerLocale()
245 switchMap(translations => {
246 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
247 .pipe(map(data => ({ data, translations })))
249 map(({ data, translations }) => {
250 const hashToPopulate: VideoConstant<T>[] = []
253 .forEach(dataKey => {
254 const label = data[ dataKey ]
256 hashToPopulate.push({
257 id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T,
258 label: peertubeTranslate(label, translations)
262 if (sort === true) sortBy(hashToPopulate, 'label')
264 return hashToPopulate
270 private saveConfigLocally (config: ServerConfig) {
271 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
274 private loadConfigLocally () {
275 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
279 const parsed = JSON.parse(configString)
280 Object.assign(this.config, parsed)
282 console.error('Cannot parse config saved in local storage.', err)