]>
Commit | Line | Data |
---|---|---|
5fb2e288 | 1 | import { Observable, of, Subject } from 'rxjs' |
ba430d75 | 2 | import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators' |
db7af09b | 3 | import { HttpClient } from '@angular/common/http' |
7ce44a74 | 4 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
4504f09f | 5 | import { getDevLocale, isOnDevLocale, sortBy } from '@app/helpers' |
bd45d503 | 6 | import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n' |
9df52d66 | 7 | import { HTMLServerConfig, ServerConfig, ServerStats, VideoConstant } from '@shared/models' |
5fb2e288 | 8 | import { environment } from '../../../environments/environment' |
db7af09b C |
9 | |
10 | @Injectable() | |
11 | export 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 | ||
2989628b | 40 | loadHTMLConfig () { |
8e08d415 | 41 | try { |
2989628b | 42 | return this.loadHTMLConfigLocally() |
8e08d415 C |
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 | |
2989628b | 79 | this.htmlConfig = config |
017fbe18 C |
80 | this.configLoaded = true |
81 | }), | |
72c33e71 | 82 | tap(config => { |
ba430d75 | 83 | if (this.configReset) { |
72c33e71 | 84 | this.configReloaded.next(config) |
ba430d75 C |
85 | this.configReset = false |
86 | } | |
87 | }), | |
88 | share() | |
89 | ) | |
90 | } | |
830b4faf | 91 | |
ba430d75 | 92 | return this.configObservable |
fd45e8f4 C |
93 | } |
94 | ||
2989628b C |
95 | getHTMLConfig () { |
96 | return this.htmlConfig | |
dbdf2d51 C |
97 | } |
98 | ||
db7af09b | 99 | getVideoCategories () { |
ba430d75 C |
100 | if (!this.videoCategoriesObservable) { |
101 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
102 | } | |
103 | ||
104 | return this.videoCategoriesObservable.pipe(first()) | |
db7af09b C |
105 | } |
106 | ||
107 | getVideoLicences () { | |
ba430d75 C |
108 | if (!this.videoLicensesObservable) { |
109 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
110 | } | |
111 | ||
112 | return this.videoLicensesObservable.pipe(first()) | |
db7af09b C |
113 | } |
114 | ||
115 | getVideoLanguages () { | |
ba430d75 C |
116 | if (!this.videoLanguagesObservable) { |
117 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
118 | } | |
119 | ||
120 | return this.videoLanguagesObservable.pipe(first()) | |
db7af09b C |
121 | } |
122 | ||
fd45e8f4 | 123 | getVideoPrivacies () { |
ba430d75 C |
124 | if (!this.videoPrivaciesObservable) { |
125 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
126 | } | |
127 | ||
128 | return this.videoPrivaciesObservable.pipe(first()) | |
fd45e8f4 C |
129 | } |
130 | ||
830b4faf | 131 | getVideoPlaylistPrivacies () { |
ba430d75 C |
132 | if (!this.videoPlaylistPrivaciesObservable) { |
133 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
134 | } | |
135 | ||
136 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
137 | } | |
138 | ||
139 | getServerLocale () { | |
140 | if (!this.localeObservable) { | |
141 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
142 | ||
143 | // Default locale, nothing to translate | |
144 | if (isDefaultLocale(completeLocale)) { | |
145 | this.localeObservable = of({}).pipe(shareReplay()) | |
146 | } else { | |
147 | this.localeObservable = this.http | |
148 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
149 | .pipe(shareReplay()) | |
150 | } | |
151 | } | |
152 | ||
153 | return this.localeObservable.pipe(first()) | |
830b4faf C |
154 | } |
155 | ||
b764380a C |
156 | getServerStats () { |
157 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
158 | } | |
159 | ||
ba430d75 | 160 | private loadAttributeEnum <T extends string | number> ( |
830b4faf | 161 | baseUrl: string, |
fd45e8f4 | 162 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
3580fc00 | 163 | sort = false |
fd45e8f4 | 164 | ) { |
ba430d75 C |
165 | return this.getServerLocale() |
166 | .pipe( | |
167 | switchMap(translations => { | |
168 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
169 | .pipe(map(data => ({ data, translations }))) | |
170 | }), | |
171 | map(({ data, translations }) => { | |
111fdc26 C |
172 | const hashToPopulate: VideoConstant<T>[] = Object.keys(data) |
173 | .map(dataKey => { | |
9df52d66 | 174 | const label = data[dataKey] |
111fdc26 C |
175 | |
176 | const id = attributeName === 'languages' | |
177 | ? dataKey as T | |
178 | : parseInt(dataKey, 10) as T | |
179 | ||
180 | return { | |
181 | id, | |
182 | label: peertubeTranslate(label, translations) | |
183 | } | |
184 | }) | |
ba430d75 C |
185 | |
186 | if (sort === true) sortBy(hashToPopulate, 'label') | |
7ce44a74 | 187 | |
ba430d75 C |
188 | return hashToPopulate |
189 | }), | |
190 | shareReplay() | |
191 | ) | |
db7af09b | 192 | } |
36f9424f | 193 | |
2989628b | 194 | private loadHTMLConfigLocally () { |
aea0b0e7 | 195 | const configString = window['PeerTubeServerConfig'] |
8e08d415 C |
196 | if (!configString) { |
197 | throw new Error('Could not find PeerTubeServerConfig in HTML') | |
36f9424f | 198 | } |
8e08d415 | 199 | |
2989628b | 200 | this.htmlConfig = JSON.parse(configString) |
36f9424f | 201 | } |
db7af09b | 202 | } |