1 import { map, 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-local-storage'
5 import { Observable, of, ReplaySubject } from 'rxjs'
6 import { getCompleteLocale, ServerConfig } from '../../../../../shared'
7 import { About } from '../../../../../shared/models/server/about.model'
8 import { environment } from '../../../environments/environment'
9 import { VideoConstant } from '../../../../../shared/models/videos'
10 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
11 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
12 import { sortBy } from '@app/shared/misc/utils'
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_LOCALE_URL = environment.apiUrl + '/client/locales/'
19 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
21 configLoaded = new ReplaySubject<boolean>(1)
22 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
23 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
24 videoLicencesLoaded = new ReplaySubject<boolean>(1)
25 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
26 localeObservable: Observable<any>
28 private config: ServerConfig = {
31 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
32 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
33 defaultClientRoute: '',
34 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
40 serverVersion: 'Unknown',
43 allowedForCurrentIP: false
46 enabledResolutions: []
84 private videoCategories: Array<VideoConstant<string>> = []
85 private videoLicences: Array<VideoConstant<string>> = []
86 private videoLanguages: Array<VideoConstant<string>> = []
87 private videoPrivacies: Array<VideoConstant<string>> = []
90 private http: HttpClient,
91 @Inject(LOCALE_ID) private localeId: string
93 this.loadServerLocale()
94 this.loadConfigLocally()
98 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
99 .pipe(tap(this.saveConfigLocally))
103 this.configLoaded.next(true)
107 loadVideoCategories () {
108 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
111 loadVideoLicences () {
112 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
115 loadVideoLanguages () {
116 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
119 loadVideoPrivacies () {
120 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
127 getVideoCategories () {
128 return this.videoCategories
131 getVideoLicences () {
132 return this.videoLicences
135 getVideoLanguages () {
136 return this.videoLanguages
139 getVideoPrivacies () {
140 return this.videoPrivacies
144 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
147 private loadVideoAttributeEnum (
148 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
149 hashToPopulate: VideoConstant<string>[],
150 notifier: ReplaySubject<boolean>,
153 this.localeObservable
155 switchMap(translations => {
156 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
157 .pipe(map(data => ({ data, translations })))
160 .subscribe(({ data, translations }) => {
162 .forEach(dataKey => {
163 const label = data[ dataKey ]
165 hashToPopulate.push({
167 label: peertubeTranslate(label, translations)
171 if (sort === true) sortBy(hashToPopulate, 'label')
177 private loadServerLocale () {
178 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
180 // Default locale, nothing to translate
181 if (isDefaultLocale(completeLocale)) {
182 this.localeObservable = of({}).pipe(shareReplay())
186 this.localeObservable = this.http
187 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
191 private saveConfigLocally (config: ServerConfig) {
192 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
195 private loadConfigLocally () {
196 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
200 const parsed = JSON.parse(configString)
201 Object.assign(this.config, parsed)
203 console.error('Cannot parse config saved in local storage.', err)