]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import omit from 'lodash-es/omit' | |
3 | import { forkJoin } from 'rxjs' | |
4 | import { SelectOptionsItem } from 'src/types/select-options-item.model' | |
5 | import { Component, OnInit } from '@angular/core' | |
6 | import { ActivatedRoute, Router } from '@angular/router' | |
7 | import { ConfigService } from '@app/+admin/config/shared/config.service' | |
8 | import { Notifier } from '@app/core' | |
9 | import { ServerService } from '@app/core/server/server.service' | |
10 | import { | |
11 | ADMIN_EMAIL_VALIDATOR, | |
12 | CACHE_CAPTIONS_SIZE_VALIDATOR, | |
13 | CACHE_PREVIEWS_SIZE_VALIDATOR, | |
14 | CONCURRENCY_VALIDATOR, | |
15 | INDEX_URL_VALIDATOR, | |
16 | INSTANCE_NAME_VALIDATOR, | |
17 | INSTANCE_SHORT_DESCRIPTION_VALIDATOR, | |
18 | MAX_INSTANCE_LIVES_VALIDATOR, | |
19 | MAX_LIVE_DURATION_VALIDATOR, | |
20 | MAX_USER_LIVES_VALIDATOR, | |
21 | MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR, | |
22 | SEARCH_INDEX_URL_VALIDATOR, | |
23 | SERVICES_TWITTER_USERNAME_VALIDATOR, | |
24 | SIGNUP_LIMIT_VALIDATOR, | |
25 | SIGNUP_MINIMUM_AGE_VALIDATOR, | |
26 | TRANSCODING_THREADS_VALIDATOR | |
27 | } from '@app/shared/form-validators/custom-config-validators' | |
28 | import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators' | |
29 | import { FormReactive, FormReactiveService } from '@app/shared/shared-forms' | |
30 | import { CustomPageService } from '@app/shared/shared-main/custom-page' | |
31 | import { CustomConfig, CustomPage, HTMLServerConfig } from '@shared/models' | |
32 | import { EditConfigurationService } from './edit-configuration.service' | |
33 | ||
34 | type ComponentCustomConfig = CustomConfig & { | |
35 | instanceCustomHomepage: CustomPage | |
36 | } | |
37 | ||
38 | @Component({ | |
39 | selector: 'my-edit-custom-config', | |
40 | templateUrl: './edit-custom-config.component.html', | |
41 | styleUrls: [ './edit-custom-config.component.scss' ] | |
42 | }) | |
43 | export class EditCustomConfigComponent extends FormReactive implements OnInit { | |
44 | activeNav: string | |
45 | ||
46 | customConfig: ComponentCustomConfig | |
47 | serverConfig: HTMLServerConfig | |
48 | ||
49 | homepage: CustomPage | |
50 | ||
51 | languageItems: SelectOptionsItem[] = [] | |
52 | categoryItems: SelectOptionsItem[] = [] | |
53 | ||
54 | constructor ( | |
55 | protected formReactiveService: FormReactiveService, | |
56 | private router: Router, | |
57 | private route: ActivatedRoute, | |
58 | private notifier: Notifier, | |
59 | private configService: ConfigService, | |
60 | private customPage: CustomPageService, | |
61 | private serverService: ServerService, | |
62 | private editConfigurationService: EditConfigurationService | |
63 | ) { | |
64 | super() | |
65 | } | |
66 | ||
67 | ngOnInit () { | |
68 | this.serverConfig = this.serverService.getHTMLConfig() | |
69 | ||
70 | const formGroupData: { [key in keyof ComponentCustomConfig ]: any } = { | |
71 | instance: { | |
72 | name: INSTANCE_NAME_VALIDATOR, | |
73 | shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR, | |
74 | description: null, | |
75 | ||
76 | isNSFW: false, | |
77 | defaultNSFWPolicy: null, | |
78 | ||
79 | terms: null, | |
80 | codeOfConduct: null, | |
81 | ||
82 | creationReason: null, | |
83 | moderationInformation: null, | |
84 | administrator: null, | |
85 | maintenanceLifetime: null, | |
86 | businessModel: null, | |
87 | ||
88 | hardwareInformation: null, | |
89 | ||
90 | categories: null, | |
91 | languages: null, | |
92 | ||
93 | defaultClientRoute: null, | |
94 | ||
95 | customizations: { | |
96 | javascript: null, | |
97 | css: null | |
98 | } | |
99 | }, | |
100 | theme: { | |
101 | default: null | |
102 | }, | |
103 | services: { | |
104 | twitter: { | |
105 | username: SERVICES_TWITTER_USERNAME_VALIDATOR, | |
106 | whitelisted: null | |
107 | } | |
108 | }, | |
109 | client: { | |
110 | videos: { | |
111 | miniature: { | |
112 | preferAuthorDisplayName: null | |
113 | } | |
114 | }, | |
115 | menu: { | |
116 | login: { | |
117 | redirectOnSingleExternalAuth: null | |
118 | } | |
119 | } | |
120 | }, | |
121 | cache: { | |
122 | previews: { | |
123 | size: CACHE_PREVIEWS_SIZE_VALIDATOR | |
124 | }, | |
125 | captions: { | |
126 | size: CACHE_CAPTIONS_SIZE_VALIDATOR | |
127 | }, | |
128 | torrents: { | |
129 | size: CACHE_CAPTIONS_SIZE_VALIDATOR | |
130 | } | |
131 | }, | |
132 | signup: { | |
133 | enabled: null, | |
134 | limit: SIGNUP_LIMIT_VALIDATOR, | |
135 | requiresApproval: null, | |
136 | requiresEmailVerification: null, | |
137 | minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR | |
138 | }, | |
139 | import: { | |
140 | videos: { | |
141 | concurrency: CONCURRENCY_VALIDATOR, | |
142 | http: { | |
143 | enabled: null | |
144 | }, | |
145 | torrent: { | |
146 | enabled: null | |
147 | } | |
148 | }, | |
149 | videoChannelSynchronization: { | |
150 | enabled: null | |
151 | } | |
152 | }, | |
153 | trending: { | |
154 | videos: { | |
155 | algorithms: { | |
156 | enabled: null, | |
157 | default: null | |
158 | } | |
159 | } | |
160 | }, | |
161 | admin: { | |
162 | email: ADMIN_EMAIL_VALIDATOR | |
163 | }, | |
164 | contactForm: { | |
165 | enabled: null | |
166 | }, | |
167 | user: { | |
168 | history: { | |
169 | videos: { | |
170 | enabled: null | |
171 | } | |
172 | }, | |
173 | videoQuota: USER_VIDEO_QUOTA_VALIDATOR, | |
174 | videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR | |
175 | }, | |
176 | videoChannels: { | |
177 | maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR | |
178 | }, | |
179 | transcoding: { | |
180 | enabled: null, | |
181 | threads: TRANSCODING_THREADS_VALIDATOR, | |
182 | allowAdditionalExtensions: null, | |
183 | allowAudioFiles: null, | |
184 | profile: null, | |
185 | concurrency: CONCURRENCY_VALIDATOR, | |
186 | resolutions: {}, | |
187 | alwaysTranscodeOriginalResolution: null, | |
188 | hls: { | |
189 | enabled: null | |
190 | }, | |
191 | webtorrent: { | |
192 | enabled: null | |
193 | }, | |
194 | remoteRunners: { | |
195 | enabled: null | |
196 | } | |
197 | }, | |
198 | live: { | |
199 | enabled: null, | |
200 | ||
201 | maxDuration: MAX_LIVE_DURATION_VALIDATOR, | |
202 | maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR, | |
203 | maxUserLives: MAX_USER_LIVES_VALIDATOR, | |
204 | allowReplay: null, | |
205 | latencySetting: { | |
206 | enabled: null | |
207 | }, | |
208 | ||
209 | transcoding: { | |
210 | enabled: null, | |
211 | threads: TRANSCODING_THREADS_VALIDATOR, | |
212 | profile: null, | |
213 | resolutions: {}, | |
214 | alwaysTranscodeOriginalResolution: null, | |
215 | remoteRunners: { | |
216 | enabled: null | |
217 | } | |
218 | } | |
219 | }, | |
220 | videoStudio: { | |
221 | enabled: null, | |
222 | remoteRunners: { | |
223 | enabled: null | |
224 | } | |
225 | }, | |
226 | autoBlacklist: { | |
227 | videos: { | |
228 | ofUsers: { | |
229 | enabled: null | |
230 | } | |
231 | } | |
232 | }, | |
233 | followers: { | |
234 | instance: { | |
235 | enabled: null, | |
236 | manualApproval: null | |
237 | } | |
238 | }, | |
239 | followings: { | |
240 | instance: { | |
241 | autoFollowBack: { | |
242 | enabled: null | |
243 | }, | |
244 | autoFollowIndex: { | |
245 | enabled: null, | |
246 | indexUrl: INDEX_URL_VALIDATOR | |
247 | } | |
248 | } | |
249 | }, | |
250 | broadcastMessage: { | |
251 | enabled: null, | |
252 | level: null, | |
253 | dismissable: null, | |
254 | message: null | |
255 | }, | |
256 | search: { | |
257 | remoteUri: { | |
258 | users: null, | |
259 | anonymous: null | |
260 | }, | |
261 | searchIndex: { | |
262 | enabled: null, | |
263 | url: SEARCH_INDEX_URL_VALIDATOR, | |
264 | disableLocalSearch: null, | |
265 | isDefaultSearch: null | |
266 | } | |
267 | }, | |
268 | ||
269 | instanceCustomHomepage: { | |
270 | content: null | |
271 | } | |
272 | } | |
273 | ||
274 | const defaultValues = { | |
275 | transcoding: { | |
276 | resolutions: {} as { [id: string]: string } | |
277 | }, | |
278 | live: { | |
279 | transcoding: { | |
280 | resolutions: {} as { [id: string]: string } | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | for (const resolution of this.editConfigurationService.getVODResolutions()) { | |
286 | defaultValues.transcoding.resolutions[resolution.id] = 'false' | |
287 | formGroupData.transcoding.resolutions[resolution.id] = null | |
288 | } | |
289 | ||
290 | for (const resolution of this.editConfigurationService.getLiveResolutions()) { | |
291 | defaultValues.live.transcoding.resolutions[resolution.id] = 'false' | |
292 | formGroupData.live.transcoding.resolutions[resolution.id] = null | |
293 | } | |
294 | ||
295 | this.buildForm(formGroupData) | |
296 | ||
297 | if (this.route.snapshot.fragment) { | |
298 | this.onNavChange(this.route.snapshot.fragment) | |
299 | } | |
300 | ||
301 | this.loadConfigAndUpdateForm() | |
302 | this.loadCategoriesAndLanguages() | |
303 | ||
304 | if (!this.isUpdateAllowed()) { | |
305 | this.form.disable() | |
306 | } | |
307 | } | |
308 | ||
309 | formValidated () { | |
310 | this.forceCheck() | |
311 | if (!this.form.valid) return | |
312 | ||
313 | const value: ComponentCustomConfig = this.form.getRawValue() | |
314 | ||
315 | forkJoin([ | |
316 | this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')), | |
317 | this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content) | |
318 | ]) | |
319 | .subscribe({ | |
320 | next: ([ resConfig ]) => { | |
321 | const instanceCustomHomepage = { | |
322 | content: value.instanceCustomHomepage.content | |
323 | } | |
324 | ||
325 | this.customConfig = { ...resConfig, instanceCustomHomepage } | |
326 | ||
327 | // Reload general configuration | |
328 | this.serverService.resetConfig() | |
329 | .subscribe(config => { | |
330 | this.serverConfig = config | |
331 | }) | |
332 | ||
333 | this.updateForm() | |
334 | ||
335 | this.notifier.success($localize`Configuration updated.`) | |
336 | }, | |
337 | ||
338 | error: err => this.notifier.error(err.message) | |
339 | }) | |
340 | } | |
341 | ||
342 | isUpdateAllowed () { | |
343 | return this.serverConfig.webadmin.configuration.edition.allowed === true | |
344 | } | |
345 | ||
346 | hasConsistentOptions () { | |
347 | if (this.hasLiveAllowReplayConsistentOptions()) return true | |
348 | ||
349 | return false | |
350 | } | |
351 | ||
352 | hasLiveAllowReplayConsistentOptions () { | |
353 | if ( | |
354 | this.editConfigurationService.isTranscodingEnabled(this.form) === false && | |
355 | this.editConfigurationService.isLiveEnabled(this.form) && | |
356 | this.form.value['live']['allowReplay'] === true | |
357 | ) { | |
358 | return false | |
359 | } | |
360 | ||
361 | return true | |
362 | } | |
363 | ||
364 | onNavChange (newActiveNav: string) { | |
365 | this.activeNav = newActiveNav | |
366 | ||
367 | this.router.navigate([], { fragment: this.activeNav }) | |
368 | } | |
369 | ||
370 | grabAllErrors (errorObjectArg?: any) { | |
371 | const errorObject = errorObjectArg || this.formErrors | |
372 | ||
373 | let acc: string[] = [] | |
374 | ||
375 | for (const key of Object.keys(errorObject)) { | |
376 | const value = errorObject[key] | |
377 | if (!value) continue | |
378 | ||
379 | if (typeof value === 'string') { | |
380 | acc.push(value) | |
381 | } else { | |
382 | acc = acc.concat(this.grabAllErrors(value)) | |
383 | } | |
384 | } | |
385 | ||
386 | return acc | |
387 | } | |
388 | ||
389 | private updateForm () { | |
390 | this.form.patchValue(this.customConfig) | |
391 | } | |
392 | ||
393 | private loadConfigAndUpdateForm () { | |
394 | forkJoin([ | |
395 | this.configService.getCustomConfig(), | |
396 | this.customPage.getInstanceHomepage() | |
397 | ]).subscribe({ | |
398 | next: ([ config, homepage ]) => { | |
399 | this.customConfig = { ...config, instanceCustomHomepage: homepage } | |
400 | ||
401 | this.updateForm() | |
402 | this.markAllAsDirty() | |
403 | }, | |
404 | ||
405 | error: err => this.notifier.error(err.message) | |
406 | }) | |
407 | } | |
408 | ||
409 | private loadCategoriesAndLanguages () { | |
410 | forkJoin([ | |
411 | this.serverService.getVideoLanguages(), | |
412 | this.serverService.getVideoCategories() | |
413 | ]).subscribe({ | |
414 | next: ([ languages, categories ]) => { | |
415 | this.languageItems = languages.map(l => ({ label: l.label, id: l.id })) | |
416 | this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' })) | |
417 | }, | |
418 | ||
419 | error: err => this.notifier.error(err.message) | |
420 | }) | |
421 | } | |
422 | } |