]>
Commit | Line | Data |
---|---|---|
9abd170d | 1 | import { forkJoin } from 'rxjs' |
67ed6552 | 2 | import { map } from 'rxjs/operators' |
21e493d4 | 3 | import { SelectChannelItem } from 'src/types/select-options-item.model' |
3c065fe3 C |
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' | |
7294aab0 | 6 | import { HooksService, PluginService, ServerService } from '@app/core' |
67ed6552 | 7 | import { removeElementFromArray } from '@app/helpers' |
3c065fe3 | 8 | import { BuildFormValidator } from '@app/shared/form-validators' |
7ed1edbb C |
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' | |
21e493d4 | 22 | import { FormReactiveValidationMessages, FormValidatorService } from '@app/shared/shared-forms' |
9abd170d | 23 | import { InstanceService } from '@app/shared/shared-instance' |
67ed6552 | 24 | import { VideoCaptionEdit, VideoEdit, VideoService } from '@app/shared/shared-main' |
428ccb8b | 25 | import { |
2989628b | 26 | HTMLServerConfig, |
428ccb8b C |
27 | LiveVideo, |
28 | RegisterClientFormFieldOptions, | |
29 | RegisterClientVideoFieldOptions, | |
428ccb8b C |
30 | VideoConstant, |
31 | VideoDetails, | |
32 | VideoPrivacy | |
33 | } from '@shared/models' | |
67ed6552 C |
34 | import { I18nPrimengCalendarService } from './i18n-primeng-calendar.service' |
35 | import { VideoCaptionAddModalComponent } from './video-caption-add-modal.component' | |
c6c0fa6c | 36 | import { VideoEditType } from './video-edit.type' |
02c01341 RK |
37 | |
38 | type VideoLanguages = VideoConstant<string> & { group?: string } | |
0f319334 C |
39 | type PluginField = { |
40 | commonOptions: RegisterClientFormFieldOptions | |
41 | videoFormOptions: RegisterClientVideoFieldOptions | |
42 | } | |
ff249f49 C |
43 | |
44 | @Component({ | |
45 | selector: 'my-video-edit', | |
46 | styleUrls: [ './video-edit.component.scss' ], | |
47 | templateUrl: './video-edit.component.html' | |
48 | }) | |
40e87e9e | 49 | export class VideoEditComponent implements OnInit, OnDestroy { |
ff249f49 C |
50 | @Input() form: FormGroup |
51 | @Input() formErrors: { [ id: string ]: string } = {} | |
e309822b | 52 | @Input() validationMessages: FormReactiveValidationMessages = {} |
0f319334 C |
53 | |
54 | @Input() videoToUpdate: VideoDetails | |
55 | ||
9abd170d | 56 | @Input() userVideoChannels: SelectChannelItem[] = [] |
bbe0f064 | 57 | @Input() schedulePublicationPossible = true |
0f319334 | 58 | |
6913f691 | 59 | @Input() videoCaptions: (VideoCaptionEdit & { captionPath?: string })[] = [] |
0f319334 | 60 | |
14e2014a | 61 | @Input() waitTranscodingEnabled = true |
c6c0fa6c | 62 | @Input() type: VideoEditType |
a5cf76af | 63 | @Input() liveVideo: LiveVideo |
40e87e9e | 64 | |
f36da21e | 65 | @ViewChild('videoCaptionAddModal', { static: true }) videoCaptionAddModal: VideoCaptionAddModalComponent |
bbe0f064 | 66 | |
0ba9696c | 67 | @Output() formBuilt = new EventEmitter<void>() |
7294aab0 C |
68 | @Output() pluginFieldsAdded = new EventEmitter<void>() |
69 | ||
bbe0f064 C |
70 | // So that it can be accessed in the template |
71 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
ff249f49 | 72 | |
851f5daa | 73 | videoPrivacies: VideoConstant<VideoPrivacy>[] = [] |
8cd7faaa C |
74 | videoCategories: VideoConstant<number>[] = [] |
75 | videoLicences: VideoConstant<number>[] = [] | |
02c01341 | 76 | videoLanguages: VideoLanguages[] = [] |
ff249f49 | 77 | |
7294aab0 C |
78 | pluginDataFormGroup: FormGroup |
79 | ||
bbe0f064 C |
80 | schedulePublicationEnabled = false |
81 | ||
bbe0f064 C |
82 | calendarLocale: any = {} |
83 | minScheduledDate = new Date() | |
1e74f19a | 84 | myYearRange = '1880:' + (new Date()).getFullYear() |
bbe0f064 C |
85 | |
86 | calendarTimezone: string | |
87 | calendarDateFormat: string | |
ff249f49 | 88 | |
2989628b | 89 | serverConfig: HTMLServerConfig |
ba430d75 | 90 | |
0f319334 | 91 | pluginFields: PluginField[] = [] |
7294aab0 | 92 | |
244b4ae3 | 93 | private schedulerInterval: any |
0f7fedc3 | 94 | private firstPatchDone = false |
772d5642 | 95 | private initialVideoCaptions: string[] = [] |
40e87e9e | 96 | |
ff249f49 | 97 | constructor ( |
d18d6478 | 98 | private formValidatorService: FormValidatorService, |
851f5daa | 99 | private videoService: VideoService, |
bbe0f064 | 100 | private serverService: ServerService, |
7294aab0 | 101 | private pluginService: PluginService, |
02c01341 | 102 | private instanceService: InstanceService, |
84c7cde6 | 103 | private i18nPrimengCalendarService: I18nPrimengCalendarService, |
7294aab0 | 104 | private ngZone: NgZone, |
3c065fe3 C |
105 | private hooks: HooksService, |
106 | private cd: ChangeDetectorRef | |
e309822b | 107 | ) { |
bbe0f064 C |
108 | this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone() |
109 | this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat() | |
e309822b | 110 | } |
ff249f49 C |
111 | |
112 | updateForm () { | |
244b4ae3 | 113 | const defaultValues: any = { |
d18d6478 | 114 | nsfw: 'false', |
3cf68b86 C |
115 | commentsEnabled: this.serverConfig.defaults.publish.commentsEnabled, |
116 | downloadEnabled: this.serverConfig.defaults.publish.downloadEnabled, | |
2186386c | 117 | waitTranscoding: 'true', |
3cf68b86 | 118 | licence: this.serverConfig.defaults.publish.licence, |
d18d6478 C |
119 | tags: [] |
120 | } | |
3c065fe3 | 121 | const obj: { [ id: string ]: BuildFormValidator } = { |
7ed1edbb C |
122 | name: VIDEO_NAME_VALIDATOR, |
123 | privacy: VIDEO_PRIVACY_VALIDATOR, | |
124 | channelId: VIDEO_CHANNEL_VALIDATOR, | |
d18d6478 C |
125 | nsfw: null, |
126 | commentsEnabled: null, | |
7f2cfe3a | 127 | downloadEnabled: null, |
2186386c | 128 | waitTranscoding: null, |
7ed1edbb C |
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, | |
d18d6478 | 134 | previewfile: null, |
7ed1edbb C |
135 | support: VIDEO_SUPPORT_VALIDATOR, |
136 | schedulePublicationAt: VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR, | |
c6c0fa6c | 137 | originallyPublishedAt: VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR, |
b5b68755 | 138 | liveStreamKey: null, |
bb4ba6d9 | 139 | permanentLive: null, |
b5b68755 | 140 | saveReplay: null |
d18d6478 | 141 | } |
ff249f49 | 142 | |
3c065fe3 | 143 | this.formValidatorService.updateFormGroup( |
d18d6478 C |
144 | this.form, |
145 | this.formErrors, | |
146 | this.validationMessages, | |
147 | obj, | |
148 | defaultValues | |
149 | ) | |
74af5145 | 150 | |
40e87e9e C |
151 | this.form.addControl('captions', new FormArray([ |
152 | new FormGroup({ | |
153 | language: new FormControl(), | |
154 | captionfile: new FormControl() | |
155 | }) | |
156 | ])) | |
157 | ||
bbe0f064 C |
158 | this.trackChannelChange() |
159 | this.trackPrivacyChange() | |
bb4ba6d9 | 160 | this.trackLivePermanentFieldChange() |
0ba9696c C |
161 | |
162 | this.formBuilt.emit() | |
bbe0f064 C |
163 | } |
164 | ||
165 | ngOnInit () { | |
3cf68b86 C |
166 | this.serverConfig = this.serverService.getHTMLConfig() |
167 | ||
bbe0f064 C |
168 | this.updateForm() |
169 | ||
7294aab0 C |
170 | this.pluginService.ensurePluginsAreLoaded('video-edit') |
171 | .then(() => this.updatePluginFields()) | |
172 | ||
ba430d75 C |
173 | this.serverService.getVideoCategories() |
174 | .subscribe(res => this.videoCategories = res) | |
7294aab0 | 175 | |
ba430d75 C |
176 | this.serverService.getVideoLicences() |
177 | .subscribe(res => this.videoLicences = res) | |
7294aab0 | 178 | |
02c01341 RK |
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 | |
7294aab0 | 185 | .map(l => { |
81547acb FC |
186 | if (l.id === 'zxx') return { ...l, group: $localize`Other`, groupOrder: 1 } |
187 | ||
7294aab0 C |
188 | return res.about.instance.languages.includes(l.id) |
189 | ? { ...l, group: $localize`Instance languages`, groupOrder: 0 } | |
81547acb | 190 | : { ...l, group: $localize`All languages`, groupOrder: 2 } |
7294aab0 | 191 | }) |
02c01341 RK |
192 | .sort((a, b) => a.groupOrder - b.groupOrder) |
193 | }) | |
ba430d75 C |
194 | |
195 | this.serverService.getVideoPrivacies() | |
02c01341 | 196 | .subscribe(privacies => { |
29510651 | 197 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies).videoPrivacies |
a3f45a2a | 198 | |
02c01341 RK |
199 | if (this.schedulePublicationPossible) { |
200 | this.videoPrivacies.push({ | |
201 | id: this.SPECIAL_SCHEDULED_PRIVACY, | |
66357162 C |
202 | label: $localize`Scheduled`, |
203 | description: $localize`Hide the video until a specific date` | |
02c01341 RK |
204 | }) |
205 | } | |
206 | }) | |
bbe0f064 | 207 | |
772d5642 | 208 | this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id) |
84c7cde6 C |
209 | |
210 | this.ngZone.runOutsideAngular(() => { | |
211 | this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute | |
212 | }) | |
7294aab0 C |
213 | |
214 | this.hooks.runAction('action:video-edit.init', 'video-edit', { type: this.type }) | |
40e87e9e C |
215 | } |
216 | ||
217 | ngOnDestroy () { | |
218 | if (this.schedulerInterval) clearInterval(this.schedulerInterval) | |
219 | } | |
220 | ||
ddb62a85 C |
221 | getExistingCaptions () { |
222 | return this.videoCaptions | |
223 | .filter(c => c.action !== 'REMOVE') | |
224 | .map(c => c.language.id) | |
225 | } | |
226 | ||
40e87e9e | 227 | onCaptionAdded (caption: VideoCaptionEdit) { |
f4001cf4 C |
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' }) | |
ad774752 C |
233 | } else { |
234 | this.videoCaptions.push( | |
235 | Object.assign(caption, { action: 'CREATE' as 'CREATE' }) | |
236 | ) | |
f4001cf4 C |
237 | } |
238 | ||
ad774752 | 239 | this.sortVideoCaptions() |
40e87e9e C |
240 | } |
241 | ||
98ab5dc8 | 242 | deleteCaption (caption: VideoCaptionEdit) { |
772d5642 | 243 | // Caption recovers his former state |
9df52d66 | 244 | if (caption.action && this.initialVideoCaptions.includes(caption.language.id)) { |
772d5642 C |
245 | caption.action = undefined |
246 | return | |
247 | } | |
248 | ||
40e87e9e C |
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() | |
bbe0f064 C |
260 | } |
261 | ||
b5b68755 C |
262 | isSaveReplayEnabled () { |
263 | return this.serverConfig.live.allowReplay | |
264 | } | |
265 | ||
bb4ba6d9 C |
266 | isPermanentLiveEnabled () { |
267 | return this.form.value['permanentLive'] === true | |
268 | } | |
269 | ||
0f319334 C |
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 | ||
3c065fe3 C |
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 | ||
ad774752 C |
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 | ||
7294aab0 C |
297 | private updatePluginFields () { |
298 | this.pluginFields = this.pluginService.getRegisteredVideoFormFields(this.type) | |
299 | ||
300 | if (this.pluginFields.length === 0) return | |
301 | ||
3c065fe3 C |
302 | const pluginObj: { [ id: string ]: BuildFormValidator } = {} |
303 | const pluginValidationMessages: FormReactiveValidationMessages = {} | |
304 | const pluginFormErrors: any = {} | |
305 | const pluginDefaults: any = {} | |
7294aab0 C |
306 | |
307 | for (const setting of this.pluginFields) { | |
3c065fe3 C |
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 | |
7294aab0 C |
324 | } |
325 | ||
3c065fe3 C |
326 | this.pluginDataFormGroup = new FormGroup({}) |
327 | this.formValidatorService.updateFormGroup( | |
328 | this.pluginDataFormGroup, | |
329 | pluginFormErrors, | |
330 | pluginValidationMessages, | |
331 | pluginObj, | |
332 | pluginDefaults | |
333 | ) | |
334 | ||
7294aab0 | 335 | this.form.addControl('pluginData', this.pluginDataFormGroup) |
3c065fe3 C |
336 | this.formErrors['pluginData'] = pluginFormErrors |
337 | this.validationMessages['pluginData'] = pluginValidationMessages | |
7294aab0 | 338 | |
3c065fe3 | 339 | this.cd.detectChanges() |
7294aab0 C |
340 | this.pluginFieldsAdded.emit() |
341 | } | |
342 | ||
bbe0f064 | 343 | private trackPrivacyChange () { |
4adf2673 | 344 | // We will update the schedule input and the wait transcoding checkbox validators |
9df52d66 | 345 | this.form.controls['privacy'] |
bbe0f064 C |
346 | .valueChanges |
347 | .pipe(map(res => parseInt(res.toString(), 10))) | |
348 | .subscribe( | |
349 | newPrivacyId => { | |
0f7fedc3 | 350 | |
bbe0f064 C |
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() | |
0f7fedc3 C |
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 | } | |
bbe0f064 C |
371 | } |
372 | ||
373 | scheduleControl.updateValueAndValidity() | |
374 | waitTranscodingControl.updateValueAndValidity() | |
0f7fedc3 C |
375 | |
376 | this.firstPatchDone = true | |
377 | ||
bbe0f064 C |
378 | } |
379 | ) | |
380 | } | |
381 | ||
382 | private trackChannelChange () { | |
74af5145 | 383 | // We will update the "support" field depending on the channel |
9df52d66 | 384 | this.form.controls['channelId'] |
74af5145 C |
385 | .valueChanges |
386 | .pipe(map(res => parseInt(res.toString(), 10))) | |
387 | .subscribe( | |
388 | newChannelId => { | |
9df52d66 | 389 | const oldChannelId = parseInt(this.form.value['channelId'], 10) |
74af5145 C |
390 | |
391 | // Not initialized yet | |
392 | if (isNaN(newChannelId)) return | |
393 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
d18d6478 | 394 | if (!newChannel) return |
74af5145 | 395 | |
6f79be11 C |
396 | // Wait support field update |
397 | setTimeout(() => { | |
9df52d66 | 398 | const currentSupport = this.form.value['support'] |
74af5145 | 399 | |
6f79be11 | 400 | // First time we set the channel? |
c6c0fa6c C |
401 | if (isNaN(oldChannelId)) { |
402 | // Fill support if it's empty | |
403 | if (!currentSupport) this.updateSupportField(newChannel.support) | |
404 | ||
405 | return | |
406 | } | |
6f79be11 C |
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 | } | |
74af5145 | 413 | |
6f79be11 C |
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 | |
74af5145 | 417 | |
6f79be11 C |
418 | // Update the support text with our new channel |
419 | this.updateSupportField(newChannel.support) | |
420 | }) | |
74af5145 C |
421 | } |
422 | ) | |
ff249f49 C |
423 | } |
424 | ||
bb4ba6d9 C |
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 | ||
74af5145 C |
443 | private updateSupportField (support: string) { |
444 | return this.form.patchValue({ support: support || '' }) | |
445 | } | |
ff249f49 | 446 | } |