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