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, peertubeLocalStorage, sortBy } from '@app/helpers'
14 } from '@shared/models'
15 import { environment } from '../../../environments/environment'
18 export class ServerService {
19 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
20 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
21 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
22 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
23 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
25 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
27 configReloaded = new Subject<ServerConfig>()
29 private localeObservable: Observable<any>
30 private videoLicensesObservable: Observable<VideoConstant<number>[]>
31 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
32 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
33 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
34 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
35 private configObservable: Observable<ServerConfig>
37 private configReset = false
39 private configLoaded = false
40 private config: ServerConfig = {
43 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
44 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
45 defaultClientRoute: '',
47 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
55 registeredExternalAuths: [],
56 registeredIdAndPassAuths: []
68 serverVersion: 'Unknown',
71 allowedForCurrentIP: false,
72 requiresEmailVerification: false
75 enabledResolutions: [],
136 indexUrl: 'https://instances.joinpeertube.org'
154 disableLocalSearch: false,
155 isDefaultSearch: false
161 private http: HttpClient,
162 @Inject(LOCALE_ID) private localeId: string
164 this.loadConfigLocally()
167 getServerVersionAndCommit () {
168 const serverVersion = this.config.serverVersion
169 const commit = this.config.serverCommit || ''
171 let result = serverVersion
172 if (commit) result += '...' + commit
178 this.configLoaded = false
179 this.configReset = true
181 // Notify config update
182 this.getConfig().subscribe(() => {
183 // empty, to fire a reset config event
188 if (this.configLoaded) return of(this.config)
190 if (!this.configObservable) {
191 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
193 tap(config => this.saveConfigLocally(config)),
196 this.configLoaded = true
199 if (this.configReset) {
200 this.configReloaded.next(config)
201 this.configReset = false
208 return this.configObservable
215 getVideoCategories () {
216 if (!this.videoCategoriesObservable) {
217 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
220 return this.videoCategoriesObservable.pipe(first())
223 getVideoLicences () {
224 if (!this.videoLicensesObservable) {
225 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
228 return this.videoLicensesObservable.pipe(first())
231 getVideoLanguages () {
232 if (!this.videoLanguagesObservable) {
233 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
236 return this.videoLanguagesObservable.pipe(first())
239 getVideoPrivacies () {
240 if (!this.videoPrivaciesObservable) {
241 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
244 return this.videoPrivaciesObservable.pipe(first())
247 getVideoPlaylistPrivacies () {
248 if (!this.videoPlaylistPrivaciesObservable) {
249 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
252 return this.videoPlaylistPrivaciesObservable.pipe(first())
256 if (!this.localeObservable) {
257 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
259 // Default locale, nothing to translate
260 if (isDefaultLocale(completeLocale)) {
261 this.localeObservable = of({}).pipe(shareReplay())
263 this.localeObservable = this.http
264 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
269 return this.localeObservable.pipe(first())
273 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
276 getDefaultSearchTarget (): Promise<SearchTargetType> {
277 return this.getConfig().pipe(
279 const searchIndexConfig = config.search.searchIndex
281 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
282 return 'search-index'
290 private loadAttributeEnum <T extends string | number> (
292 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
295 return this.getServerLocale()
297 switchMap(translations => {
298 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
299 .pipe(map(data => ({ data, translations })))
301 map(({ data, translations }) => {
302 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
304 const label = data[ dataKey ]
306 const id = attributeName === 'languages'
308 : parseInt(dataKey, 10) as T
312 label: peertubeTranslate(label, translations)
316 if (sort === true) sortBy(hashToPopulate, 'label')
318 return hashToPopulate
324 private saveConfigLocally (config: ServerConfig) {
325 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
328 private loadConfigLocally () {
329 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
333 const parsed = JSON.parse(configString)
334 Object.assign(this.config, parsed)
336 console.error('Cannot parse config saved in local storage.', err)