]>
Commit | Line | Data |
---|---|---|
ba430d75 | 1 | import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators' |
db7af09b | 2 | import { HttpClient } from '@angular/common/http' |
7ce44a74 | 3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
88a7f93f | 4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' |
ba430d75 | 5 | import { Observable, of, Subject } from 'rxjs' |
74b7c6d4 | 6 | import { getCompleteLocale, ServerConfig } from '../../../../../shared' |
63c4db6d | 7 | import { environment } from '../../../environments/environment' |
ba430d75 | 8 | import { VideoConstant } from '../../../../../shared/models/videos' |
3dfa8494 C |
9 | import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n' |
10 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' | |
ad774752 | 11 | import { sortBy } from '@app/shared/misc/utils' |
b764380a | 12 | import { ServerStats } from '@shared/models/server' |
db7af09b C |
13 | |
14 | @Injectable() | |
15 | export class ServerService { | |
63c4db6d C |
16 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' |
17 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
830b4faf | 18 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' |
7ce44a74 | 19 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' |
b764380a C |
20 | private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats' |
21 | ||
36f9424f | 22 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' |
db7af09b | 23 | |
72c33e71 | 24 | configReloaded = new Subject<ServerConfig>() |
baeefe22 | 25 | |
ba430d75 C |
26 | private localeObservable: Observable<any> |
27 | private videoLicensesObservable: Observable<VideoConstant<number>[]> | |
28 | private videoCategoriesObservable: Observable<VideoConstant<number>[]> | |
29 | private videoPrivaciesObservable: Observable<VideoConstant<number>[]> | |
30 | private videoPlaylistPrivaciesObservable: Observable<VideoConstant<number>[]> | |
31 | private videoLanguagesObservable: Observable<VideoConstant<string>[]> | |
32 | private configObservable: Observable<ServerConfig> | |
33 | ||
34 | private configReset = false | |
35 | ||
36 | private configLoaded = false | |
db7af09b | 37 | private config: ServerConfig = { |
36f9424f | 38 | instance: { |
00b5556c | 39 | name: 'PeerTube', |
63ac2857 C |
40 | shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' + |
41 | 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.', | |
901637bb | 42 | defaultClientRoute: '', |
f8802489 | 43 | isNSFW: false, |
0883b324 | 44 | defaultNSFWPolicy: 'do_not_list' as 'do_not_list', |
00b5556c C |
45 | customizations: { |
46 | javascript: '', | |
47 | css: '' | |
48 | } | |
36f9424f | 49 | }, |
9677fca7 RK |
50 | search: { |
51 | remoteUri: { | |
52 | users: true, | |
53 | anonymous: false | |
54 | } | |
55 | }, | |
7cd4d2ba | 56 | plugin: { |
4a8d113b C |
57 | registered: [], |
58 | registeredExternalAuths: [], | |
59 | registeredIdAndPassAuths: [] | |
7cd4d2ba C |
60 | }, |
61 | theme: { | |
62 | registered: [], | |
63 | default: 'default' | |
64 | }, | |
3b3b1820 C |
65 | email: { |
66 | enabled: false | |
67 | }, | |
3866f1a0 C |
68 | contactForm: { |
69 | enabled: false | |
70 | }, | |
915c5bbe | 71 | serverVersion: 'Unknown', |
db7af09b | 72 | signup: { |
ff2c1fe8 | 73 | allowed: false, |
d9eaee39 JM |
74 | allowedForCurrentIP: false, |
75 | requiresEmailVerification: false | |
6a84aafd C |
76 | }, |
77 | transcoding: { | |
09209296 C |
78 | enabledResolutions: [], |
79 | hls: { | |
80 | enabled: false | |
5a71acd2 C |
81 | }, |
82 | webtorrent: { | |
83 | enabled: true | |
09209296 | 84 | } |
01de67b9 C |
85 | }, |
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 | |
db7af09b C |
148 | } |
149 | } | |
db7af09b | 150 | |
7ce44a74 C |
151 | constructor ( |
152 | private http: HttpClient, | |
153 | @Inject(LOCALE_ID) private localeId: string | |
154 | ) { | |
74b7c6d4 | 155 | this.loadConfigLocally() |
36f9424f | 156 | } |
db7af09b | 157 | |
ba430d75 C |
158 | getServerVersionAndCommit () { |
159 | const serverVersion = this.config.serverVersion | |
160 | const commit = this.config.serverCommit || '' | |
00b5556c | 161 | |
ba430d75 C |
162 | let result = serverVersion |
163 | if (commit) result += '...' + commit | |
db7af09b | 164 | |
ba430d75 | 165 | return result |
db7af09b C |
166 | } |
167 | ||
ba430d75 C |
168 | resetConfig () { |
169 | this.configLoaded = false | |
170 | this.configReset = true | |
72c33e71 C |
171 | |
172 | // Notify config update | |
173 | this.getConfig().subscribe(() => { | |
174 | // empty, to fire a reset config event | |
175 | }) | |
db7af09b C |
176 | } |
177 | ||
ba430d75 C |
178 | getConfig () { |
179 | if (this.configLoaded) return of(this.config) | |
db7af09b | 180 | |
ba430d75 C |
181 | if (!this.configObservable) { |
182 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
183 | .pipe( | |
017fbe18 C |
184 | tap(config => this.saveConfigLocally(config)), |
185 | tap(config => { | |
186 | this.config = config | |
187 | this.configLoaded = true | |
188 | }), | |
72c33e71 | 189 | tap(config => { |
ba430d75 | 190 | if (this.configReset) { |
72c33e71 | 191 | this.configReloaded.next(config) |
ba430d75 C |
192 | this.configReset = false |
193 | } | |
194 | }), | |
195 | share() | |
196 | ) | |
197 | } | |
830b4faf | 198 | |
ba430d75 | 199 | return this.configObservable |
fd45e8f4 C |
200 | } |
201 | ||
ba430d75 C |
202 | getTmpConfig () { |
203 | return this.config | |
dbdf2d51 C |
204 | } |
205 | ||
db7af09b | 206 | getVideoCategories () { |
ba430d75 C |
207 | if (!this.videoCategoriesObservable) { |
208 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
209 | } | |
210 | ||
211 | return this.videoCategoriesObservable.pipe(first()) | |
db7af09b C |
212 | } |
213 | ||
214 | getVideoLicences () { | |
ba430d75 C |
215 | if (!this.videoLicensesObservable) { |
216 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
217 | } | |
218 | ||
219 | return this.videoLicensesObservable.pipe(first()) | |
db7af09b C |
220 | } |
221 | ||
222 | getVideoLanguages () { | |
ba430d75 C |
223 | if (!this.videoLanguagesObservable) { |
224 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
225 | } | |
226 | ||
227 | return this.videoLanguagesObservable.pipe(first()) | |
db7af09b C |
228 | } |
229 | ||
fd45e8f4 | 230 | getVideoPrivacies () { |
ba430d75 C |
231 | if (!this.videoPrivaciesObservable) { |
232 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
233 | } | |
234 | ||
235 | return this.videoPrivaciesObservable.pipe(first()) | |
fd45e8f4 C |
236 | } |
237 | ||
830b4faf | 238 | getVideoPlaylistPrivacies () { |
ba430d75 C |
239 | if (!this.videoPlaylistPrivaciesObservable) { |
240 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
241 | } | |
242 | ||
243 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
244 | } | |
245 | ||
246 | getServerLocale () { | |
247 | if (!this.localeObservable) { | |
248 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
249 | ||
250 | // Default locale, nothing to translate | |
251 | if (isDefaultLocale(completeLocale)) { | |
252 | this.localeObservable = of({}).pipe(shareReplay()) | |
253 | } else { | |
254 | this.localeObservable = this.http | |
255 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
256 | .pipe(shareReplay()) | |
257 | } | |
258 | } | |
259 | ||
260 | return this.localeObservable.pipe(first()) | |
830b4faf C |
261 | } |
262 | ||
b764380a C |
263 | getServerStats () { |
264 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
265 | } | |
266 | ||
ba430d75 | 267 | private loadAttributeEnum <T extends string | number> ( |
830b4faf | 268 | baseUrl: string, |
fd45e8f4 | 269 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
3580fc00 | 270 | sort = false |
fd45e8f4 | 271 | ) { |
ba430d75 C |
272 | return this.getServerLocale() |
273 | .pipe( | |
274 | switchMap(translations => { | |
275 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
276 | .pipe(map(data => ({ data, translations }))) | |
277 | }), | |
278 | map(({ data, translations }) => { | |
111fdc26 C |
279 | const hashToPopulate: VideoConstant<T>[] = Object.keys(data) |
280 | .map(dataKey => { | |
281 | const label = data[ dataKey ] | |
282 | ||
283 | const id = attributeName === 'languages' | |
284 | ? dataKey as T | |
285 | : parseInt(dataKey, 10) as T | |
286 | ||
287 | return { | |
288 | id, | |
289 | label: peertubeTranslate(label, translations) | |
290 | } | |
291 | }) | |
ba430d75 C |
292 | |
293 | if (sort === true) sortBy(hashToPopulate, 'label') | |
7ce44a74 | 294 | |
ba430d75 C |
295 | return hashToPopulate |
296 | }), | |
297 | shareReplay() | |
298 | ) | |
db7af09b | 299 | } |
36f9424f C |
300 | |
301 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 302 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
303 | } |
304 | ||
305 | private loadConfigLocally () { | |
0bd78bf3 | 306 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
307 | |
308 | if (configString) { | |
309 | try { | |
310 | const parsed = JSON.parse(configString) | |
311 | Object.assign(this.config, parsed) | |
312 | } catch (err) { | |
313 | console.error('Cannot parse config saved in local storage.', err) | |
314 | } | |
315 | } | |
316 | } | |
db7af09b | 317 | } |