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