]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/server/server.service.ts
Load server config on app init
[github/Chocobozzz/PeerTube.git] / client / src / app / core / server / server.service.ts
1 import { Observable, of, Subject } from 'rxjs'
2 import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
3 import { HttpClient } from '@angular/common/http'
4 import { Inject, Injectable, LOCALE_ID } from '@angular/core'
5 import { getDevLocale, isOnDevLocale, sortBy } from '@app/helpers'
6 import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
7 import { HTMLServerConfig, SearchTargetType, ServerConfig, ServerStats, VideoConstant } from '@shared/models'
8 import { environment } from '../../../environments/environment'
9
10 @Injectable()
11 export class ServerService {
12 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
13 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
14 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
15 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
16 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
17
18 configReloaded = new Subject<ServerConfig>()
19
20 private localeObservable: Observable<any>
21 private videoLicensesObservable: Observable<VideoConstant<number>[]>
22 private videoCategoriesObservable: Observable<VideoConstant<number>[]>
23 private videoPrivaciesObservable: Observable<VideoConstant<number>[]>
24 private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]>
25 private videoLanguagesObservable: Observable<VideoConstant<string>[]>
26 private configObservable: Observable<ServerConfig>
27
28 private configReset = false
29
30 private configLoaded = false
31 private config: ServerConfig
32 private htmlConfig: HTMLServerConfig
33
34 constructor (
35 private http: HttpClient,
36 @Inject(LOCALE_ID) private localeId: string
37 ) {
38 }
39
40 loadConfig () {
41 try {
42 return this.loadConfigLocally()
43 } catch (err) {
44 // Expected in dev mode since we can't inject the config in the HTML
45 if (environment.production !== false) {
46 console.error('Cannot load config locally. Fallback to API.')
47 }
48
49 return this.getConfig()
50 }
51 }
52
53 getServerVersionAndCommit () {
54 const serverVersion = this.config.serverVersion
55 const commit = this.config.serverCommit || ''
56
57 let result = serverVersion
58 if (commit) result += '...' + commit
59
60 return result
61 }
62
63 resetConfig () {
64 this.configLoaded = false
65 this.configReset = true
66
67 // Notify config update
68 return this.getConfig()
69 }
70
71 getConfig () {
72 if (this.configLoaded) return of(this.config)
73
74 if (!this.configObservable) {
75 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
76 .pipe(
77 tap(config => {
78 this.config = config
79 this.configLoaded = true
80 }),
81 tap(config => {
82 if (this.configReset) {
83 this.configReloaded.next(config)
84 this.configReset = false
85 }
86 }),
87 share()
88 )
89 }
90
91 return this.configObservable
92 }
93
94 getTmpConfig () {
95 return this.config
96 }
97
98 getVideoCategories () {
99 if (!this.videoCategoriesObservable) {
100 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
101 }
102
103 return this.videoCategoriesObservable.pipe(first())
104 }
105
106 getVideoLicences () {
107 if (!this.videoLicensesObservable) {
108 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
109 }
110
111 return this.videoLicensesObservable.pipe(first())
112 }
113
114 getVideoLanguages () {
115 if (!this.videoLanguagesObservable) {
116 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
117 }
118
119 return this.videoLanguagesObservable.pipe(first())
120 }
121
122 getVideoPrivacies () {
123 if (!this.videoPrivaciesObservable) {
124 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
125 }
126
127 return this.videoPrivaciesObservable.pipe(first())
128 }
129
130 getVideoPlaylistPrivacies () {
131 if (!this.videoPlaylistPrivaciesObservable) {
132 this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies')
133 }
134
135 return this.videoPlaylistPrivaciesObservable.pipe(first())
136 }
137
138 getServerLocale () {
139 if (!this.localeObservable) {
140 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
141
142 // Default locale, nothing to translate
143 if (isDefaultLocale(completeLocale)) {
144 this.localeObservable = of({}).pipe(shareReplay())
145 } else {
146 this.localeObservable = this.http
147 .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json')
148 .pipe(shareReplay())
149 }
150 }
151
152 return this.localeObservable.pipe(first())
153 }
154
155 getServerStats () {
156 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
157 }
158
159 getDefaultSearchTarget (): Promise<SearchTargetType> {
160 return this.getConfig().pipe(
161 map(config => {
162 const searchIndexConfig = config.search.searchIndex
163
164 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
165 return 'search-index'
166 }
167
168 return 'local'
169 })
170 ).toPromise()
171 }
172
173 private loadAttributeEnum <T extends string | number> (
174 baseUrl: string,
175 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
176 sort = false
177 ) {
178 return this.getServerLocale()
179 .pipe(
180 switchMap(translations => {
181 return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName)
182 .pipe(map(data => ({ data, translations })))
183 }),
184 map(({ data, translations }) => {
185 const hashToPopulate: VideoConstant<T>[] = Object.keys(data)
186 .map(dataKey => {
187 const label = data[ dataKey ]
188
189 const id = attributeName === 'languages'
190 ? dataKey as T
191 : parseInt(dataKey, 10) as T
192
193 return {
194 id,
195 label: peertubeTranslate(label, translations)
196 }
197 })
198
199 if (sort === true) sortBy(hashToPopulate, 'label')
200
201 return hashToPopulate
202 }),
203 shareReplay()
204 )
205 }
206
207 private loadConfigLocally () {
208 const configString = window['PeerTubeServerConfig']
209 if (!configString) {
210 throw new Error('Could not find PeerTubeServerConfig in HTML')
211 }
212
213 this.config = JSON.parse(configString)
214 }
215 }