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: [],
121 private videoCategories: Array<VideoConstant<number>> = []
122 private videoLicences: Array<VideoConstant<number>> = []
123 private videoLanguages: Array<VideoConstant<string>> = []
124 private videoPrivacies: Array<VideoConstant<VideoPrivacy>> = []
125 private videoPlaylistPrivacies: Array<VideoConstant<VideoPlaylistPrivacy>> = []
128 private http: HttpClient,
129 @Inject(LOCALE_ID) private localeId: string
131 this.loadServerLocale()
132 this.loadConfigLocally()
136 this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
137 .pipe(tap(this.saveConfigLocally))
141 this.configLoaded.next(true)
145 loadVideoCategories () {
146 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'categories', this.videoCategories, this.videoCategoriesLoaded, true)
149 loadVideoLicences () {
150 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'licences', this.videoLicences, this.videoLicencesLoaded)
153 loadVideoLanguages () {
154 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'languages', this.videoLanguages, this.videoLanguagesLoaded, true)
157 loadVideoPrivacies () {
158 return this.loadAttributeEnum(ServerService.BASE_VIDEO_URL, 'privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
161 loadVideoPlaylistPrivacies () {
162 return this.loadAttributeEnum(
163 ServerService.BASE_VIDEO_PLAYLIST_URL,
165 this.videoPlaylistPrivacies,
166 this.videoPlaylistPrivaciesLoaded
171 return cloneDeep(this.config)
174 getVideoCategories () {
175 return cloneDeep(this.videoCategories)
178 getVideoLicences () {
179 return cloneDeep(this.videoLicences)
182 getVideoLanguages () {
183 return cloneDeep(this.videoLanguages)
186 getVideoPrivacies () {
187 return cloneDeep(this.videoPrivacies)
190 getVideoPlaylistPrivacies () {
191 return cloneDeep(this.videoPlaylistPrivacies)
194 private loadAttributeEnum (
196 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
197 hashToPopulate: VideoConstant<string | number>[],
198 notifier: ReplaySubject<boolean>,
201 this.localeObservable
203 switchMap(translations => {
204 return this.http.get<{ [id: string]: string }>(baseUrl + attributeName)
205 .pipe(map(data => ({ data, translations })))
208 .subscribe(({ data, translations }) => {
210 .forEach(dataKey => {
211 const label = data[ dataKey ]
213 hashToPopulate.push({
214 id: attributeName === 'languages' ? dataKey : parseInt(dataKey, 10),
215 label: peertubeTranslate(label, translations)
219 if (sort === true) sortBy(hashToPopulate, 'label')
225 private loadServerLocale () {
226 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
228 // Default locale, nothing to translate
229 if (isDefaultLocale(completeLocale)) {
230 this.localeObservable = of({}).pipe(shareReplay())
234 this.localeObservable = this.http
235 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
239 private saveConfigLocally (config: ServerConfig) {
240 peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
243 private loadConfigLocally () {
244 const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
248 const parsed = JSON.parse(configString)
249 Object.assign(this.config, parsed)
251 console.error('Cannot parse config saved in local storage.', err)