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