1 import { map, share, 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: []
83 private videoCategories: Array<VideoConstant<string>> = []
84 private videoLicences: Array<VideoConstant<string>> = []
85 private videoLanguages: Array<VideoConstant<string>> = []
86 private videoPrivacies: Array<VideoConstant<string>> = []
89 private http: HttpClient,
90 @Inject(LOCALE_ID) private localeId: string
92 this.loadServerLocale()
93 this.loadConfigLocally()
97 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
98 .pipe(tap(this.saveConfigLocally))
102 this.configLoaded.next(true)
106 loadVideoCategories () {
107 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
110 loadVideoLicences () {
111 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
114 loadVideoLanguages () {
115 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
118 loadVideoPrivacies () {
119 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
126 getVideoCategories () {
127 return this.videoCategories
130 getVideoLicences () {
131 return this.videoLicences
134 getVideoLanguages () {
135 return this.videoLanguages
138 getVideoPrivacies () {
139 return this.videoPrivacies
143 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
146 private loadVideoAttributeEnum (
147 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
148 hashToPopulate: VideoConstant<string>[],
149 notifier: ReplaySubject<boolean>,
152 this.localeObservable
154 switchMap(translations => {
155 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
156 .pipe(map(data => ({ data, translations })))
159 .subscribe(({ data, translations }) => {
161 .forEach(dataKey => {
162 const label = data[ dataKey ]
164 hashToPopulate.push({
166 label: peertubeTranslate(label, translations)
170 if (sort === true) sortBy(hashToPopulate, 'label')
176 private loadServerLocale () {
177 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
179 // Default locale, nothing to translate
180 if (isDefaultLocale(completeLocale)) {
181 this.localeObservable = of({}).pipe(share())
185 this.localeObservable = this.http
186 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
190 private saveConfigLocally (config: ServerConfig) {
191 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
194 private loadConfigLocally () {
195 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
199 const parsed = JSON.parse(configString)
200 Object.assign(this.config, parsed)
202 console.error('Cannot parse config saved in local storage.', err)