]>
Commit | Line | Data |
---|---|---|
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, 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 | loadHTMLConfig () { | |
41 | try { | |
42 | return this.loadHTMLConfigLocally() | |
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.htmlConfig = config | |
80 | this.configLoaded = true | |
81 | }), | |
82 | tap(config => { | |
83 | if (this.configReset) { | |
84 | this.configReloaded.next(config) | |
85 | this.configReset = false | |
86 | } | |
87 | }), | |
88 | share() | |
89 | ) | |
90 | } | |
91 | ||
92 | return this.configObservable | |
93 | } | |
94 | ||
95 | getHTMLConfig () { | |
96 | return this.htmlConfig | |
97 | } | |
98 | ||
99 | getVideoCategories () { | |
100 | if (!this.videoCategoriesObservable) { | |
101 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
102 | } | |
103 | ||
104 | return this.videoCategoriesObservable.pipe(first()) | |
105 | } | |
106 | ||
107 | getVideoLicences () { | |
108 | if (!this.videoLicensesObservable) { | |
109 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
110 | } | |
111 | ||
112 | return this.videoLicensesObservable.pipe(first()) | |
113 | } | |
114 | ||
115 | getVideoLanguages () { | |
116 | if (!this.videoLanguagesObservable) { | |
117 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
118 | } | |
119 | ||
120 | return this.videoLanguagesObservable.pipe(first()) | |
121 | } | |
122 | ||
123 | getVideoPrivacies () { | |
124 | if (!this.videoPrivaciesObservable) { | |
125 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
126 | } | |
127 | ||
128 | return this.videoPrivaciesObservable.pipe(first()) | |
129 | } | |
130 | ||
131 | getVideoPlaylistPrivacies () { | |
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()) | |
154 | } | |
155 | ||
156 | getServerStats () { | |
157 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
158 | } | |
159 | ||
160 | private loadAttributeEnum <T extends string | number> ( | |
161 | baseUrl: string, | |
162 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
163 | sort = false | |
164 | ) { | |
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 }) => { | |
172 | const hashToPopulate: VideoConstant<T>[] = Object.keys(data) | |
173 | .map(dataKey => { | |
174 | const label = data[dataKey] | |
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 | }) | |
185 | ||
186 | if (sort === true) sortBy(hashToPopulate, 'label') | |
187 | ||
188 | return hashToPopulate | |
189 | }), | |
190 | shareReplay() | |
191 | ) | |
192 | } | |
193 | ||
194 | private loadHTMLConfigLocally () { | |
195 | const configString = window['PeerTubeServerConfig'] | |
196 | if (!configString) { | |
197 | throw new Error('Could not find PeerTubeServerConfig in HTML') | |
198 | } | |
199 | ||
200 | this.htmlConfig = JSON.parse(configString) | |
201 | } | |
202 | } |