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