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