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',
52 serverVersion: 'Unknown',
55 allowedForCurrentIP: false,
56 requiresEmailVerification: false
59 enabledResolutions: [],
115 private videoCategories: Array<VideoConstant<number>> = []
116 private videoLicences: Array<VideoConstant<number>> = []
117 private videoLanguages: Array<VideoConstant<string>> = []
118 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
119 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
122 private http: HttpClient,
123 @Inject(LOCALE_ID) private localeId: string
125 this.loadServerLocale()
126 this.loadConfigLocally()
130 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
131 .pipe(tap(this.saveConfigLocally))
135 this.configLoaded.next(true)
139 loadVideoCategories () {
140 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
143 loadVideoLicences () {
144 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
147 loadVideoLanguages () {
148 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
151 loadVideoPrivacies () {
152 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
155 loadVideoPlaylistPrivacies () {
156 return this.loadAttributeEnum(
157 ServerService.BASE_VIDEO_PLAYLIST_URL,
159 this.videoPlaylistPrivacies,
160 this.videoPlaylistPrivaciesLoaded
165 return cloneDeep(this.config)
168 getVideoCategories () {
169 return cloneDeep(this.videoCategories)
172 getVideoLicences () {
173 return cloneDeep(this.videoLicences)
176 getVideoLanguages () {
177 return cloneDeep(this.videoLanguages)
180 getVideoPrivacies () {
181 return cloneDeep(this.videoPrivacies)
184 getVideoPlaylistPrivacies () {
185 return cloneDeep(this.videoPlaylistPrivacies)
188 private loadAttributeEnum (
190 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
191 hashToPopulate: VideoConstant<string | number>[],
192 notifier: ReplaySubject<boolean>,
195 this.localeObservable
197 switchMap(translations => {
198 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
199 .pipe(map(data => ({ data, translations })))
202 .subscribe(({ data, translations }) => {
204 .forEach(dataKey => {
205 const label = data[ dataKey ]
207 hashToPopulate.push({
208 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
209 label: peertubeTranslate(label, translations)
213 if (sort === true) sortBy(hashToPopulate, 'label')
219 private loadServerLocale () {
220 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
222 // Default locale, nothing to translate
223 if (isDefaultLocale(completeLocale)) {
224 this.localeObservable = of({}).pipe(shareReplay())
228 this.localeObservable = this.http
229 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
233 private saveConfigLocally (config: ServerConfig) {
234 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
237 private loadConfigLocally () {
238 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
242 const parsed = JSON.parse(configString)
243 Object.assign(this.config, parsed)
245 console.error('Cannot parse config saved in local storage.', err)