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 { environment } from '../../../environments/environment'
8 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
9 import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n'
10 import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
11 import { sortBy } from '@app/shared/misc/utils'
12 import { VideoPlaylistPrivacy } from '@shared/models/videos/playlist/video-playlist-privacy.model'
13 import { cloneDeep } from 'lodash-es'
16 export class ServerService {
17 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
18 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
19 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
20 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
21 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
22 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
24 configLoaded = new ReplaySubject<boolean>(1)
25 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
26 videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1)
27 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
28 videoLicencesLoaded = new ReplaySubject<boolean>(1)
29 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
30 localeObservable: Observable<any>
32 private config: ServerConfig = {
35 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
36 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
37 defaultClientRoute: '',
39 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
58 serverVersion: 'Unknown',
61 allowedForCurrentIP: false,
62 requiresEmailVerification: false
65 enabledResolutions: [],
124 private videoCategories: Array<VideoConstant<number>> = []
125 private videoLicences: Array<VideoConstant<number>> = []
126 private videoLanguages: Array<VideoConstant<string>> = []
127 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
128 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
131 private http: HttpClient,
132 @Inject(LOCALE_ID) private localeId: string
134 this.loadServerLocale()
135 this.loadConfigLocally()
139 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
140 .pipe(tap(this.saveConfigLocally))
144 this.configLoaded.next(true)
148 loadVideoCategories () {
149 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
152 loadVideoLicences () {
153 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
156 loadVideoLanguages () {
157 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
160 loadVideoPrivacies () {
161 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
164 loadVideoPlaylistPrivacies () {
165 return this.loadAttributeEnum(
166 ServerService.BASE_VIDEO_PLAYLIST_URL,
168 this.videoPlaylistPrivacies,
169 this.videoPlaylistPrivaciesLoaded
174 return cloneDeep(this.config)
177 getVideoCategories () {
178 return cloneDeep(this.videoCategories)
181 getVideoLicences () {
182 return cloneDeep(this.videoLicences)
185 getVideoLanguages () {
186 return cloneDeep(this.videoLanguages)
189 getVideoPrivacies () {
190 return cloneDeep(this.videoPrivacies)
193 getVideoPlaylistPrivacies () {
194 return cloneDeep(this.videoPlaylistPrivacies)
197 private loadAttributeEnum (
199 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
200 hashToPopulate: VideoConstant<string | number>[],
201 notifier: ReplaySubject<boolean>,
204 this.localeObservable
206 switchMap(translations => {
207 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
208 .pipe(map(data => ({ data, translations })))
211 .subscribe(({ data, translations }) => {
213 .forEach(dataKey => {
214 const label = data[ dataKey ]
216 hashToPopulate.push({
217 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
218 label: peertubeTranslate(label, translations)
222 if (sort === true) sortBy(hashToPopulate, 'label')
228 private loadServerLocale () {
229 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
231 // Default locale, nothing to translate
232 if (isDefaultLocale(completeLocale)) {
233 this.localeObservable = of({}).pipe(shareReplay())
237 this.localeObservable = this.http
238 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
242 private saveConfigLocally (config: ServerConfig) {
243 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
246 private loadConfigLocally () {
247 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
251 const parsed = JSON.parse(configString)
252 Object.assign(this.config, parsed)
254 console.error('Cannot parse config saved in local storage.', err)