PeerTube/client/src/app/core/server/server.service.ts

197 lines
5.7 KiB
TypeScript
Raw Normal View History

2018-06-06 16:46:42 +02:00
import { map, share, switchMap, tap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
2018-06-06 16:46:42 +02:00
import { Inject, Injectable, LOCALE_ID } from '@angular/core'
import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
2018-07-25 15:11:25 +02:00
import { Observable, of, ReplaySubject } from 'rxjs'
2018-06-06 17:37:13 +02:00
import { getCompleteLocale, ServerConfig } from '../../../../../shared'
2018-02-28 18:04:46 +01:00
import { About } from '../../../../../shared/models/server/about.model'
2017-12-11 17:36:46 +01:00
import { environment } from '../../../environments/environment'
2018-07-25 15:11:25 +02:00
import { VideoConstant } from '../../../../../shared/models/videos'
2018-06-06 17:37:13 +02:00
import { isDefaultLocale } from '../../../../../shared/models/i18n'
import { getDevLocale, isOnDevLocale, peertubeTranslate } from '@app/shared/i18n/i18n-utils'
2018-07-25 15:11:25 +02:00
import { sortBy } from '@app/shared/misc/utils'
@Injectable()
export class ServerService {
2017-12-11 17:36:46 +01:00
private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
2018-06-06 16:46:42 +02:00
private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
2018-01-31 17:47:36 +01:00
private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'
configLoaded = new ReplaySubject<boolean>(1)
2017-12-07 17:22:44 +01:00
videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
videoCategoriesLoaded = new ReplaySubject<boolean>(1)
videoLicencesLoaded = new ReplaySubject<boolean>(1)
videoLanguagesLoaded = new ReplaySubject<boolean>(1)
2018-06-06 16:46:42 +02:00
localeObservable: Observable<any>
2017-12-07 17:22:44 +01:00
private config: ServerConfig = {
2018-01-31 17:47:36 +01:00
instance: {
name: 'PeerTube',
2018-03-22 14:13:30 +01:00
shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' +
'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.',
2018-03-01 13:57:29 +01:00
defaultClientRoute: '',
defaultNSFWPolicy: 'do_not_list' as 'do_not_list',
customizations: {
javascript: '',
css: ''
}
2018-01-31 17:47:36 +01:00
},
2018-01-31 10:19:34 +01:00
serverVersion: 'Unknown',
signup: {
allowed: false,
allowedForCurrentIP: false
},
transcoding: {
enabledResolutions: []
2018-01-03 11:10:40 +01:00
},
avatar: {
file: {
size: { max: 0 },
extensions: []
}
},
video: {
image: {
size: { max: 0 },
extensions: []
},
2018-01-03 11:10:40 +01:00
file: {
extensions: []
}
},
2018-07-12 19:02:00 +02:00
videoCaption: {
file: {
size: { max: 0 },
extensions: []
}
},
user: {
videoQuota: -1
}
}
2018-07-16 17:36:42 +02:00
private videoCategories: Array<VideoConstant<string>> = []
private videoLicences: Array<VideoConstant<string>> = []
2018-04-23 14:39:52 +02:00
private videoLanguages: Array<VideoConstant<string>> = []
2018-07-16 17:36:42 +02:00
private videoPrivacies: Array<VideoConstant<string>> = []
2018-06-06 16:46:42 +02:00
constructor (
private http: HttpClient,
@Inject(LOCALE_ID) private localeId: string
) {
this.loadServerLocale()
2018-06-06 17:37:13 +02:00
this.loadConfigLocally()
2018-01-31 17:47:36 +01:00
}
loadConfig () {
this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
2018-05-15 11:55:51 +02:00
.pipe(tap(this.saveConfigLocally))
.subscribe(data => {
this.config = data
2018-05-15 11:55:51 +02:00
this.configLoaded.next(true)
})
}
loadVideoCategories () {
2018-02-20 09:50:44 +01:00
return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
}
loadVideoLicences () {
2017-12-07 17:22:44 +01:00
return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
}
loadVideoLanguages () {
2018-02-20 09:50:44 +01:00
return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
}
2017-10-31 11:52:52 +01:00
loadVideoPrivacies () {
2017-12-07 17:22:44 +01:00
return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
2017-10-31 11:52:52 +01:00
}
getConfig () {
return this.config
}
getVideoCategories () {
return this.videoCategories
}
getVideoLicences () {
return this.videoLicences
}
getVideoLanguages () {
return this.videoLanguages
}
2017-10-31 11:52:52 +01:00
getVideoPrivacies () {
return this.videoPrivacies
}
2018-01-31 17:47:36 +01:00
getAbout () {
return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
}
2017-10-31 11:52:52 +01:00
private loadVideoAttributeEnum (
attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
2018-07-16 17:36:42 +02:00
hashToPopulate: VideoConstant<string>[],
2018-02-20 09:50:44 +01:00
notifier: ReplaySubject<boolean>,
sort = false
2017-10-31 11:52:52 +01:00
) {
2018-06-06 16:46:42 +02:00
this.localeObservable
.pipe(
switchMap(translations => {
return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
.pipe(map(data => ({ data, translations })))
})
)
.subscribe(({ data, translations }) => {
Object.keys(data)
.forEach(dataKey => {
const label = data[ dataKey ]
hashToPopulate.push({
id: dataKey,
label: peertubeTranslate(label, translations)
})
})
2018-07-25 15:11:25 +02:00
if (sort === true) sortBy(hashToPopulate, 'label')
2018-06-06 16:46:42 +02:00
notifier.next(true)
})
}
private loadServerLocale () {
2018-06-06 17:37:13 +02:00
const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
2018-06-06 16:46:42 +02:00
// Default locale, nothing to translate
2018-06-06 17:37:13 +02:00
if (isDefaultLocale(completeLocale)) {
this.localeObservable = of({}).pipe(share())
return
}
2018-06-06 16:46:42 +02:00
this.localeObservable = this.http
2018-06-06 17:37:13 +02:00
.get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
2018-06-06 16:46:42 +02:00
.pipe(share())
}
2018-01-31 17:47:36 +01:00
private saveConfigLocally (config: ServerConfig) {
peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
2018-01-31 17:47:36 +01:00
}
private loadConfigLocally () {
const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
2018-01-31 17:47:36 +01:00
if (configString) {
try {
const parsed = JSON.parse(configString)
Object.assign(this.config, parsed)
} catch (err) {
console.error('Cannot parse config saved in local storage.', err)
}
}
}
}