]>
Commit | Line | Data |
---|---|---|
ccc00cb2 | 1 | import { forkJoin } from 'rxjs' |
45e0d669 | 2 | import { ViewportScroller } from '@angular/common' |
67ed6552 C |
3 | import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core' |
4 | import { ConfigService } from '@app/+admin/config/shared/config.service' | |
5 | import { Notifier } from '@app/core' | |
6 | import { ServerService } from '@app/core/server/server.service' | |
52c4976f C |
7 | import { |
8 | CustomConfigValidatorsService, | |
9 | FormReactive, | |
10 | FormValidatorService, | |
11 | SelectOptionsItem, | |
12 | UserValidatorsService | |
13 | } from '@app/shared/shared-forms' | |
45c6bcf3 | 14 | import { NgbNav } from '@ng-bootstrap/ng-bootstrap' |
67ed6552 C |
15 | import { I18n } from '@ngx-translate/i18n-polyfill' |
16 | import { CustomConfig, ServerConfig } from '@shared/models' | |
fd206f0b C |
17 | |
18 | @Component({ | |
19 | selector: 'my-edit-custom-config', | |
20 | templateUrl: './edit-custom-config.component.html', | |
21 | styleUrls: [ './edit-custom-config.component.scss' ] | |
22 | }) | |
45e0d669 | 23 | export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked { |
45c6bcf3 C |
24 | // FIXME: use built-in router |
25 | @ViewChild('nav') nav: NgbNav | |
45e0d669 RK |
26 | |
27 | initDone = false | |
bee0abff | 28 | customConfig: CustomConfig |
bee0abff | 29 | |
46db9430 | 30 | resolutions: { id: string, label: string, description?: string }[] = [] |
3827c3b3 | 31 | transcodingThreadOptions: { label: string, value: number }[] = [] |
fd206f0b | 32 | |
52c4976f C |
33 | languageItems: SelectOptionsItem[] = [] |
34 | categoryItems: SelectOptionsItem[] = [] | |
ccc00cb2 | 35 | |
ba430d75 C |
36 | private serverConfig: ServerConfig |
37 | ||
fd206f0b | 38 | constructor ( |
45e0d669 | 39 | private viewportScroller: ViewportScroller, |
d18d6478 | 40 | protected formValidatorService: FormValidatorService, |
e309822b C |
41 | private customConfigValidatorsService: CustomConfigValidatorsService, |
42 | private userValidatorsService: UserValidatorsService, | |
f8b2c1b4 | 43 | private notifier: Notifier, |
fd206f0b | 44 | private configService: ConfigService, |
1f30a185 | 45 | private serverService: ServerService, |
b1d40cff | 46 | private i18n: I18n |
fd206f0b C |
47 | ) { |
48 | super() | |
3827c3b3 C |
49 | |
50 | this.resolutions = [ | |
2fa9c40e | 51 | { |
5c7d6508 | 52 | id: '0p', |
46db9430 | 53 | label: this.i18n('Audio-only'), |
6a07a058 | 54 | description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video') |
5c7d6508 | 55 | }, |
00aa1f0d C |
56 | { |
57 | id: '240p', | |
58 | label: this.i18n('240p') | |
59 | }, | |
60 | { | |
61 | id: '360p', | |
62 | label: this.i18n('360p') | |
63 | }, | |
64 | { | |
65 | id: '480p', | |
66 | label: this.i18n('480p') | |
67 | }, | |
68 | { | |
69 | id: '720p', | |
70 | label: this.i18n('720p') | |
71 | }, | |
72 | { | |
73 | id: '1080p', | |
74 | label: this.i18n('1080p') | |
75 | }, | |
76 | { | |
77 | id: '2160p', | |
78 | label: this.i18n('2160p') | |
79 | } | |
3827c3b3 C |
80 | ] |
81 | ||
82 | this.transcodingThreadOptions = [ | |
83 | { value: 0, label: this.i18n('Auto (via ffmpeg)') }, | |
84 | { value: 1, label: '1' }, | |
85 | { value: 2, label: '2' }, | |
86 | { value: 4, label: '4' }, | |
87 | { value: 8, label: '8' } | |
88 | ] | |
fd206f0b C |
89 | } |
90 | ||
41a676db | 91 | get videoQuotaOptions () { |
3827c3b3 | 92 | return this.configService.videoQuotaOptions |
41a676db C |
93 | } |
94 | ||
95 | get videoQuotaDailyOptions () { | |
3827c3b3 | 96 | return this.configService.videoQuotaDailyOptions |
41a676db C |
97 | } |
98 | ||
7cd4d2ba | 99 | get availableThemes () { |
ba430d75 | 100 | return this.serverConfig.theme.registered |
ffb321be | 101 | .map(t => t.name) |
7cd4d2ba C |
102 | } |
103 | ||
fd206f0b | 104 | getResolutionKey (resolution: string) { |
3866f1a0 | 105 | return 'transcoding.resolutions.' + resolution |
fd206f0b C |
106 | } |
107 | ||
d18d6478 | 108 | ngOnInit () { |
ba430d75 C |
109 | this.serverConfig = this.serverService.getTmpConfig() |
110 | this.serverService.getConfig() | |
111 | .subscribe(config => this.serverConfig = config) | |
112 | ||
3866f1a0 C |
113 | const formGroupData: { [key in keyof CustomConfig ]: any } = { |
114 | instance: { | |
115 | name: this.customConfigValidatorsService.INSTANCE_NAME, | |
116 | shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION, | |
117 | description: null, | |
ccc00cb2 | 118 | |
f8802489 | 119 | isNSFW: false, |
3866f1a0 | 120 | defaultNSFWPolicy: null, |
ccc00cb2 C |
121 | |
122 | terms: null, | |
123 | codeOfConduct: null, | |
8ae03c37 C |
124 | |
125 | creationReason: null, | |
ccc00cb2 C |
126 | moderationInformation: null, |
127 | administrator: null, | |
128 | maintenanceLifetime: null, | |
129 | businessModel: null, | |
130 | ||
be04c6fd C |
131 | hardwareInformation: null, |
132 | ||
ccc00cb2 C |
133 | categories: null, |
134 | languages: null, | |
135 | ||
136 | defaultClientRoute: null, | |
137 | ||
3866f1a0 C |
138 | customizations: { |
139 | javascript: null, | |
140 | css: null | |
141 | } | |
142 | }, | |
7cd4d2ba C |
143 | theme: { |
144 | default: null | |
145 | }, | |
3866f1a0 C |
146 | services: { |
147 | twitter: { | |
148 | username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME, | |
149 | whitelisted: null | |
150 | } | |
151 | }, | |
152 | cache: { | |
153 | previews: { | |
154 | size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE | |
155 | }, | |
156 | captions: { | |
157 | size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE | |
158 | } | |
159 | }, | |
160 | signup: { | |
161 | enabled: null, | |
162 | limit: this.customConfigValidatorsService.SIGNUP_LIMIT, | |
163 | requiresEmailVerification: null | |
164 | }, | |
165 | import: { | |
166 | videos: { | |
167 | http: { | |
168 | enabled: null | |
169 | }, | |
170 | torrent: { | |
171 | enabled: null | |
172 | } | |
173 | } | |
174 | }, | |
175 | admin: { | |
176 | email: this.customConfigValidatorsService.ADMIN_EMAIL | |
177 | }, | |
178 | contactForm: { | |
179 | enabled: null | |
180 | }, | |
181 | user: { | |
182 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, | |
183 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY | |
184 | }, | |
185 | transcoding: { | |
186 | enabled: null, | |
187 | threads: this.customConfigValidatorsService.TRANSCODING_THREADS, | |
188 | allowAdditionalExtensions: null, | |
536598cf | 189 | allowAudioFiles: null, |
5d9e4eaa C |
190 | resolutions: {}, |
191 | hls: { | |
192 | enabled: null | |
5a71acd2 C |
193 | }, |
194 | webtorrent: { | |
195 | enabled: null | |
5d9e4eaa | 196 | } |
7ccddd7b JM |
197 | }, |
198 | autoBlacklist: { | |
199 | videos: { | |
200 | ofUsers: { | |
201 | enabled: null | |
202 | } | |
203 | } | |
0dc64777 C |
204 | }, |
205 | followers: { | |
206 | instance: { | |
207 | enabled: null, | |
208 | manualApproval: null | |
209 | } | |
e1b49ee5 C |
210 | }, |
211 | followings: { | |
212 | instance: { | |
213 | autoFollowBack: { | |
214 | enabled: null | |
215 | }, | |
216 | autoFollowIndex: { | |
217 | enabled: null, | |
218 | indexUrl: this.customConfigValidatorsService.INDEX_URL | |
219 | } | |
220 | } | |
72c33e71 C |
221 | }, |
222 | broadcastMessage: { | |
223 | enabled: null, | |
224 | level: null, | |
225 | dismissable: null, | |
226 | message: null | |
5fb2e288 C |
227 | }, |
228 | search: { | |
229 | remoteUri: { | |
230 | users: null, | |
231 | anonymous: null | |
232 | }, | |
233 | searchIndex: { | |
234 | enabled: null, | |
235 | url: this.customConfigValidatorsService.SEARCH_INDEX_URL, | |
236 | disableLocalSearch: null, | |
237 | isDefaultSearch: null | |
238 | } | |
3866f1a0 | 239 | } |
fd206f0b C |
240 | } |
241 | ||
3866f1a0 C |
242 | const defaultValues = { |
243 | transcoding: { | |
244 | resolutions: {} | |
245 | } | |
246 | } | |
fd206f0b | 247 | for (const resolution of this.resolutions) { |
00aa1f0d C |
248 | defaultValues.transcoding.resolutions[resolution.id] = 'false' |
249 | formGroupData.transcoding.resolutions[resolution.id] = null | |
fd206f0b C |
250 | } |
251 | ||
d18d6478 | 252 | this.buildForm(formGroupData) |
04cda1d7 C |
253 | this.loadForm() |
254 | this.checkTranscodingFields() | |
fd206f0b C |
255 | } |
256 | ||
45e0d669 RK |
257 | ngAfterViewChecked () { |
258 | if (!this.initDone) { | |
259 | this.initDone = true | |
260 | this.gotoAnchor() | |
261 | } | |
262 | } | |
263 | ||
fd206f0b | 264 | isTranscodingEnabled () { |
3866f1a0 | 265 | return this.form.value['transcoding']['enabled'] === true |
fd206f0b C |
266 | } |
267 | ||
268 | isSignupEnabled () { | |
3866f1a0 | 269 | return this.form.value['signup']['enabled'] === true |
fd206f0b C |
270 | } |
271 | ||
5fb2e288 C |
272 | isSearchIndexEnabled () { |
273 | return this.form.value['search']['searchIndex']['enabled'] === true | |
274 | } | |
275 | ||
4ee6a8b1 C |
276 | isAutoFollowIndexEnabled () { |
277 | return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true | |
278 | } | |
279 | ||
1f30a185 | 280 | async formValidated () { |
04cda1d7 | 281 | this.configService.updateCustomConfig(this.form.getRawValue()) |
fd206f0b C |
282 | .subscribe( |
283 | res => { | |
284 | this.customConfig = res | |
285 | ||
286 | // Reload general configuration | |
ba430d75 | 287 | this.serverService.resetConfig() |
fd206f0b C |
288 | |
289 | this.updateForm() | |
66b16caf | 290 | |
f8b2c1b4 | 291 | this.notifier.success(this.i18n('Configuration updated.')) |
fd206f0b C |
292 | }, |
293 | ||
f8b2c1b4 | 294 | err => this.notifier.error(err.message) |
fd206f0b C |
295 | ) |
296 | } | |
297 | ||
45e0d669 | 298 | gotoAnchor () { |
45c6bcf3 | 299 | const hashToNav = { |
45e0d669 RK |
300 | 'customizations': 'advanced-configuration' |
301 | } | |
302 | const hash = window.location.hash.replace('#', '') | |
303 | ||
45c6bcf3 C |
304 | if (hash && Object.keys(hashToNav).includes(hash)) { |
305 | this.nav.select(hashToNav[hash]) | |
45e0d669 RK |
306 | setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100) |
307 | } | |
308 | } | |
309 | ||
fd206f0b | 310 | private updateForm () { |
3866f1a0 | 311 | this.form.patchValue(this.customConfig) |
fd206f0b | 312 | } |
04cda1d7 C |
313 | |
314 | private loadForm () { | |
315 | forkJoin([ | |
316 | this.configService.getCustomConfig(), | |
317 | this.serverService.getVideoLanguages(), | |
318 | this.serverService.getVideoCategories() | |
319 | ]).subscribe( | |
320 | ([ config, languages, categories ]) => { | |
321 | this.customConfig = config | |
322 | ||
52c4976f C |
323 | this.languageItems = languages.map(l => ({ label: l.label, id: l.id })) |
324 | this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' })) | |
04cda1d7 C |
325 | |
326 | this.updateForm() | |
327 | // Force form validation | |
328 | this.forceCheck() | |
329 | }, | |
330 | ||
331 | err => this.notifier.error(err.message) | |
332 | ) | |
333 | } | |
334 | ||
335 | private checkTranscodingFields () { | |
336 | const hlsControl = this.form.get('transcoding.hls.enabled') | |
337 | const webtorrentControl = this.form.get('transcoding.webtorrent.enabled') | |
338 | ||
339 | webtorrentControl.valueChanges | |
340 | .subscribe(newValue => { | |
341 | if (newValue === false && !hlsControl.disabled) { | |
342 | hlsControl.disable() | |
343 | } | |
344 | ||
345 | if (newValue === true && !hlsControl.enabled) { | |
346 | hlsControl.enable() | |
347 | } | |
348 | }) | |
349 | ||
350 | hlsControl.valueChanges | |
351 | .subscribe(newValue => { | |
352 | if (newValue === false && !webtorrentControl.disabled) { | |
353 | webtorrentControl.disable() | |
354 | } | |
355 | ||
356 | if (newValue === true && !webtorrentControl.enabled) { | |
357 | webtorrentControl.enable() | |
358 | } | |
359 | }) | |
360 | } | |
fd206f0b | 361 | } |