]>
Commit | Line | Data |
---|---|---|
1 | import { forkJoin } from 'rxjs' | |
2 | import { map } from 'rxjs/operators' | |
3 | import { SelectChannelItem } from 'src/types/select-options-item.model' | |
4 | import { ChangeDetectorRef, Component, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output, ViewChild } from '@angular/core' | |
5 | import { AbstractControl, FormArray, FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms' | |
6 | import { HooksService, PluginService, ServerService } from '@app/core' | |
7 | import { removeElementFromArray } from '@app/helpers' | |
8 | import { BuildFormValidator } from '@app/shared/form-validators' | |
9 | import { | |
10 | VIDEO_CATEGORY_VALIDATOR, | |
11 | VIDEO_CHANNEL_VALIDATOR, | |
12 | VIDEO_DESCRIPTION_VALIDATOR, | |
13 | VIDEO_LANGUAGE_VALIDATOR, | |
14 | VIDEO_LICENCE_VALIDATOR, | |
15 | VIDEO_NAME_VALIDATOR, | |
16 | VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR, | |
17 | VIDEO_PRIVACY_VALIDATOR, | |
18 | VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR, | |
19 | VIDEO_SUPPORT_VALIDATOR, | |
20 | VIDEO_TAGS_ARRAY_VALIDATOR | |
21 | } from '@app/shared/form-validators/video-validators' | |
22 | import { FormReactiveValidationMessages, FormValidatorService } from '@app/shared/shared-forms' | |
23 | import { InstanceService } from '@app/shared/shared-instance' | |
24 | import { VideoCaptionEdit, VideoEdit, VideoService } from '@app/shared/shared-main' | |
25 | import { | |
26 | HTMLServerConfig, | |
27 | LiveVideo, | |
28 | RegisterClientFormFieldOptions, | |
29 | RegisterClientVideoFieldOptions, | |
30 | VideoConstant, | |
31 | VideoDetails, | |
32 | VideoPrivacy | |
33 | } from '@shared/models' | |
34 | import { I18nPrimengCalendarService } from './i18n-primeng-calendar.service' | |
35 | import { VideoCaptionAddModalComponent } from './video-caption-add-modal.component' | |
36 | import { VideoEditType } from './video-edit.type' | |
37 | ||
38 | type VideoLanguages = VideoConstant<string> & { group?: string } | |
39 | type PluginField = { | |
40 | commonOptions: RegisterClientFormFieldOptions | |
41 | videoFormOptions: RegisterClientVideoFieldOptions | |
42 | } | |
43 | ||
44 | @Component({ | |
45 | selector: 'my-video-edit', | |
46 | styleUrls: [ './video-edit.component.scss' ], | |
47 | templateUrl: './video-edit.component.html' | |
48 | }) | |
49 | export class VideoEditComponent implements OnInit, OnDestroy { | |
50 | @Input() form: FormGroup | |
51 | @Input() formErrors: { [ id: string ]: string } = {} | |
52 | @Input() validationMessages: FormReactiveValidationMessages = {} | |
53 | ||
54 | @Input() videoToUpdate: VideoDetails | |
55 | ||
56 | @Input() userVideoChannels: SelectChannelItem[] = [] | |
57 | @Input() schedulePublicationPossible = true | |
58 | ||
59 | @Input() videoCaptions: (VideoCaptionEdit & { captionPath?: string })[] = [] | |
60 | ||
61 | @Input() waitTranscodingEnabled = true | |
62 | @Input() type: VideoEditType | |
63 | @Input() liveVideo: LiveVideo | |
64 | ||
65 | @ViewChild('videoCaptionAddModal', { static: true }) videoCaptionAddModal: VideoCaptionAddModalComponent | |
66 | ||
67 | @Output() formBuilt = new EventEmitter<void>() | |
68 | @Output() pluginFieldsAdded = new EventEmitter<void>() | |
69 | ||
70 | // So that it can be accessed in the template | |
71 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
72 | ||
73 | videoPrivacies: VideoConstant<VideoPrivacy>[] = [] | |
74 | videoCategories: VideoConstant<number>[] = [] | |
75 | videoLicences: VideoConstant<number>[] = [] | |
76 | videoLanguages: VideoLanguages[] = [] | |
77 | ||
78 | pluginDataFormGroup: FormGroup | |
79 | ||
80 | schedulePublicationEnabled = false | |
81 | ||
82 | calendarLocale: any = {} | |
83 | minScheduledDate = new Date() | |
84 | myYearRange = '1880:' + (new Date()).getFullYear() | |
85 | ||
86 | calendarTimezone: string | |
87 | calendarDateFormat: string | |
88 | ||
89 | serverConfig: HTMLServerConfig | |
90 | ||
91 | pluginFields: PluginField[] = [] | |
92 | ||
93 | private schedulerInterval: any | |
94 | private firstPatchDone = false | |
95 | private initialVideoCaptions: string[] = [] | |
96 | ||
97 | constructor ( | |
98 | private formValidatorService: FormValidatorService, | |
99 | private videoService: VideoService, | |
100 | private serverService: ServerService, | |
101 | private pluginService: PluginService, | |
102 | private instanceService: InstanceService, | |
103 | private i18nPrimengCalendarService: I18nPrimengCalendarService, | |
104 | private ngZone: NgZone, | |
105 | private hooks: HooksService, | |
106 | private cd: ChangeDetectorRef | |
107 | ) { | |
108 | this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone() | |
109 | this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat() | |
110 | } | |
111 | ||
112 | updateForm () { | |
113 | const defaultValues: any = { | |
114 | nsfw: 'false', | |
115 | commentsEnabled: this.serverConfig.defaults.publish.commentsEnabled, | |
116 | downloadEnabled: this.serverConfig.defaults.publish.downloadEnabled, | |
117 | waitTranscoding: 'true', | |
118 | licence: this.serverConfig.defaults.publish.licence, | |
119 | tags: [] | |
120 | } | |
121 | const obj: { [ id: string ]: BuildFormValidator } = { | |
122 | name: VIDEO_NAME_VALIDATOR, | |
123 | privacy: VIDEO_PRIVACY_VALIDATOR, | |
124 | channelId: VIDEO_CHANNEL_VALIDATOR, | |
125 | nsfw: null, | |
126 | commentsEnabled: null, | |
127 | downloadEnabled: null, | |
128 | waitTranscoding: null, | |
129 | category: VIDEO_CATEGORY_VALIDATOR, | |
130 | licence: VIDEO_LICENCE_VALIDATOR, | |
131 | language: VIDEO_LANGUAGE_VALIDATOR, | |
132 | description: VIDEO_DESCRIPTION_VALIDATOR, | |
133 | tags: VIDEO_TAGS_ARRAY_VALIDATOR, | |
134 | previewfile: null, | |
135 | support: VIDEO_SUPPORT_VALIDATOR, | |
136 | schedulePublicationAt: VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR, | |
137 | originallyPublishedAt: VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR, | |
138 | liveStreamKey: null, | |
139 | permanentLive: null, | |
140 | saveReplay: null | |
141 | } | |
142 | ||
143 | this.formValidatorService.updateFormGroup( | |
144 | this.form, | |
145 | this.formErrors, | |
146 | this.validationMessages, | |
147 | obj, | |
148 | defaultValues | |
149 | ) | |
150 | ||
151 | this.form.addControl('captions', new FormArray([ | |
152 | new FormGroup({ | |
153 | language: new FormControl(), | |
154 | captionfile: new FormControl() | |
155 | }) | |
156 | ])) | |
157 | ||
158 | this.trackChannelChange() | |
159 | this.trackPrivacyChange() | |
160 | this.trackLivePermanentFieldChange() | |
161 | ||
162 | this.formBuilt.emit() | |
163 | } | |
164 | ||
165 | ngOnInit () { | |
166 | this.serverConfig = this.serverService.getHTMLConfig() | |
167 | ||
168 | this.updateForm() | |
169 | ||
170 | this.pluginService.ensurePluginsAreLoaded('video-edit') | |
171 | .then(() => this.updatePluginFields()) | |
172 | ||
173 | this.serverService.getVideoCategories() | |
174 | .subscribe(res => this.videoCategories = res) | |
175 | ||
176 | this.serverService.getVideoLicences() | |
177 | .subscribe(res => this.videoLicences = res) | |
178 | ||
179 | forkJoin([ | |
180 | this.instanceService.getAbout(), | |
181 | this.serverService.getVideoLanguages() | |
182 | ]).pipe(map(([ about, languages ]) => ({ about, languages }))) | |
183 | .subscribe(res => { | |
184 | this.videoLanguages = res.languages | |
185 | .map(l => { | |
186 | if (l.id === 'zxx') return { ...l, group: $localize`Other`, groupOrder: 1 } | |
187 | ||
188 | return res.about.instance.languages.includes(l.id) | |
189 | ? { ...l, group: $localize`Instance languages`, groupOrder: 0 } | |
190 | : { ...l, group: $localize`All languages`, groupOrder: 2 } | |
191 | }) | |
192 | .sort((a, b) => a.groupOrder - b.groupOrder) | |
193 | }) | |
194 | ||
195 | this.serverService.getVideoPrivacies() | |
196 | .subscribe(privacies => { | |
197 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies).videoPrivacies | |
198 | ||
199 | if (this.schedulePublicationPossible) { | |
200 | this.videoPrivacies.push({ | |
201 | id: this.SPECIAL_SCHEDULED_PRIVACY, | |
202 | label: $localize`Scheduled`, | |
203 | description: $localize`Hide the video until a specific date` | |
204 | }) | |
205 | } | |
206 | }) | |
207 | ||
208 | this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id) | |
209 | ||
210 | this.ngZone.runOutsideAngular(() => { | |
211 | this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute | |
212 | }) | |
213 | ||
214 | this.hooks.runAction('action:video-edit.init', 'video-edit', { type: this.type }) | |
215 | } | |
216 | ||
217 | ngOnDestroy () { | |
218 | if (this.schedulerInterval) clearInterval(this.schedulerInterval) | |
219 | } | |
220 | ||
221 | getExistingCaptions () { | |
222 | return this.videoCaptions | |
223 | .filter(c => c.action !== 'REMOVE') | |
224 | .map(c => c.language.id) | |
225 | } | |
226 | ||
227 | onCaptionAdded (caption: VideoCaptionEdit) { | |
228 | const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id) | |
229 | ||
230 | // Replace existing caption? | |
231 | if (existingCaption) { | |
232 | Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' }) | |
233 | } else { | |
234 | this.videoCaptions.push( | |
235 | Object.assign(caption, { action: 'CREATE' as 'CREATE' }) | |
236 | ) | |
237 | } | |
238 | ||
239 | this.sortVideoCaptions() | |
240 | } | |
241 | ||
242 | deleteCaption (caption: VideoCaptionEdit) { | |
243 | // Caption recovers his former state | |
244 | if (caption.action && this.initialVideoCaptions.includes(caption.language.id)) { | |
245 | caption.action = undefined | |
246 | return | |
247 | } | |
248 | ||
249 | // This caption is not on the server, just remove it from our array | |
250 | if (caption.action === 'CREATE') { | |
251 | removeElementFromArray(this.videoCaptions, caption) | |
252 | return | |
253 | } | |
254 | ||
255 | caption.action = 'REMOVE' as 'REMOVE' | |
256 | } | |
257 | ||
258 | openAddCaptionModal () { | |
259 | this.videoCaptionAddModal.show() | |
260 | } | |
261 | ||
262 | isSaveReplayEnabled () { | |
263 | return this.serverConfig.live.allowReplay | |
264 | } | |
265 | ||
266 | isPermanentLiveEnabled () { | |
267 | return this.form.value['permanentLive'] === true | |
268 | } | |
269 | ||
270 | isPluginFieldHidden (pluginField: PluginField) { | |
271 | if (typeof pluginField.commonOptions.hidden !== 'function') return false | |
272 | ||
273 | return pluginField.commonOptions.hidden({ | |
274 | formValues: this.form.value, | |
275 | videoToUpdate: this.videoToUpdate, | |
276 | liveVideo: this.liveVideo | |
277 | }) | |
278 | } | |
279 | ||
280 | getPluginsFields (tab: 'main' | 'plugin-settings') { | |
281 | return this.pluginFields.filter(p => { | |
282 | const wanted = p.videoFormOptions.tab ?? 'plugin-settings' | |
283 | ||
284 | return wanted === tab | |
285 | }) | |
286 | } | |
287 | ||
288 | private sortVideoCaptions () { | |
289 | this.videoCaptions.sort((v1, v2) => { | |
290 | if (v1.language.label < v2.language.label) return -1 | |
291 | if (v1.language.label === v2.language.label) return 0 | |
292 | ||
293 | return 1 | |
294 | }) | |
295 | } | |
296 | ||
297 | private updatePluginFields () { | |
298 | this.pluginFields = this.pluginService.getRegisteredVideoFormFields(this.type) | |
299 | ||
300 | if (this.pluginFields.length === 0) return | |
301 | ||
302 | const pluginObj: { [ id: string ]: BuildFormValidator } = {} | |
303 | const pluginValidationMessages: FormReactiveValidationMessages = {} | |
304 | const pluginFormErrors: any = {} | |
305 | const pluginDefaults: any = {} | |
306 | ||
307 | for (const setting of this.pluginFields) { | |
308 | const validator = (control: AbstractControl): ValidationErrors | null => { | |
309 | if (!setting.commonOptions.error) return null | |
310 | ||
311 | const error = setting.commonOptions.error({ formValues: this.form.value, value: control.value }) | |
312 | ||
313 | return error?.error ? { [setting.commonOptions.name]: error.text } : null | |
314 | } | |
315 | ||
316 | const name = setting.commonOptions.name | |
317 | ||
318 | pluginObj[name] = { | |
319 | VALIDATORS: [ validator ], | |
320 | MESSAGES: {} | |
321 | } | |
322 | ||
323 | pluginDefaults[name] = setting.commonOptions.default | |
324 | } | |
325 | ||
326 | this.pluginDataFormGroup = new FormGroup({}) | |
327 | this.formValidatorService.updateFormGroup( | |
328 | this.pluginDataFormGroup, | |
329 | pluginFormErrors, | |
330 | pluginValidationMessages, | |
331 | pluginObj, | |
332 | pluginDefaults | |
333 | ) | |
334 | ||
335 | this.form.addControl('pluginData', this.pluginDataFormGroup) | |
336 | this.formErrors['pluginData'] = pluginFormErrors | |
337 | this.validationMessages['pluginData'] = pluginValidationMessages | |
338 | ||
339 | this.cd.detectChanges() | |
340 | this.pluginFieldsAdded.emit() | |
341 | } | |
342 | ||
343 | private trackPrivacyChange () { | |
344 | // We will update the schedule input and the wait transcoding checkbox validators | |
345 | this.form.controls['privacy'] | |
346 | .valueChanges | |
347 | .pipe(map(res => parseInt(res.toString(), 10))) | |
348 | .subscribe( | |
349 | newPrivacyId => { | |
350 | ||
351 | this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY | |
352 | ||
353 | // Value changed | |
354 | const scheduleControl = this.form.get('schedulePublicationAt') | |
355 | const waitTranscodingControl = this.form.get('waitTranscoding') | |
356 | ||
357 | if (this.schedulePublicationEnabled) { | |
358 | scheduleControl.setValidators([ Validators.required ]) | |
359 | ||
360 | waitTranscodingControl.disable() | |
361 | waitTranscodingControl.setValue(false) | |
362 | } else { | |
363 | scheduleControl.clearValidators() | |
364 | ||
365 | waitTranscodingControl.enable() | |
366 | ||
367 | // Do not update the control value on first patch (values come from the server) | |
368 | if (this.firstPatchDone === true) { | |
369 | waitTranscodingControl.setValue(true) | |
370 | } | |
371 | } | |
372 | ||
373 | scheduleControl.updateValueAndValidity() | |
374 | waitTranscodingControl.updateValueAndValidity() | |
375 | ||
376 | this.firstPatchDone = true | |
377 | ||
378 | } | |
379 | ) | |
380 | } | |
381 | ||
382 | private trackChannelChange () { | |
383 | // We will update the "support" field depending on the channel | |
384 | this.form.controls['channelId'] | |
385 | .valueChanges | |
386 | .pipe(map(res => parseInt(res.toString(), 10))) | |
387 | .subscribe( | |
388 | newChannelId => { | |
389 | const oldChannelId = parseInt(this.form.value['channelId'], 10) | |
390 | ||
391 | // Not initialized yet | |
392 | if (isNaN(newChannelId)) return | |
393 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
394 | if (!newChannel) return | |
395 | ||
396 | // Wait support field update | |
397 | setTimeout(() => { | |
398 | const currentSupport = this.form.value['support'] | |
399 | ||
400 | // First time we set the channel? | |
401 | if (isNaN(oldChannelId)) { | |
402 | // Fill support if it's empty | |
403 | if (!currentSupport) this.updateSupportField(newChannel.support) | |
404 | ||
405 | return | |
406 | } | |
407 | ||
408 | const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId) | |
409 | if (!newChannel || !oldChannel) { | |
410 | console.error('Cannot find new or old channel.') | |
411 | return | |
412 | } | |
413 | ||
414 | // If the current support text is not the same than the old channel, the user updated it. | |
415 | // We don't want the user to lose his text, so stop here | |
416 | if (currentSupport && currentSupport !== oldChannel.support) return | |
417 | ||
418 | // Update the support text with our new channel | |
419 | this.updateSupportField(newChannel.support) | |
420 | }) | |
421 | } | |
422 | ) | |
423 | } | |
424 | ||
425 | private trackLivePermanentFieldChange () { | |
426 | // We will update the "support" field depending on the channel | |
427 | this.form.controls['permanentLive'] | |
428 | .valueChanges | |
429 | .subscribe( | |
430 | permanentLive => { | |
431 | const saveReplayControl = this.form.controls['saveReplay'] | |
432 | ||
433 | if (permanentLive === true) { | |
434 | saveReplayControl.setValue(false) | |
435 | saveReplayControl.disable() | |
436 | } else { | |
437 | saveReplayControl.enable() | |
438 | } | |
439 | } | |
440 | ) | |
441 | } | |
442 | ||
443 | private updateSupportField (support: string) { | |
444 | return this.form.patchValue({ support: support || '' }) | |
445 | } | |
446 | } |