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, ReplaySubject, of } 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 } from '../../../../../shared/models/i18n'
11 import { getDevLocale, isOnDevLocale, peertubeTranslate } from '@app/shared/i18n/i18n-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_LOCALE_URL = environment.apiUrl + '/client/locales/'
18 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
20 configLoaded = new ReplaySubject<boolean>(1)
21 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
22 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
23 videoLicencesLoaded = new ReplaySubject<boolean>(1)
24 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
25 localeObservable: Observable<any>
27 private config: ServerConfig = {
30 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
31 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
32 defaultClientRoute: '',
33 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
39 serverVersion: 'Unknown',
42 allowedForCurrentIP: false
45 enabledResolutions: []
72 private videoCategories: Array<VideoConstant<string>> = []
73 private videoLicences: Array<VideoConstant<string>> = []
74 private videoLanguages: Array<VideoConstant<string>> = []
75 private videoPrivacies: Array<VideoConstant<string>> = []
78 private http: HttpClient,
79 @Inject(LOCALE_ID) private localeId: string
81 this.loadServerLocale()
82 this.loadConfigLocally()
86 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
87 .pipe(tap(this.saveConfigLocally))
91 this.configLoaded.next(true)
95 loadVideoCategories () {
96 return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
99 loadVideoLicences () {
100 return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
103 loadVideoLanguages () {
104 return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
107 loadVideoPrivacies () {
108 return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
115 getVideoCategories () {
116 return this.videoCategories
119 getVideoLicences () {
120 return this.videoLicences
123 getVideoLanguages () {
124 return this.videoLanguages
127 getVideoPrivacies () {
128 return this.videoPrivacies
132 return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
135 private loadVideoAttributeEnum (
136 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
137 hashToPopulate: VideoConstant<string>[],
138 notifier: ReplaySubject<boolean>,
141 this.localeObservable
143 switchMap(translations => {
144 return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
145 .pipe(map(data => ({ data, translations })))
148 .subscribe(({ data, translations }) => {
150 .forEach(dataKey => {
151 const label = data[ dataKey ]
153 hashToPopulate.push({
155 label: peertubeTranslate(label, translations)
160 hashToPopulate.sort((a, b) => {
161 if (a.label < b.label) return -1
162 if (a.label === b.label) return 0
171 private loadServerLocale () {
172 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
174 // Default locale, nothing to translate
175 if (isDefaultLocale(completeLocale)) {
176 this.localeObservable = of({}).pipe(share())
180 this.localeObservable = this.http
181 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
185 private saveConfigLocally (config: ServerConfig) {
186 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
189 private loadConfigLocally () {
190 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
194 const parsed = JSON.parse(configString)
195 Object.assign(this.config, parsed)
197 console.error('Cannot parse config saved in local storage.', err)