1
0
Fork 0
peertube/client/src/app/+videos/+video-edit/video-update.component.ts

174 lines
5.0 KiB
TypeScript
Raw Normal View History

2020-11-05 09:56:23 +00:00
import { of } from 'rxjs'
2021-10-22 13:51:02 +00:00
import { switchMap } from 'rxjs/operators'
2021-02-10 08:05:29 +00:00
import { SelectChannelItem } from 'src/types/select-options-item.model'
import { Component, HostListener, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { Notifier } from '@app/core'
2021-02-10 08:05:29 +00:00
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
import { Video, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
2020-11-05 09:56:23 +00:00
import { LiveVideoService } from '@app/shared/shared-video-live'
2020-06-23 12:10:17 +00:00
import { LoadingBarService } from '@ngx-loading-bar/core'
2020-10-26 15:44:23 +00:00
import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
import { hydrateFormFromVideo } from './shared/video-edit-utils'
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-videos-update',
styleUrls: [ './shared/video-edit.component.scss' ],
templateUrl: './video-update.component.html'
2016-03-14 12:50:19 +00:00
})
export class VideoUpdateComponent extends FormReactive implements OnInit {
2017-10-25 14:43:19 +00:00
video: VideoEdit
2021-01-26 09:56:55 +00:00
videoDetails: VideoDetails
userVideoChannels: SelectChannelItem[] = []
videoCaptions: VideoCaptionEdit[] = []
liveVideo: LiveVideo
2016-09-09 20:16:51 +00:00
2018-02-16 16:24:47 +00:00
isUpdatingVideo = false
schedulePublicationPossible = false
2018-12-11 13:52:50 +00:00
waitTranscodingEnabled = true
2016-03-14 12:50:19 +00:00
private updateDone = false
constructor (
2018-06-05 08:58:45 +00:00
protected formValidatorService: FormValidatorService,
private route: ActivatedRoute,
private router: Router,
private notifier: Notifier,
2017-12-20 13:29:55 +00:00
private videoService: VideoService,
private loadingBar: LoadingBarService,
2020-10-26 15:44:23 +00:00
private videoCaptionService: VideoCaptionService,
private liveVideoService: LiveVideoService
2021-08-17 12:42:53 +00:00
) {
super()
2016-09-09 20:16:51 +00:00
}
2016-03-14 12:50:19 +00:00
ngOnInit () {
2018-06-05 08:58:45 +00:00
this.buildForm({})
2021-10-22 13:51:02 +00:00
const { videoData } = this.route.snapshot.data
const { video, videoChannels, videoCaptions, liveVideo } = videoData
this.video = new VideoEdit(video)
this.videoDetails = video
this.userVideoChannels = videoChannels
this.videoCaptions = videoCaptions
this.liveVideo = liveVideo
this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
}
onFormBuilt () {
hydrateFormFromVideo(this.form, this.video, true)
if (this.liveVideo) {
this.form.patchValue({
saveReplay: this.liveVideo.saveReplay,
permanentLive: this.liveVideo.permanentLive
})
}
}
@HostListener('window:beforeunload', [ '$event' ])
onUnload (event: any) {
const { text, canDeactivate } = this.canDeactivate()
if (canDeactivate) return
event.returnValue = text
return text
}
canDeactivate (): { canDeactivate: boolean, text?: string } {
if (this.updateDone === true) return { canDeactivate: true }
const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
for (const caption of this.videoCaptions) {
if (caption.action) return { canDeactivate: false, text }
}
return { canDeactivate: this.formChanged === false, text }
}
checkForm () {
this.forceCheck()
return this.form.valid
}
2021-01-26 09:56:55 +00:00
isWaitTranscodingEnabled () {
if (this.videoDetails.getFiles().length > 1) { // Already transcoded
return false
}
if (this.liveVideo && this.form.value['saveReplay'] !== true) {
return false
}
return true
}
update () {
2021-08-17 12:42:53 +00:00
if (this.checkForm() === false || this.isUpdatingVideo === true) {
return
}
this.video.patch(this.form.value)
2020-08-06 14:14:58 +00:00
this.loadingBar.useRef().start()
2018-02-16 16:24:47 +00:00
this.isUpdatingVideo = true
2018-07-12 17:02:00 +00:00
// Update the video
this.videoService.updateVideo(this.video)
2018-07-12 17:02:00 +00:00
.pipe(
// Then update captions
2020-10-26 15:44:23 +00:00
switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
switchMap(() => {
if (!this.liveVideo) return of(undefined)
const liveVideoUpdate: LiveVideoUpdate = {
2021-01-26 14:41:51 +00:00
saveReplay: !!this.form.value.saveReplay,
permanentLive: !!this.form.value.permanentLive
}
// Don't update live attributes if they did not change
const liveChanged = Object.keys(liveVideoUpdate)
.some(key => this.liveVideo[key] !== liveVideoUpdate[key])
if (!liveChanged) return of(undefined)
2020-10-26 15:44:23 +00:00
return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
})
2018-07-12 17:02:00 +00:00
)
2021-08-17 09:27:47 +00:00
.subscribe({
next: () => {
this.updateDone = true
2018-07-12 17:02:00 +00:00
this.isUpdatingVideo = false
2020-08-06 14:14:58 +00:00
this.loadingBar.useRef().complete()
this.notifier.success($localize`Video updated.`)
this.router.navigateByUrl(Video.buildWatchUrl(this.video))
2018-07-12 17:02:00 +00:00
},
2021-08-17 09:27:47 +00:00
error: err => {
2020-08-06 14:14:58 +00:00
this.loadingBar.useRef().complete()
2018-07-12 17:02:00 +00:00
this.isUpdatingVideo = false
this.notifier.error(err.message)
2018-07-12 17:02:00 +00:00
console.error(err)
}
2021-08-17 09:27:47 +00:00
})
2016-03-14 12:50:19 +00:00
}
hydratePluginFieldsFromVideo () {
if (!this.video.pluginData) return
this.form.patchValue({
pluginData: this.video.pluginData
})
}
getVideoUrl () {
return Video.buildWatchUrl(this.videoDetails)
}
2016-03-14 12:50:19 +00:00
}