]>
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 { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | |
7 | import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n' | |
8 | import { SearchTargetType, ServerConfig, ServerStats, VideoConstant } from '@shared/models' | |
9 | import { environment } from '../../../environments/environment' | |
10 | ||
11 | @Injectable() | |
12 | export class ServerService { | |
13 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' | |
14 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
15 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | |
16 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' | |
17 | private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats' | |
18 | ||
19 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' | |
20 | ||
21 | configReloaded = new Subject<ServerConfig>() | |
22 | ||
23 | private localeObservable: Observable<any> | |
24 | private videoLicensesObservable: Observable<VideoConstant<number>[]> | |
25 | private videoCategoriesObservable: Observable<VideoConstant<number>[]> | |
26 | private videoPrivaciesObservable: Observable<VideoConstant<number>[]> | |
27 | private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]> | |
28 | private videoLanguagesObservable: Observable<VideoConstant<string>[]> | |
29 | private configObservable: Observable<ServerConfig> | |
30 | ||
31 | private configReset = false | |
32 | ||
33 | private configLoaded = false | |
34 | private config: ServerConfig = { | |
35 | instance: { | |
36 | name: 'PeerTube', | |
37 | shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' + | |
38 | 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.', | |
39 | defaultClientRoute: '', | |
40 | isNSFW: false, | |
41 | defaultNSFWPolicy: 'do_not_list' as 'do_not_list', | |
42 | customizations: { | |
43 | javascript: '', | |
44 | css: '' | |
45 | } | |
46 | }, | |
47 | plugin: { | |
48 | registered: [], | |
49 | registeredExternalAuths: [], | |
50 | registeredIdAndPassAuths: [] | |
51 | }, | |
52 | theme: { | |
53 | registered: [], | |
54 | default: 'default' | |
55 | }, | |
56 | email: { | |
57 | enabled: false | |
58 | }, | |
59 | contactForm: { | |
60 | enabled: false | |
61 | }, | |
62 | serverVersion: 'Unknown', | |
63 | signup: { | |
64 | allowed: false, | |
65 | allowedForCurrentIP: false, | |
66 | requiresEmailVerification: false | |
67 | }, | |
68 | transcoding: { | |
69 | enabledResolutions: [], | |
70 | hls: { | |
71 | enabled: false | |
72 | }, | |
73 | webtorrent: { | |
74 | enabled: true | |
75 | } | |
76 | }, | |
77 | live: { | |
78 | enabled: false, | |
79 | allowReplay: true, | |
80 | maxDuration: null, | |
81 | maxInstanceLives: -1, | |
82 | maxUserLives: -1, | |
83 | transcoding: { | |
84 | enabled: false, | |
85 | enabledResolutions: [] | |
86 | }, | |
87 | rtmp: { | |
88 | port: 1935 | |
89 | } | |
90 | }, | |
91 | avatar: { | |
92 | file: { | |
93 | size: { max: 0 }, | |
94 | extensions: [] | |
95 | } | |
96 | }, | |
97 | video: { | |
98 | image: { | |
99 | size: { max: 0 }, | |
100 | extensions: [] | |
101 | }, | |
102 | file: { | |
103 | extensions: [] | |
104 | } | |
105 | }, | |
106 | videoCaption: { | |
107 | file: { | |
108 | size: { max: 0 }, | |
109 | extensions: [] | |
110 | } | |
111 | }, | |
112 | user: { | |
113 | videoQuota: -1, | |
114 | videoQuotaDaily: -1 | |
115 | }, | |
116 | import: { | |
117 | videos: { | |
118 | http: { | |
119 | enabled: false | |
120 | }, | |
121 | torrent: { | |
122 | enabled: false | |
123 | } | |
124 | } | |
125 | }, | |
126 | trending: { | |
127 | videos: { | |
128 | intervalDays: 0 | |
129 | } | |
130 | }, | |
131 | autoBlacklist: { | |
132 | videos: { | |
133 | ofUsers: { | |
134 | enabled: false | |
135 | } | |
136 | } | |
137 | }, | |
138 | tracker: { | |
139 | enabled: true | |
140 | }, | |
141 | followings: { | |
142 | instance: { | |
143 | autoFollowIndex: { | |
144 | indexUrl: 'https://instances.joinpeertube.org' | |
145 | } | |
146 | } | |
147 | }, | |
148 | broadcastMessage: { | |
149 | enabled: false, | |
150 | message: '', | |
151 | level: 'info', | |
152 | dismissable: false | |
153 | }, | |
154 | search: { | |
155 | remoteUri: { | |
156 | users: true, | |
157 | anonymous: false | |
158 | }, | |
159 | searchIndex: { | |
160 | enabled: false, | |
161 | url: '', | |
162 | disableLocalSearch: false, | |
163 | isDefaultSearch: false | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | constructor ( | |
169 | private http: HttpClient, | |
170 | @Inject(LOCALE_ID) private localeId: string | |
171 | ) { | |
172 | this.loadConfigLocally() | |
173 | } | |
174 | ||
175 | getServerVersionAndCommit () { | |
176 | const serverVersion = this.config.serverVersion | |
177 | const commit = this.config.serverCommit || '' | |
178 | ||
179 | let result = serverVersion | |
180 | if (commit) result += '...' + commit | |
181 | ||
182 | return result | |
183 | } | |
184 | ||
185 | resetConfig () { | |
186 | this.configLoaded = false | |
187 | this.configReset = true | |
188 | ||
189 | // Notify config update | |
190 | this.getConfig().subscribe(() => { | |
191 | // empty, to fire a reset config event | |
192 | }) | |
193 | } | |
194 | ||
195 | getConfig () { | |
196 | if (this.configLoaded) return of(this.config) | |
197 | ||
198 | if (!this.configObservable) { | |
199 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
200 | .pipe( | |
201 | tap(config => this.saveConfigLocally(config)), | |
202 | tap(config => { | |
203 | this.config = config | |
204 | this.configLoaded = true | |
205 | }), | |
206 | tap(config => { | |
207 | if (this.configReset) { | |
208 | this.configReloaded.next(config) | |
209 | this.configReset = false | |
210 | } | |
211 | }), | |
212 | share() | |
213 | ) | |
214 | } | |
215 | ||
216 | return this.configObservable | |
217 | } | |
218 | ||
219 | getTmpConfig () { | |
220 | return this.config | |
221 | } | |
222 | ||
223 | getVideoCategories () { | |
224 | if (!this.videoCategoriesObservable) { | |
225 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
226 | } | |
227 | ||
228 | return this.videoCategoriesObservable.pipe(first()) | |
229 | } | |
230 | ||
231 | getVideoLicences () { | |
232 | if (!this.videoLicensesObservable) { | |
233 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
234 | } | |
235 | ||
236 | return this.videoLicensesObservable.pipe(first()) | |
237 | } | |
238 | ||
239 | getVideoLanguages () { | |
240 | if (!this.videoLanguagesObservable) { | |
241 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
242 | } | |
243 | ||
244 | return this.videoLanguagesObservable.pipe(first()) | |
245 | } | |
246 | ||
247 | getVideoPrivacies () { | |
248 | if (!this.videoPrivaciesObservable) { | |
249 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
250 | } | |
251 | ||
252 | return this.videoPrivaciesObservable.pipe(first()) | |
253 | } | |
254 | ||
255 | getVideoPlaylistPrivacies () { | |
256 | if (!this.videoPlaylistPrivaciesObservable) { | |
257 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
258 | } | |
259 | ||
260 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
261 | } | |
262 | ||
263 | getServerLocale () { | |
264 | if (!this.localeObservable) { | |
265 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
266 | ||
267 | // Default locale, nothing to translate | |
268 | if (isDefaultLocale(completeLocale)) { | |
269 | this.localeObservable = of({}).pipe(shareReplay()) | |
270 | } else { | |
271 | this.localeObservable = this.http | |
272 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
273 | .pipe(shareReplay()) | |
274 | } | |
275 | } | |
276 | ||
277 | return this.localeObservable.pipe(first()) | |
278 | } | |
279 | ||
280 | getServerStats () { | |
281 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
282 | } | |
283 | ||
284 | getDefaultSearchTarget (): Promise<SearchTargetType> { | |
285 | return this.getConfig().pipe( | |
286 | map(config => { | |
287 | const searchIndexConfig = config.search.searchIndex | |
288 | ||
289 | if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) { | |
290 | return 'search-index' | |
291 | } | |
292 | ||
293 | return 'local' | |
294 | }) | |
295 | ).toPromise() | |
296 | } | |
297 | ||
298 | private loadAttributeEnum <T extends string | number> ( | |
299 | baseUrl: string, | |
300 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
301 | sort = false | |
302 | ) { | |
303 | return this.getServerLocale() | |
304 | .pipe( | |
305 | switchMap(translations => { | |
306 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
307 | .pipe(map(data => ({ data, translations }))) | |
308 | }), | |
309 | map(({ data, translations }) => { | |
310 | const hashToPopulate: VideoConstant<T>[] = Object.keys(data) | |
311 | .map(dataKey => { | |
312 | const label = data[ dataKey ] | |
313 | ||
314 | const id = attributeName === 'languages' | |
315 | ? dataKey as T | |
316 | : parseInt(dataKey, 10) as T | |
317 | ||
318 | return { | |
319 | id, | |
320 | label: peertubeTranslate(label, translations) | |
321 | } | |
322 | }) | |
323 | ||
324 | if (sort === true) sortBy(hashToPopulate, 'label') | |
325 | ||
326 | return hashToPopulate | |
327 | }), | |
328 | shareReplay() | |
329 | ) | |
330 | } | |
331 | ||
332 | private saveConfigLocally (config: ServerConfig) { | |
333 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) | |
334 | } | |
335 | ||
336 | private loadConfigLocally () { | |
337 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) | |
338 | ||
339 | if (configString) { | |
340 | try { | |
341 | const parsed = JSON.parse(configString) | |
342 | Object.assign(this.config, parsed) | |
343 | } catch (err) { | |
344 | console.error('Cannot parse config saved in local storage.', err) | |
345 | } | |
346 | } | |
347 | } | |
348 | } |