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