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