]>
Commit | Line | Data |
---|---|---|
1 | import { first, map, share, shareReplay, switchMap, tap } from 'rxjs/operators' | |
2 | import { HttpClient } from '@angular/common/http' | |
3 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' | |
4 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage' | |
5 | import { Observable, of, Subject } from 'rxjs' | |
6 | import { getCompleteLocale, ServerConfig } from '../../../../../shared' | |
7 | import { environment } from '../../../environments/environment' | |
8 | import { VideoConstant } from '../../../../../shared/models/videos' | |
9 | import { isDefaultLocale, peertubeTranslate } from '../../../../../shared/models/i18n' | |
10 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' | |
11 | import { sortBy } from '@app/shared/misc/utils' | |
12 | import { ServerStats } from '@shared/models/server' | |
13 | ||
14 | @Injectable() | |
15 | export class ServerService { | |
16 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/' | |
17 | private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/' | |
18 | private static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | |
19 | private static BASE_LOCALE_URL = environment.apiUrl + '/client/locales/' | |
20 | private static BASE_STATS_URL = environment.apiUrl + '/api/v1/server/stats' | |
21 | ||
22 | private static CONFIG_LOCAL_STORAGE_KEY = 'server-config' | |
23 | ||
24 | configReloaded = new Subject<void>() | |
25 | ||
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 | |
37 | private config: ServerConfig = { | |
38 | instance: { | |
39 | name: 'PeerTube', | |
40 | shortDescription: 'PeerTube, a federated (ActivityPub) video streaming platform ' + | |
41 | 'using P2P (BitTorrent) directly in the web browser with WebTorrent and Angular.', | |
42 | defaultClientRoute: '', | |
43 | isNSFW: false, | |
44 | defaultNSFWPolicy: 'do_not_list' as 'do_not_list', | |
45 | customizations: { | |
46 | javascript: '', | |
47 | css: '' | |
48 | } | |
49 | }, | |
50 | search: { | |
51 | remoteUri: { | |
52 | users: true, | |
53 | anonymous: false | |
54 | } | |
55 | }, | |
56 | plugin: { | |
57 | registered: [] | |
58 | }, | |
59 | theme: { | |
60 | registered: [], | |
61 | default: 'default' | |
62 | }, | |
63 | email: { | |
64 | enabled: false | |
65 | }, | |
66 | contactForm: { | |
67 | enabled: false | |
68 | }, | |
69 | serverVersion: 'Unknown', | |
70 | signup: { | |
71 | allowed: false, | |
72 | allowedForCurrentIP: false, | |
73 | requiresEmailVerification: false | |
74 | }, | |
75 | transcoding: { | |
76 | enabledResolutions: [], | |
77 | hls: { | |
78 | enabled: false | |
79 | }, | |
80 | webtorrent: { | |
81 | enabled: true | |
82 | } | |
83 | }, | |
84 | avatar: { | |
85 | file: { | |
86 | size: { max: 0 }, | |
87 | extensions: [] | |
88 | } | |
89 | }, | |
90 | video: { | |
91 | image: { | |
92 | size: { max: 0 }, | |
93 | extensions: [] | |
94 | }, | |
95 | file: { | |
96 | extensions: [] | |
97 | } | |
98 | }, | |
99 | videoCaption: { | |
100 | file: { | |
101 | size: { max: 0 }, | |
102 | extensions: [] | |
103 | } | |
104 | }, | |
105 | user: { | |
106 | videoQuota: -1, | |
107 | videoQuotaDaily: -1 | |
108 | }, | |
109 | import: { | |
110 | videos: { | |
111 | http: { | |
112 | enabled: false | |
113 | }, | |
114 | torrent: { | |
115 | enabled: false | |
116 | } | |
117 | } | |
118 | }, | |
119 | trending: { | |
120 | videos: { | |
121 | intervalDays: 0 | |
122 | } | |
123 | }, | |
124 | autoBlacklist: { | |
125 | videos: { | |
126 | ofUsers: { | |
127 | enabled: false | |
128 | } | |
129 | } | |
130 | }, | |
131 | tracker: { | |
132 | enabled: true | |
133 | }, | |
134 | followings: { | |
135 | instance: { | |
136 | autoFollowIndex: { | |
137 | indexUrl: 'https://instances.joinpeertube.org' | |
138 | } | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
143 | constructor ( | |
144 | private http: HttpClient, | |
145 | @Inject(LOCALE_ID) private localeId: string | |
146 | ) { | |
147 | this.loadConfigLocally() | |
148 | } | |
149 | ||
150 | getServerVersionAndCommit () { | |
151 | const serverVersion = this.config.serverVersion | |
152 | const commit = this.config.serverCommit || '' | |
153 | ||
154 | let result = serverVersion | |
155 | if (commit) result += '...' + commit | |
156 | ||
157 | return result | |
158 | } | |
159 | ||
160 | resetConfig () { | |
161 | this.configLoaded = false | |
162 | this.configReset = true | |
163 | } | |
164 | ||
165 | getConfig () { | |
166 | if (this.configLoaded) return of(this.config) | |
167 | ||
168 | if (!this.configObservable) { | |
169 | this.configObservable = this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL) | |
170 | .pipe( | |
171 | tap(config => this.saveConfigLocally(config)), | |
172 | tap(config => { | |
173 | this.config = config | |
174 | this.configLoaded = true | |
175 | }), | |
176 | tap(() => { | |
177 | if (this.configReset) { | |
178 | this.configReloaded.next() | |
179 | this.configReset = false | |
180 | } | |
181 | }), | |
182 | share() | |
183 | ) | |
184 | } | |
185 | ||
186 | return this.configObservable | |
187 | } | |
188 | ||
189 | getTmpConfig () { | |
190 | return this.config | |
191 | } | |
192 | ||
193 | getVideoCategories () { | |
194 | if (!this.videoCategoriesObservable) { | |
195 | this.videoCategoriesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'categories', true) | |
196 | } | |
197 | ||
198 | return this.videoCategoriesObservable.pipe(first()) | |
199 | } | |
200 | ||
201 | getVideoLicences () { | |
202 | if (!this.videoLicensesObservable) { | |
203 | this.videoLicensesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'licences') | |
204 | } | |
205 | ||
206 | return this.videoLicensesObservable.pipe(first()) | |
207 | } | |
208 | ||
209 | getVideoLanguages () { | |
210 | if (!this.videoLanguagesObservable) { | |
211 | this.videoLanguagesObservable = this.loadAttributeEnum<string>(ServerService.BASE_VIDEO_URL, 'languages', true) | |
212 | } | |
213 | ||
214 | return this.videoLanguagesObservable.pipe(first()) | |
215 | } | |
216 | ||
217 | getVideoPrivacies () { | |
218 | if (!this.videoPrivaciesObservable) { | |
219 | this.videoPrivaciesObservable = this.loadAttributeEnum<number>(ServerService.BASE_VIDEO_URL, 'privacies') | |
220 | } | |
221 | ||
222 | return this.videoPrivaciesObservable.pipe(first()) | |
223 | } | |
224 | ||
225 | getVideoPlaylistPrivacies () { | |
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()) | |
248 | } | |
249 | ||
250 | getServerStats () { | |
251 | return this.http.get<ServerStats>(ServerService.BASE_STATS_URL) | |
252 | } | |
253 | ||
254 | private loadAttributeEnum <T extends string | number> ( | |
255 | baseUrl: string, | |
256 | attributeName: 'categories' | 'licences' | 'languages' | 'privacies', | |
257 | sort = false | |
258 | ) { | |
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>[] = [] | |
267 | ||
268 | Object.keys(data) | |
269 | .forEach(dataKey => { | |
270 | const label = data[ dataKey ] | |
271 | ||
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') | |
279 | ||
280 | return hashToPopulate | |
281 | }), | |
282 | shareReplay() | |
283 | ) | |
284 | } | |
285 | ||
286 | private saveConfigLocally (config: ServerConfig) { | |
287 | peertubeLocalStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config)) | |
288 | } | |
289 | ||
290 | private loadConfigLocally () { | |
291 | const configString = peertubeLocalStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY) | |
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 | } | |
302 | } |