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