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