]>
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 | videoQuota: USER_VIDEO_QUOTA_VALIDATOR, | |
169 | videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR | |
170 | }, | |
171 | videoChannels: { | |
172 | maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR | |
173 | }, | |
174 | transcoding: { | |
175 | enabled: null, | |
176 | threads: TRANSCODING_THREADS_VALIDATOR, | |
177 | allowAdditionalExtensions: null, | |
178 | allowAudioFiles: null, | |
179 | profile: null, | |
180 | concurrency: CONCURRENCY_VALIDATOR, | |
181 | resolutions: {}, | |
182 | alwaysTranscodeOriginalResolution: null, | |
183 | hls: { | |
184 | enabled: null | |
185 | }, | |
186 | webtorrent: { | |
187 | enabled: null | |
188 | } | |
189 | }, | |
190 | live: { | |
191 | enabled: null, | |
192 | ||
193 | maxDuration: MAX_LIVE_DURATION_VALIDATOR, | |
194 | maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR, | |
195 | maxUserLives: MAX_USER_LIVES_VALIDATOR, | |
196 | allowReplay: null, | |
197 | latencySetting: { | |
198 | enabled: null | |
199 | }, | |
200 | ||
201 | transcoding: { | |
202 | enabled: null, | |
203 | threads: TRANSCODING_THREADS_VALIDATOR, | |
204 | profile: null, | |
205 | resolutions: {}, | |
206 | alwaysTranscodeOriginalResolution: null | |
207 | } | |
208 | }, | |
209 | videoStudio: { | |
210 | enabled: null | |
211 | }, | |
212 | autoBlacklist: { | |
213 | videos: { | |
214 | ofUsers: { | |
215 | enabled: null | |
216 | } | |
217 | } | |
218 | }, | |
219 | followers: { | |
220 | instance: { | |
221 | enabled: null, | |
222 | manualApproval: null | |
223 | } | |
224 | }, | |
225 | followings: { | |
226 | instance: { | |
227 | autoFollowBack: { | |
228 | enabled: null | |
229 | }, | |
230 | autoFollowIndex: { | |
231 | enabled: null, | |
232 | indexUrl: INDEX_URL_VALIDATOR | |
233 | } | |
234 | } | |
235 | }, | |
236 | broadcastMessage: { | |
237 | enabled: null, | |
238 | level: null, | |
239 | dismissable: null, | |
240 | message: null | |
241 | }, | |
242 | search: { | |
243 | remoteUri: { | |
244 | users: null, | |
245 | anonymous: null | |
246 | }, | |
247 | searchIndex: { | |
248 | enabled: null, | |
249 | url: SEARCH_INDEX_URL_VALIDATOR, | |
250 | disableLocalSearch: null, | |
251 | isDefaultSearch: null | |
252 | } | |
253 | }, | |
254 | ||
255 | instanceCustomHomepage: { | |
256 | content: null | |
257 | } | |
258 | } | |
259 | ||
260 | const defaultValues = { | |
261 | transcoding: { | |
262 | resolutions: {} | |
263 | }, | |
264 | live: { | |
265 | transcoding: { | |
266 | resolutions: {} | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | for (const resolution of this.editConfigurationService.getVODResolutions()) { | |
272 | defaultValues.transcoding.resolutions[resolution.id] = 'false' | |
273 | formGroupData.transcoding.resolutions[resolution.id] = null | |
274 | } | |
275 | ||
276 | for (const resolution of this.editConfigurationService.getLiveResolutions()) { | |
277 | defaultValues.live.transcoding.resolutions[resolution.id] = 'false' | |
278 | formGroupData.live.transcoding.resolutions[resolution.id] = null | |
279 | } | |
280 | ||
281 | this.buildForm(formGroupData) | |
282 | ||
283 | if (this.route.snapshot.fragment) { | |
284 | this.onNavChange(this.route.snapshot.fragment) | |
285 | } | |
286 | ||
287 | this.loadConfigAndUpdateForm() | |
288 | this.loadCategoriesAndLanguages() | |
289 | ||
290 | if (!this.isUpdateAllowed()) { | |
291 | this.form.disable() | |
292 | } | |
293 | } | |
294 | ||
295 | formValidated () { | |
296 | this.forceCheck() | |
297 | if (!this.form.valid) return | |
298 | ||
299 | const value: ComponentCustomConfig = this.form.getRawValue() | |
300 | ||
301 | forkJoin([ | |
302 | this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')), | |
303 | this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content) | |
304 | ]) | |
305 | .subscribe({ | |
306 | next: ([ resConfig ]) => { | |
307 | const instanceCustomHomepage = { | |
308 | content: value.instanceCustomHomepage.content | |
309 | } | |
310 | ||
311 | this.customConfig = { ...resConfig, instanceCustomHomepage } | |
312 | ||
313 | // Reload general configuration | |
314 | this.serverService.resetConfig() | |
315 | .subscribe(config => { | |
316 | this.serverConfig = config | |
317 | }) | |
318 | ||
319 | this.updateForm() | |
320 | ||
321 | this.notifier.success($localize`Configuration updated.`) | |
322 | }, | |
323 | ||
324 | error: err => this.notifier.error(err.message) | |
325 | }) | |
326 | } | |
327 | ||
328 | isUpdateAllowed () { | |
329 | return this.serverConfig.webadmin.configuration.edition.allowed === true | |
330 | } | |
331 | ||
332 | hasConsistentOptions () { | |
333 | if (this.hasLiveAllowReplayConsistentOptions()) return true | |
334 | ||
335 | return false | |
336 | } | |
337 | ||
338 | hasLiveAllowReplayConsistentOptions () { | |
339 | if ( | |
340 | this.editConfigurationService.isTranscodingEnabled(this.form) === false && | |
341 | this.editConfigurationService.isLiveEnabled(this.form) && | |
342 | this.form.value['live']['allowReplay'] === true | |
343 | ) { | |
344 | return false | |
345 | } | |
346 | ||
347 | return true | |
348 | } | |
349 | ||
350 | onNavChange (newActiveNav: string) { | |
351 | this.activeNav = newActiveNav | |
352 | ||
353 | this.router.navigate([], { fragment: this.activeNav }) | |
354 | } | |
355 | ||
356 | grabAllErrors (errorObjectArg?: any) { | |
357 | const errorObject = errorObjectArg || this.formErrors | |
358 | ||
359 | let acc: string[] = [] | |
360 | ||
361 | for (const key of Object.keys(errorObject)) { | |
362 | const value = errorObject[key] | |
363 | if (!value) continue | |
364 | ||
365 | if (typeof value === 'string') { | |
366 | acc.push(value) | |
367 | } else { | |
368 | acc = acc.concat(this.grabAllErrors(value)) | |
369 | } | |
370 | } | |
371 | ||
372 | return acc | |
373 | } | |
374 | ||
375 | private updateForm () { | |
376 | this.form.patchValue(this.customConfig) | |
377 | } | |
378 | ||
379 | private loadConfigAndUpdateForm () { | |
380 | forkJoin([ | |
381 | this.configService.getCustomConfig(), | |
382 | this.customPage.getInstanceHomepage() | |
383 | ]).subscribe({ | |
384 | next: ([ config, homepage ]) => { | |
385 | this.customConfig = { ...config, instanceCustomHomepage: homepage } | |
386 | ||
387 | this.updateForm() | |
388 | this.markAllAsDirty() | |
389 | }, | |
390 | ||
391 | error: err => this.notifier.error(err.message) | |
392 | }) | |
393 | } | |
394 | ||
395 | private loadCategoriesAndLanguages () { | |
396 | forkJoin([ | |
397 | this.serverService.getVideoLanguages(), | |
398 | this.serverService.getVideoCategories() | |
399 | ]).subscribe({ | |
400 | next: ([ languages, categories ]) => { | |
401 | this.languageItems = languages.map(l => ({ label: l.label, id: l.id })) | |
402 | this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' })) | |
403 | }, | |
404 | ||
405 | error: err => this.notifier.error(err.message) | |
406 | }) | |
407 | } | |
408 | } |