]>
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 | }, |
7cd4d2ba C |
50 | plugin: { |
51 | registered: [] | |
52 | }, | |
53 | theme: { | |
54 | registered: [], | |
55 | default: 'default' | |
56 | }, | |
3b3b1820 C |
57 | email: { |
58 | enabled: false | |
59 | }, | |
3866f1a0 C |
60 | contactForm: { |
61 | enabled: false | |
62 | }, | |
915c5bbe | 63 | serverVersion: 'Unknown', |
db7af09b | 64 | signup: { |
ff2c1fe8 | 65 | allowed: false, |
d9eaee39 JM |
66 | allowedForCurrentIP: false, |
67 | requiresEmailVerification: false | |
6a84aafd C |
68 | }, |
69 | transcoding: { | |
09209296 C |
70 | enabledResolutions: [], |
71 | hls: { | |
72 | enabled: false | |
5a71acd2 C |
73 | }, |
74 | webtorrent: { | |
75 | enabled: true | |
09209296 | 76 | } |
01de67b9 C |
77 | }, |
78 | avatar: { | |
79 | file: { | |
80 | size: { max: 0 }, | |
81 | extensions: [] | |
82 | } | |
83 | }, | |
84 | video: { | |
6de36768 C |
85 | image: { |
86 | size: { max: 0 }, | |
87 | extensions: [] | |
88 | }, | |
01de67b9 C |
89 | file: { |
90 | extensions: [] | |
91 | } | |
1869c875 | 92 | }, |
40e87e9e C |
93 | videoCaption: { |
94 | file: { | |
95 | size: { max: 0 }, | |
96 | extensions: [] | |
97 | } | |
98 | }, | |
1869c875 | 99 | user: { |
bee0abff FA |
100 | videoQuota: -1, |
101 | videoQuotaDaily: -1 | |
5d08a6a7 C |
102 | }, |
103 | import: { | |
b2977eec | 104 | videos: { |
5d08a6a7 C |
105 | http: { |
106 | enabled: false | |
a84b8fa5 C |
107 | }, |
108 | torrent: { | |
109 | enabled: false | |
5d08a6a7 C |
110 | } |
111 | } | |
9b4b15f9 AB |
112 | }, |
113 | trending: { | |
114 | videos: { | |
115 | intervalDays: 0 | |
116 | } | |
7ccddd7b JM |
117 | }, |
118 | autoBlacklist: { | |
119 | videos: { | |
120 | ofUsers: { | |
121 | enabled: false | |
122 | } | |
123 | } | |
31b6ddf8 C |
124 | }, |
125 | tracker: { | |
126 | enabled: true | |
f24c8b14 RK |
127 | }, |
128 | followings: { | |
129 | instance: { | |
130 | autoFollowIndex: { | |
131 | indexUrl: 'https://instances.joinpeertube.org' | |
132 | } | |
133 | } | |
db7af09b C |
134 | } |
135 | } | |
db7af09b | 136 | |
7ce44a74 C |
137 | constructor ( |
138 | private http: HttpClient, | |
139 | @Inject(LOCALE_ID) private localeId: string | |
140 | ) { | |
74b7c6d4 | 141 | this.loadConfigLocally() |
36f9424f | 142 | } |
db7af09b | 143 | |
ba430d75 C |
144 | getServerVersionAndCommit () { |
145 | const serverVersion = this.config.serverVersion | |
146 | const commit = this.config.serverCommit || '' | |
00b5556c | 147 | |
ba430d75 C |
148 | let result = serverVersion |
149 | if (commit) result += '...' + commit | |
db7af09b | 150 | |
ba430d75 | 151 | return result |
db7af09b C |
152 | } |
153 | ||
ba430d75 C |
154 | resetConfig () { |
155 | this.configLoaded = false | |
156 | this.configReset = true | |
db7af09b C |
157 | } |
158 | ||
ba430d75 C |
159 | getConfig () { |
160 | if (this.configLoaded) return of(this.config) | |
db7af09b | 161 | |
ba430d75 C |
162 | if (!this.configObservable) { |
163 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
164 | .pipe( | |
165 | tap(this.saveConfigLocally), | |
166 | tap(() => this.configLoaded = true), | |
167 | tap(() => { | |
168 | if (this.configReset) { | |
169 | this.configReloaded.next() | |
170 | this.configReset = false | |
171 | } | |
172 | }), | |
173 | share() | |
174 | ) | |
175 | } | |
830b4faf | 176 | |
ba430d75 | 177 | return this.configObservable |
fd45e8f4 C |
178 | } |
179 | ||
ba430d75 C |
180 | getTmpConfig () { |
181 | return this.config | |
dbdf2d51 C |
182 | } |
183 | ||
db7af09b | 184 | getVideoCategories () { |
ba430d75 C |
185 | if (!this.videoCategoriesObservable) { |
186 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
187 | } | |
188 | ||
189 | return this.videoCategoriesObservable.pipe(first()) | |
db7af09b C |
190 | } |
191 | ||
192 | getVideoLicences () { | |
ba430d75 C |
193 | if (!this.videoLicensesObservable) { |
194 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
195 | } | |
196 | ||
197 | return this.videoLicensesObservable.pipe(first()) | |
db7af09b C |
198 | } |
199 | ||
200 | getVideoLanguages () { | |
ba430d75 C |
201 | if (!this.videoLanguagesObservable) { |
202 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
203 | } | |
204 | ||
205 | return this.videoLanguagesObservable.pipe(first()) | |
db7af09b C |
206 | } |
207 | ||
fd45e8f4 | 208 | getVideoPrivacies () { |
ba430d75 C |
209 | if (!this.videoPrivaciesObservable) { |
210 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
211 | } | |
212 | ||
213 | return this.videoPrivaciesObservable.pipe(first()) | |
fd45e8f4 C |
214 | } |
215 | ||
830b4faf | 216 | getVideoPlaylistPrivacies () { |
ba430d75 C |
217 | if (!this.videoPlaylistPrivaciesObservable) { |
218 | this.videoPlaylistPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_PLAYLIST_URL, 'privacies') | |
219 | } | |
220 | ||
221 | return this.videoPlaylistPrivaciesObservable.pipe(first()) | |
222 | } | |
223 | ||
224 | getServerLocale () { | |
225 | if (!this.localeObservable) { | |
226 | const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
227 | ||
228 | // Default locale, nothing to translate | |
229 | if (isDefaultLocale(completeLocale)) { | |
230 | this.localeObservable = of({}).pipe(shareReplay()) | |
231 | } else { | |
232 | this.localeObservable = this.http | |
233 | .get(ServerService.BASE_LOCALE_URL + completeLocale + '/server.json') | |
234 | .pipe(shareReplay()) | |
235 | } | |
236 | } | |
237 | ||
238 | return this.localeObservable.pipe(first()) | |
830b4faf C |
239 | } |
240 | ||
b764380a C |
241 | getServerStats () { |
242 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
243 | } | |
244 | ||
ba430d75 | 245 | private loadAttributeEnum <T extends string | number> ( |
830b4faf | 246 | baseUrl: string, |
fd45e8f4 | 247 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', |
3580fc00 | 248 | sort = false |
fd45e8f4 | 249 | ) { |
ba430d75 C |
250 | return this.getServerLocale() |
251 | .pipe( | |
252 | switchMap(translations => { | |
253 | return this.http.get<{ [ id: string ]: string }>(baseUrl + attributeName) | |
254 | .pipe(map(data => ({ data, translations }))) | |
255 | }), | |
256 | map(({ data, translations }) => { | |
257 | const hashToPopulate: VideoConstant<T>[] = [] | |
7ce44a74 | 258 | |
ba430d75 C |
259 | Object.keys(data) |
260 | .forEach(dataKey => { | |
261 | const label = data[ dataKey ] | |
7ce44a74 | 262 | |
ba430d75 C |
263 | hashToPopulate.push({ |
264 | id: (attributeName === 'languages' ? dataKey : parseInt(dataKey, 10)) as T, | |
265 | label: peertubeTranslate(label, translations) | |
266 | }) | |
267 | }) | |
268 | ||
269 | if (sort === true) sortBy(hashToPopulate, 'label') | |
7ce44a74 | 270 | |
ba430d75 C |
271 | return hashToPopulate |
272 | }), | |
273 | shareReplay() | |
274 | ) | |
db7af09b | 275 | } |
36f9424f C |
276 | |
277 | private saveConfigLocally (config: ServerConfig) { | |
0bd78bf3 | 278 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) |
36f9424f C |
279 | } |
280 | ||
281 | private loadConfigLocally () { | |
0bd78bf3 | 282 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) |
36f9424f C |
283 | |
284 | if (configString) { | |
285 | try { | |
286 | const parsed = JSON.parse(configString) | |
287 | Object.assign(this.config, parsed) | |
288 | } catch (err) { | |
289 | console.error('Cannot parse config saved in local storage.', err) | |
290 | } | |
291 | } | |
292 | } | |
db7af09b | 293 | } |