]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
5fb2e288 1import { Observable, of, Subject } from 'rxjs'
ba430d75 2import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators'
db7af09b 3import { HttpClient } from '@angular/common/http'
7ce44a74 4import { Inject, Injectable, LOCALE_ID } from '@angular/core'
4504f09f 5import { getDevLocale, isOnDevLocale, sortBy } from '@app/helpers'
bd45d503 6import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
8e08d415 7import { HTMLServerConfig, SearchTargetType, ServerConfig, ServerStats, VideoConstant } from '@shared/models'
5fb2e288 8import { environment } from '../../../environments/environment'
db7af09b
C
9
10@Injectable()
11export class ServerService {
63c4db6d
C
12 private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
13 private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
830b4faf 14 private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
7ce44a74 15 private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/'
b764380a
C
16 private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats'
17
72c33e71 18 configReloaded = new Subject<ServerConfig>()
baeefe22 19
ba430d75
C
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
8e08d415
C
31 private config: ServerConfig
32 private htmlConfig: HTMLServerConfig
db7af09b 33
7ce44a74
C
34 constructor (
35 private http: HttpClient,
36 @Inject(LOCALE_ID) private localeId: string
37 ) {
8e08d415
C
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 }
36f9424f 51 }
db7af09b 52
ba430d75
C
53 getServerVersionAndCommit () {
54 const serverVersion = this.config.serverVersion
55 const commit = this.config.serverCommit || ''
00b5556c 56
ba430d75
C
57 let result = serverVersion
58 if (commit) result += '...' + commit
db7af09b 59
ba430d75 60 return result
db7af09b
C
61 }
62
ba430d75
C
63 resetConfig () {
64 this.configLoaded = false
65 this.configReset = true
72c33e71
C
66
67 // Notify config update
2539932e 68 return this.getConfig()
db7af09b
C
69 }
70
ba430d75
C
71 getConfig () {
72 if (this.configLoaded) return of(this.config)
db7af09b 73
ba430d75
C
74 if (!this.configObservable) {
75 this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
76 .pipe(
017fbe18
C
77 tap(config => {
78 this.config = config
79 this.configLoaded = true
80 }),
72c33e71 81 tap(config => {
ba430d75 82 if (this.configReset) {
72c33e71 83 this.configReloaded.next(config)
ba430d75
C
84 this.configReset = false
85 }
86 }),
87 share()
88 )
89 }
830b4faf 90
ba430d75 91 return this.configObservable
fd45e8f4
C
92 }
93
ba430d75
C
94 getTmpConfig () {
95 return this.config
dbdf2d51
C
96 }
97
db7af09b 98 getVideoCategories () {
ba430d75
C
99 if (!this.videoCategoriesObservable) {
100 this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true)
101 }
102
103 return this.videoCategoriesObservable.pipe(first())
db7af09b
C
104 }
105
106 getVideoLicences () {
ba430d75
C
107 if (!this.videoLicensesObservable) {
108 this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences')
109 }
110
111 return this.videoLicensesObservable.pipe(first())
db7af09b
C
112 }
113
114 getVideoLanguages () {
ba430d75
C
115 if (!this.videoLanguagesObservable) {
116 this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true)
117 }
118
119 return this.videoLanguagesObservable.pipe(first())
db7af09b
C
120 }
121
fd45e8f4 122 getVideoPrivacies () {
ba430d75
C
123 if (!this.videoPrivaciesObservable) {
124 this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies')
125 }
126
127 return this.videoPrivaciesObservable.pipe(first())
fd45e8f4
C
128 }
129
830b4faf 130 getVideoPlaylistPrivacies () {
ba430d75
C
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())
830b4faf
C
153 }
154
b764380a
C
155 getServerStats () {
156 return this.http.get<ServerStats>(ServerService.BASE_STATS_URL)
157 }
158
5fb2e288
C
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
ba430d75 173 private loadAttributeEnum <T extends string | number> (
830b4faf 174 baseUrl: string,
fd45e8f4 175 attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
3580fc00 176 sort = false
fd45e8f4 177 ) {
ba430d75
C
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 }) => {
111fdc26
C
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 })
ba430d75
C
198
199 if (sort === true) sortBy(hashToPopulate, 'label')
7ce44a74 200
ba430d75
C
201 return hashToPopulate
202 }),
203 shareReplay()
204 )
db7af09b 205 }
36f9424f 206
36f9424f 207 private loadConfigLocally () {
aea0b0e7 208 const configString = window['PeerTubeServerConfig']
8e08d415
C
209 if (!configString) {
210 throw new Error('Could not find PeerTubeServerConfig in HTML')
36f9424f 211 }
8e08d415
C
212
213 this.config = JSON.parse(configString)
36f9424f 214 }
db7af09b 215}