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