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