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