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