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