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'
15 export class ServerService {
16 private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server/'
17 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
18 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
19 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
20 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
21 private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
23 configLoaded = new ReplaySubject<boolean>(1)
24 videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
25 videoPlaylistPrivaciesLoaded = new ReplaySubject<boolean>(1)
26 videoCategoriesLoaded = new ReplaySubject<boolean>(1)
27 videoLicencesLoaded = new ReplaySubject<boolean>(1)
28 videoLanguagesLoaded = new ReplaySubject<boolean>(1)
29 localeObservable: Observable<any>
31 private config: ServerConfig = {
34 shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
35 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
36 defaultClientRoute: '',
38 defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
50 serverVersion: 'Unknown',
53 allowedForCurrentIP: false,
54 requiresEmailVerification: false
57 enabledResolutions: [],
110 private videoCategories: Array<VideoConstant<number>> = []
111 private videoLicences: Array<VideoConstant<number>> = []
112 private videoLanguages: Array<VideoConstant<string>> = []
113 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
114 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
117 private http: HttpClient,
118 @Inject(LOCALE_ID) private localeId: string
120 this.loadServerLocale()
121 this.loadConfigLocally()
125 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
126 .pipe(tap(this.saveConfigLocally))
130 this.configLoaded.next(true)
134 loadVideoCategories () {
135 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
138 loadVideoLicences () {
139 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
142 loadVideoLanguages () {
143 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
146 loadVideoPrivacies () {
147 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
150 loadVideoPlaylistPrivacies () {
151 return this.loadAttributeEnum(
152 ServerService.BASE_VIDEO_PLAYLIST_URL,
154 this.videoPlaylistPrivacies,
155 this.videoPlaylistPrivaciesLoaded
163 getVideoCategories () {
164 return this.videoCategories
167 getVideoLicences () {
168 return this.videoLicences
171 getVideoLanguages () {
172 return this.videoLanguages
175 getVideoPrivacies () {
176 return this.videoPrivacies
179 getVideoPlaylistPrivacies () {
180 return this.videoPlaylistPrivacies
183 private loadAttributeEnum (
185 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
186 hashToPopulate: VideoConstant<string | number>[],
187 notifier: ReplaySubject<boolean>,
190 this.localeObservable
192 switchMap(translations => {
193 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
194 .pipe(map(data => ({ data, translations })))
197 .subscribe(({ data, translations }) => {
199 .forEach(dataKey => {
200 const label = data[ dataKey ]
202 hashToPopulate.push({
203 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
204 label: peertubeTranslate(label, translations)
208 if (sort === true) sortBy(hashToPopulate, 'label')
214 private loadServerLocale () {
215 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
217 // Default locale, nothing to translate
218 if (isDefaultLocale(completeLocale)) {
219 this.localeObservable = of({}).pipe(shareReplay())
223 this.localeObservable = this.http
224 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
228 private saveConfigLocally (config: ServerConfig) {
229 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
232 private loadConfigLocally () {
233 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
237 const parsed = JSON.parse(configString)
238 Object.assign(this.config, parsed)
240 console.error('Cannot parse config saved in local storage.', err)