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, VideoPrivacy } 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,
44 requiresEmailVerification: false
47 enabledResolutions: []
85 private videoCategories: Array<VideoConstant<number>> = []
86 private videoLicences: Array<VideoConstant<number>> = []
87 private videoLanguages: Array<VideoConstant<string>> = []
88 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
91 private http: HttpClient,
92 @Inject(LOCALE_ID) private localeId: string
94 this.loadServerLocale()
95 this.loadConfigLocally()
99 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
100 .pipe(tap(this.saveConfigLocally))
104 this.configLoaded.next(true)
108 loadVideoCategories () {
109 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
112 loadVideoLicences () {
113 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
116 loadVideoLanguages () {
117 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
120 loadVideoPrivacies () {
121 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
128 getVideoCategories () {
129 return this.videoCategories
132 getVideoLicences () {
133 return this.videoLicences
136 getVideoLanguages () {
137 return this.videoLanguages
140 getVideoPrivacies () {
141 return this.videoPrivacies
145 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
148 private loadVideoAttributeEnum (
149 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
150 hashToPopulate: VideoConstant<string | number>[],
151 notifier: ReplaySubject<boolean>,
154 this.localeObservable
156 switchMap(translations => {
157 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
158 .pipe(map(data => ({ data, translations })))
161 .subscribe(({ data, translations }) => {
163 .forEach(dataKey => {
164 const label = data[ dataKey ]
166 hashToPopulate.push({
167 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
168 label: peertubeTranslate(label, translations)
172 if (sort === true) sortBy(hashToPopulate, 'label')
178 private loadServerLocale () {
179 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
181 // Default locale, nothing to translate
182 if (isDefaultLocale(completeLocale)) {
183 this.localeObservable = of({}).pipe(shareReplay())
187 this.localeObservable = this.http
188 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
192 private saveConfigLocally (config: ServerConfig) {
193 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
196 private loadConfigLocally () {
197 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
201 const parsed = JSON.parse(configString)
202 Object.assign(this.config, parsed)
204 console.error('Cannot parse config saved in local storage.', err)