]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/shared/video-edit.component.ts
Give moderators access to edit channels (#4608)
[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'
21e493d4 3import { SelectChannelItem } from 'src/types/select-options-item.model'
7294aab0 4import { Component, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output, ViewChild } from '@angular/core'
0f319334 5import { FormArray, FormControl, FormGroup, Validators } from '@angular/forms'
7294aab0 6import { HooksService, PluginService, ServerService } from '@app/core'
67ed6552 7import { removeElementFromArray } from '@app/helpers'
7ed1edbb
C
8import {
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 21import { FormReactiveValidationMessages, FormValidatorService } from '@app/shared/shared-forms'
9abd170d 22import { InstanceService } from '@app/shared/shared-instance'
67ed6552 23import { VideoCaptionEdit, VideoEdit, VideoService } from '@app/shared/shared-main'
428ccb8b 24import {
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
33import { I18nPrimengCalendarService } from './i18n-primeng-calendar.service'
34import { VideoCaptionAddModalComponent } from './video-caption-add-modal.component'
c6c0fa6c 35import { VideoEditType } from './video-edit.type'
02c01341
RK
36
37type VideoLanguages = VideoConstant<string> & { group?: string }
0f319334
C
38type 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 48export 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
C
180 .map(l => {
181 return res.about.instance.languages.includes(l.id)
182 ? { ...l, group: $localize`Instance languages`, groupOrder: 0 }
183 : { ...l, group: $localize`All languages`, groupOrder: 1 }
184 })
02c01341
RK
185 .sort((a, b) => a.groupOrder - b.groupOrder)
186 })
ba430d75
C
187
188 this.serverService.getVideoPrivacies()
02c01341 189 .subscribe(privacies => {
29510651 190 this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies).videoPrivacies
a3f45a2a 191
02c01341
RK
192 if (this.schedulePublicationPossible) {
193 this.videoPrivacies.push({
194 id: this.SPECIAL_SCHEDULED_PRIVACY,
66357162
C
195 label: $localize`Scheduled`,
196 description: $localize`Hide the video until a specific date`
02c01341
RK
197 })
198 }
199 })
bbe0f064 200
2989628b 201 this.serverConfig = this.serverService.getHTMLConfig()
851f5daa 202
772d5642 203 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
84c7cde6
C
204
205 this.ngZone.runOutsideAngular(() => {
206 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
207 })
7294aab0
C
208
209 this.hooks.runAction('action:video-edit.init', 'video-edit', { type: this.type })
40e87e9e
C
210 }
211
212 ngOnDestroy () {
213 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
214 }
215
ddb62a85
C
216 getExistingCaptions () {
217 return this.videoCaptions
218 .filter(c => c.action !== 'REMOVE')
219 .map(c => c.language.id)
220 }
221
40e87e9e 222 onCaptionAdded (caption: VideoCaptionEdit) {
f4001cf4
C
223 const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id)
224
225 // Replace existing caption?
226 if (existingCaption) {
227 Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' })
ad774752
C
228 } else {
229 this.videoCaptions.push(
230 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
231 )
f4001cf4
C
232 }
233
ad774752 234 this.sortVideoCaptions()
40e87e9e
C
235 }
236
98ab5dc8 237 deleteCaption (caption: VideoCaptionEdit) {
772d5642 238 // Caption recovers his former state
9df52d66 239 if (caption.action && this.initialVideoCaptions.includes(caption.language.id)) {
772d5642
C
240 caption.action = undefined
241 return
242 }
243
40e87e9e
C
244 // This caption is not on the server, just remove it from our array
245 if (caption.action === 'CREATE') {
246 removeElementFromArray(this.videoCaptions, caption)
247 return
248 }
249
250 caption.action = 'REMOVE' as 'REMOVE'
251 }
252
253 openAddCaptionModal () {
254 this.videoCaptionAddModal.show()
bbe0f064
C
255 }
256
b5b68755
C
257 isSaveReplayEnabled () {
258 return this.serverConfig.live.allowReplay
259 }
260
bb4ba6d9
C
261 isPermanentLiveEnabled () {
262 return this.form.value['permanentLive'] === true
263 }
264
0f319334
C
265 isPluginFieldHidden (pluginField: PluginField) {
266 if (typeof pluginField.commonOptions.hidden !== 'function') return false
267
268 return pluginField.commonOptions.hidden({
269 formValues: this.form.value,
270 videoToUpdate: this.videoToUpdate,
271 liveVideo: this.liveVideo
272 })
273 }
274
ad774752
C
275 private sortVideoCaptions () {
276 this.videoCaptions.sort((v1, v2) => {
277 if (v1.language.label < v2.language.label) return -1
278 if (v1.language.label === v2.language.label) return 0
279
280 return 1
281 })
282 }
283
7294aab0
C
284 private updatePluginFields () {
285 this.pluginFields = this.pluginService.getRegisteredVideoFormFields(this.type)
286
287 if (this.pluginFields.length === 0) return
288
289 const obj: any = {}
290
291 for (const setting of this.pluginFields) {
292 obj[setting.commonOptions.name] = new FormControl(setting.commonOptions.default)
293 }
294
295 this.pluginDataFormGroup = new FormGroup(obj)
296 this.form.addControl('pluginData', this.pluginDataFormGroup)
297
298 this.pluginFieldsAdded.emit()
299 }
300
bbe0f064 301 private trackPrivacyChange () {
4adf2673 302 // We will update the schedule input and the wait transcoding checkbox validators
9df52d66 303 this.form.controls['privacy']
bbe0f064
C
304 .valueChanges
305 .pipe(map(res => parseInt(res.toString(), 10)))
306 .subscribe(
307 newPrivacyId => {
0f7fedc3 308
bbe0f064
C
309 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
310
311 // Value changed
312 const scheduleControl = this.form.get('schedulePublicationAt')
313 const waitTranscodingControl = this.form.get('waitTranscoding')
314
315 if (this.schedulePublicationEnabled) {
316 scheduleControl.setValidators([ Validators.required ])
317
318 waitTranscodingControl.disable()
319 waitTranscodingControl.setValue(false)
320 } else {
321 scheduleControl.clearValidators()
322
323 waitTranscodingControl.enable()
0f7fedc3
C
324
325 // Do not update the control value on first patch (values come from the server)
326 if (this.firstPatchDone === true) {
327 waitTranscodingControl.setValue(true)
328 }
bbe0f064
C
329 }
330
331 scheduleControl.updateValueAndValidity()
332 waitTranscodingControl.updateValueAndValidity()
0f7fedc3
C
333
334 this.firstPatchDone = true
335
bbe0f064
C
336 }
337 )
338 }
339
340 private trackChannelChange () {
74af5145 341 // We will update the "support" field depending on the channel
9df52d66 342 this.form.controls['channelId']
74af5145
C
343 .valueChanges
344 .pipe(map(res => parseInt(res.toString(), 10)))
345 .subscribe(
346 newChannelId => {
9df52d66 347 const oldChannelId = parseInt(this.form.value['channelId'], 10)
74af5145
C
348
349 // Not initialized yet
350 if (isNaN(newChannelId)) return
351 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
d18d6478 352 if (!newChannel) return
74af5145 353
6f79be11
C
354 // Wait support field update
355 setTimeout(() => {
9df52d66 356 const currentSupport = this.form.value['support']
74af5145 357
6f79be11 358 // First time we set the channel?
c6c0fa6c
C
359 if (isNaN(oldChannelId)) {
360 // Fill support if it's empty
361 if (!currentSupport) this.updateSupportField(newChannel.support)
362
363 return
364 }
6f79be11
C
365
366 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
367 if (!newChannel || !oldChannel) {
368 console.error('Cannot find new or old channel.')
369 return
370 }
74af5145 371
6f79be11
C
372 // If the current support text is not the same than the old channel, the user updated it.
373 // We don't want the user to lose his text, so stop here
374 if (currentSupport && currentSupport !== oldChannel.support) return
74af5145 375
6f79be11
C
376 // Update the support text with our new channel
377 this.updateSupportField(newChannel.support)
378 })
74af5145
C
379 }
380 )
ff249f49
C
381 }
382
bb4ba6d9
C
383 private trackLivePermanentFieldChange () {
384 // We will update the "support" field depending on the channel
385 this.form.controls['permanentLive']
386 .valueChanges
387 .subscribe(
388 permanentLive => {
389 const saveReplayControl = this.form.controls['saveReplay']
390
391 if (permanentLive === true) {
392 saveReplayControl.setValue(false)
393 saveReplayControl.disable()
394 } else {
395 saveReplayControl.enable()
396 }
397 }
398 )
399 }
400
74af5145
C
401 private updateSupportField (support: string) {
402 return this.form.patchValue({ support: support || '' })
403 }
ff249f49 404}