]>
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 | |
ba430d75 | 24 | configReloaded = new Subject<void>() |
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 C |
56 | plugin: { |
57 | registered: [] | |
58 | }, | |
59 | theme: { | |
60 | registered: [], | |
61 | default: 'default' | |
62 | }, | |
3b3b1820 C |
63 | email: { |
64 | enabled: false | |
65 | }, | |
3866f1a0 C |
66 | contactForm: { |
67 | enabled: false | |
68 | }, | |
915c5bbe | 69 | serverVersion: 'Unknown', |
db7af09b | 70 | signup: { |
ff2c1fe8 | 71 | allowed: false, |
d9eaee39 JM |
72 | allowedForCurrentIP: false, |
73 | requiresEmailVerification: false | |
6a84aafd C |
74 | }, |
75 | transcoding: { | |
09209296 C |
76 | enabledResolutions: [], |
77 | hls: { | |
78 | enabled: false | |
5a71acd2 C |
79 | }, |
80 | webtorrent: { | |
81 | enabled: true | |
09209296 | 82 | } |
01de67b9 C |
83 | }, |
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 | } | |
db7af09b C |
140 | } |
141 | } | |
db7af09b | 142 | |
7ce44a74 C |
143 | constructor ( |
144 | private http: HttpClient, | |
145 | @Inject(LOCALE_ID) private localeId: string | |
146 | ) { | |
74b7c6d4 | 147 | this.loadConfigLocally() |
36f9424f | 148 | } |
db7af09b | 149 | |
ba430d75 C |
150 | getServerVersionAndCommit () { |
151 | const serverVersion = this.config.serverVersion | |
152 | const commit = this.config.serverCommit || '' | |
00b5556c | 153 | |
ba430d75 C |
154 | let result = serverVersion |
155 | if (commit) result += '...' + commit | |
db7af09b | 156 | |
ba430d75 | 157 | return result |
db7af09b C |
158 | } |
159 | ||
ba430d75 C |
160 | resetConfig () { |
161 | this.configLoaded = false | |
162 | this.configReset = true | |
db7af09b C |
163 | } |
164 | ||
ba430d75 C |
165 | getConfig () { |
166 | if (this.configLoaded) return of(this.config) | |
db7af09b | 167 | |
ba430d75 C |
168 | if (!this.configObservable) { |
169 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
170 | .pipe( | |
017fbe18 C |
171 | tap(config => this.saveConfigLocally(config)), |
172 | tap(config => { | |
173 | this.config = config | |
174 | this.configLoaded = true | |
175 | }), | |
ba430d75 C |
176 | tap(() => { |
177 | if (this.configReset) { | |
178 | this.configReloaded.next() | |
179 | this.configReset = false | |
180 | } | |
181 | }), | |
182 | share() | |
183 | ) | |
184 | } | |
830b4faf | 185 | |
ba430d75 | 186 | return this.configObservable |
fd45e8f4 C |
187 | } |
188 | ||
ba430d75 C |
189 | getTmpConfig () { |
190 | return this.config | |
dbdf2d51 C |
191 | } |
192 | ||
db7af09b | 193 | getVideoCategories () { |
ba430d75 C |
194 | if (!this.videoCategoriesObservable) { |
195 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
196 | } | |
197 | ||
198 | return this.videoCategoriesObservable.pipe(first()) | |
db7af09b C |
199 | } |
200 | ||
201 | getVideoLicences () { | |
ba430d75 C |
202 | if (!this.videoLicensesObservable) { |
203 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
204 | } | |
205 | ||
206 | return this.videoLicensesObservable.pipe(first()) | |
db7af09b C |
207 | } |
208 | ||
209 | getVideoLanguages () { | |
ba430d75 C |
210 | if (!this.videoLanguagesObservable) { |
211 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
212 | } | |
213 | ||
214 | return this.videoLanguagesObservable.pipe(first()) | |
db7af09b C |
215 | } |
216 | ||
fd45e8f4 | 217 | getVideoPrivacies () { |
ba430d75 C |
218 | if (!this.videoPrivaciesObservable) { |
219 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
220 | } | |
221 | ||
222 | return this.videoPrivaciesObservable.pipe(first()) | |
fd45e8f4 C |
223 | } |
224 | ||
830b4faf | 225 | getVideoPlaylistPrivacies () { |
ba430d75 C |
226 | if (!this.videoPlaylistPrivaciesObservable) { |
227 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
228 | } | |
229 | ||
230 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
231 | } | |
232 | ||
233 | getServerLocale () { | |
234 | if (!this.localeObservable) { | |
235 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
236 | ||
237 | // Default locale, nothing to translate | |
238 | if (isDefaultLocale(completeLocale)) { | |
239 | this.localeObservable = of({}).pipe(shareReplay()) | |
240 | } else { | |
241 | this.localeObservable = this.http | |
242 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
243 | .pipe(shareReplay()) | |
244 | } | |
245 | } | |
246 | ||
247 | return this.localeObservable.pipe(first()) | |
830b4faf C |
248 | } |
249 | ||
b764380a C |
250 | getServerStats () { |
251 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
252 | } | |
253 | ||
ba430d75 | 254 | private loadAttributeEnum <T extends string | number> ( |
830b4faf | 255 | baseUrl: string, |
fd45e8f4 | 256 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
3580fc00 | 257 | sort = false |
fd45e8f4 | 258 | ) { |
ba430d75 C |
259 | return this.getServerLocale() |
260 | .pipe( | |
261 | switchMap(translations => { | |
262 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
263 | .pipe(map(data => ({ data, translations }))) | |
264 | }), | |
265 | map(({ data, translations }) => { | |
266 | const hashToPopulate: VideoConstant<T>[] = [] | |
7ce44a74 | 267 | |
ba430d75 C |
268 | Object.keys(data) |
269 | .forEach(dataKey => { | |
270 | const label = data[ dataKey ] | |
7ce44a74 | 271 | |
ba430d75 C |
272 | hashToPopulate.push({ |
273 | id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T, | |
274 | label: peertubeTranslate(label, translations) | |
275 | }) | |
276 | }) | |
277 | ||
278 | if (sort === true) sortBy(hashToPopulate, 'label') | |
7ce44a74 | 279 | |
ba430d75 C |
280 | return hashToPopulate |
281 | }), | |
282 | shareReplay() | |
283 | ) | |
db7af09b | 284 | } |
36f9424f C |
285 | |
286 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 287 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
288 | } |
289 | ||
290 | private loadConfigLocally () { | |
0bd78bf3 | 291 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
292 | |
293 | if (configString) { | |
294 | try { | |
295 | const parsed = JSON.parse(configString) | |
296 | Object.assign(this.config, parsed) | |
297 | } catch (err) { | |
298 | console.error('Cannot parse config saved in local storage.', err) | |
299 | } | |
300 | } | |
301 | } | |
db7af09b | 302 | } |