]>
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, | |
79 | transcoding: { | |
80 | enabled: false, | |
81 | enabledResolutions: [] | |
82 | } | |
83 | }, | |
01de67b9 C |
84 | avatar: { |
85 | file: { | |
86 | size: { max: 0 }, | |
87 | extensions: [] | |
88 | } | |
89 | }, | |
90 | video: { | |
6de36768 C |
91 | image: { |
92 | size: { max: 0 }, | |
93 | extensions: [] | |
94 | }, | |
01de67b9 C |
95 | file: { |
96 | extensions: [] | |
97 | } | |
1869c875 | 98 | }, |
40e87e9e C |
99 | videoCaption: { |
100 | file: { | |
101 | size: { max: 0 }, | |
102 | extensions: [] | |
103 | } | |
104 | }, | |
1869c875 | 105 | user: { |
bee0abff FA |
106 | videoQuota: -1, |
107 | videoQuotaDaily: -1 | |
5d08a6a7 C |
108 | }, |
109 | import: { | |
b2977eec | 110 | videos: { |
5d08a6a7 C |
111 | http: { |
112 | enabled: false | |
a84b8fa5 C |
113 | }, |
114 | torrent: { | |
115 | enabled: false | |
5d08a6a7 C |
116 | } |
117 | } | |
9b4b15f9 AB |
118 | }, |
119 | trending: { | |
120 | videos: { | |
121 | intervalDays: 0 | |
122 | } | |
7ccddd7b JM |
123 | }, |
124 | autoBlacklist: { | |
125 | videos: { | |
126 | ofUsers: { | |
127 | enabled: false | |
128 | } | |
129 | } | |
31b6ddf8 C |
130 | }, |
131 | tracker: { | |
132 | enabled: true | |
f24c8b14 RK |
133 | }, |
134 | followings: { | |
135 | instance: { | |
136 | autoFollowIndex: { | |
137 | indexUrl: 'https://instances.joinpeertube.org' | |
138 | } | |
139 | } | |
72c33e71 C |
140 | }, |
141 | broadcastMessage: { | |
142 | enabled: false, | |
143 | message: '', | |
144 | level: 'info', | |
145 | dismissable: false | |
5fb2e288 C |
146 | }, |
147 | search: { | |
148 | remoteUri: { | |
149 | users: true, | |
150 | anonymous: false | |
151 | }, | |
152 | searchIndex: { | |
153 | enabled: false, | |
154 | url: '', | |
155 | disableLocalSearch: false, | |
156 | isDefaultSearch: false | |
157 | } | |
db7af09b C |
158 | } |
159 | } | |
db7af09b | 160 | |
7ce44a74 C |
161 | constructor ( |
162 | private http: HttpClient, | |
163 | @Inject(LOCALE_ID) private localeId: string | |
164 | ) { | |
74b7c6d4 | 165 | this.loadConfigLocally() |
36f9424f | 166 | } |
db7af09b | 167 | |
ba430d75 C |
168 | getServerVersionAndCommit () { |
169 | const serverVersion = this.config.serverVersion | |
170 | const commit = this.config.serverCommit || '' | |
00b5556c | 171 | |
ba430d75 C |
172 | let result = serverVersion |
173 | if (commit) result += '...' + commit | |
db7af09b | 174 | |
ba430d75 | 175 | return result |
db7af09b C |
176 | } |
177 | ||
ba430d75 C |
178 | resetConfig () { |
179 | this.configLoaded = false | |
180 | this.configReset = true | |
72c33e71 C |
181 | |
182 | // Notify config update | |
183 | this.getConfig().subscribe(() => { | |
184 | // empty, to fire a reset config event | |
185 | }) | |
db7af09b C |
186 | } |
187 | ||
ba430d75 C |
188 | getConfig () { |
189 | if (this.configLoaded) return of(this.config) | |
db7af09b | 190 | |
ba430d75 C |
191 | if (!this.configObservable) { |
192 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
193 | .pipe( | |
017fbe18 C |
194 | tap(config => this.saveConfigLocally(config)), |
195 | tap(config => { | |
196 | this.config = config | |
197 | this.configLoaded = true | |
198 | }), | |
72c33e71 | 199 | tap(config => { |
ba430d75 | 200 | if (this.configReset) { |
72c33e71 | 201 | this.configReloaded.next(config) |
ba430d75 C |
202 | this.configReset = false |
203 | } | |
204 | }), | |
205 | share() | |
206 | ) | |
207 | } | |
830b4faf | 208 | |
ba430d75 | 209 | return this.configObservable |
fd45e8f4 C |
210 | } |
211 | ||
ba430d75 C |
212 | getTmpConfig () { |
213 | return this.config | |
dbdf2d51 C |
214 | } |
215 | ||
db7af09b | 216 | getVideoCategories () { |
ba430d75 C |
217 | if (!this.videoCategoriesObservable) { |
218 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
219 | } | |
220 | ||
221 | return this.videoCategoriesObservable.pipe(first()) | |
db7af09b C |
222 | } |
223 | ||
224 | getVideoLicences () { | |
ba430d75 C |
225 | if (!this.videoLicensesObservable) { |
226 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
227 | } | |
228 | ||
229 | return this.videoLicensesObservable.pipe(first()) | |
db7af09b C |
230 | } |
231 | ||
232 | getVideoLanguages () { | |
ba430d75 C |
233 | if (!this.videoLanguagesObservable) { |
234 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
235 | } | |
236 | ||
237 | return this.videoLanguagesObservable.pipe(first()) | |
db7af09b C |
238 | } |
239 | ||
fd45e8f4 | 240 | getVideoPrivacies () { |
ba430d75 C |
241 | if (!this.videoPrivaciesObservable) { |
242 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
243 | } | |
244 | ||
245 | return this.videoPrivaciesObservable.pipe(first()) | |
fd45e8f4 C |
246 | } |
247 | ||
830b4faf | 248 | getVideoPlaylistPrivacies () { |
ba430d75 C |
249 | if (!this.videoPlaylistPrivaciesObservable) { |
250 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
251 | } | |
252 | ||
253 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
254 | } | |
255 | ||
256 | getServerLocale () { | |
257 | if (!this.localeObservable) { | |
258 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
259 | ||
260 | // Default locale, nothing to translate | |
261 | if (isDefaultLocale(completeLocale)) { | |
262 | this.localeObservable = of({}).pipe(shareReplay()) | |
263 | } else { | |
264 | this.localeObservable = this.http | |
265 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
266 | .pipe(shareReplay()) | |
267 | } | |
268 | } | |
269 | ||
270 | return this.localeObservable.pipe(first()) | |
830b4faf C |
271 | } |
272 | ||
b764380a C |
273 | getServerStats () { |
274 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
275 | } | |
276 | ||
5fb2e288 C |
277 | getDefaultSearchTarget (): Promise<SearchTargetType> { |
278 | return this.getConfig().pipe( | |
279 | map(config => { | |
280 | const searchIndexConfig = config.search.searchIndex | |
281 | ||
282 | if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) { | |
283 | return 'search-index' | |
284 | } | |
285 | ||
286 | return 'local' | |
287 | }) | |
288 | ).toPromise() | |
289 | } | |
290 | ||
ba430d75 | 291 | private loadAttributeEnum <T extends string | number> ( |
830b4faf | 292 | baseUrl: string, |
fd45e8f4 | 293 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
3580fc00 | 294 | sort = false |
fd45e8f4 | 295 | ) { |
ba430d75 C |
296 | return this.getServerLocale() |
297 | .pipe( | |
298 | switchMap(translations => { | |
299 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
300 | .pipe(map(data => ({ data, translations }))) | |
301 | }), | |
302 | map(({ data, translations }) => { | |
111fdc26 C |
303 | const hashToPopulate: VideoConstant<T>[] = Object.keys(data) |
304 | .map(dataKey => { | |
305 | const label = data[ dataKey ] | |
306 | ||
307 | const id = attributeName === 'languages' | |
308 | ? dataKey as T | |
309 | : parseInt(dataKey, 10) as T | |
310 | ||
311 | return { | |
312 | id, | |
313 | label: peertubeTranslate(label, translations) | |
314 | } | |
315 | }) | |
ba430d75 C |
316 | |
317 | if (sort === true) sortBy(hashToPopulate, 'label') | |
7ce44a74 | 318 | |
ba430d75 C |
319 | return hashToPopulate |
320 | }), | |
321 | shareReplay() | |
322 | ) | |
db7af09b | 323 | } |
36f9424f C |
324 | |
325 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 326 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
327 | } |
328 | ||
329 | private loadConfigLocally () { | |
0bd78bf3 | 330 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
331 | |
332 | if (configString) { | |
333 | try { | |
334 | const parsed = JSON.parse(configString) | |
335 | Object.assign(this.config, parsed) | |
336 | } catch (err) { | |
337 | console.error('Cannot parse config saved in local storage.', err) | |
338 | } | |
339 | } | |
340 | } | |
db7af09b | 341 | } |