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: [],
113 private videoCategories: Array<VideoConstant<number>> = []
114 private videoLicences: Array<VideoConstant<number>> = []
115 private videoLanguages: Array<VideoConstant<string>> = []
116 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
117 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
120 private http: HttpClient,
121 @Inject(LOCALE_ID) private localeId: string
123 this.loadServerLocale()
124 this.loadConfigLocally()
128 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
129 .pipe(tap(this.saveConfigLocally))
133 this.configLoaded.next(true)
137 loadVideoCategories () {
138 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
141 loadVideoLicences () {
142 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
145 loadVideoLanguages () {
146 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
149 loadVideoPrivacies () {
150 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
153 loadVideoPlaylistPrivacies () {
154 return this.loadAttributeEnum(
155 ServerService.BASE_VIDEO_PLAYLIST_URL,
157 this.videoPlaylistPrivacies,
158 this.videoPlaylistPrivaciesLoaded
166 getVideoCategories () {
167 return this.videoCategories
170 getVideoLicences () {
171 return this.videoLicences
174 getVideoLanguages () {
175 return this.videoLanguages
178 getVideoPrivacies () {
179 return this.videoPrivacies
182 getVideoPlaylistPrivacies () {
183 return this.videoPlaylistPrivacies
186 private loadAttributeEnum (
188 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
189 hashToPopulate: VideoConstant<string | number>[],
190 notifier: ReplaySubject<boolean>,
193 this.localeObservable
195 switchMap(translations => {
196 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
197 .pipe(map(data => ({ data, translations })))
200 .subscribe(({ data, translations }) => {
202 .forEach(dataKey => {
203 const label = data[ dataKey ]
205 hashToPopulate.push({
206 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
207 label: peertubeTranslate(label, translations)
211 if (sort === true) sortBy(hashToPopulate, 'label')
217 private loadServerLocale () {
218 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
220 // Default locale, nothing to translate
221 if (isDefaultLocale(completeLocale)) {
222 this.localeObservable = of({}).pipe(shareReplay())
226 this.localeObservable = this.http
227 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
231 private saveConfigLocally (config: ServerConfig) {
232 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
235 private loadConfigLocally () {
236 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
240 const parsed = JSON.parse(configString)
241 Object.assign(this.config, parsed)
243 console.error('Cannot parse config saved in local storage.', err)