]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Move to peertube feed fork
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-update.component.ts
CommitLineData
f8c00564 1import { of } from 'rxjs'
0ba9696c 2import { switchMap } from 'rxjs/operators'
21e493d4 3import { SelectChannelItem } from 'src/types/select-options-item.model'
674a66bb 4import { Component, HostListener, OnInit } from '@angular/core'
df98563e 5import { ActivatedRoute, Router } from '@angular/router'
f8b2c1b4 6import { Notifier } from '@app/core'
21e493d4 7import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
d4a8e7a6 8import { Video, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
f8c00564 9import { LiveVideoService } from '@app/shared/shared-video-live'
67ed6552 10import { LoadingBarService } from '@ngx-loading-bar/core'
b5b68755 11import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
c6c0fa6c 12import { hydrateFormFromVideo } from './shared/video-edit-utils'
1553e15d 13
dc8bc31b 14@Component({
d8e689b8 15 selector: 'my-videos-update',
80958c78 16 styleUrls: [ './shared/video-edit.component.scss' ],
d8e689b8 17 templateUrl: './video-update.component.html'
dc8bc31b 18})
d8e689b8 19export class VideoUpdateComponent extends FormReactive implements OnInit {
404b54e1 20 video: VideoEdit
ddb62a85 21 videoDetails: VideoDetails
c6c0fa6c
C
22 userVideoChannels: SelectChannelItem[] = []
23 videoCaptions: VideoCaptionEdit[] = []
a5cf76af 24 liveVideo: LiveVideo
4b2f33f3 25
68e24d72 26 isUpdatingVideo = false
bbe0f064 27 schedulePublicationPossible = false
14e2014a 28 waitTranscodingEnabled = true
dc8bc31b 29
772d5642
C
30 private updateDone = false
31
df98563e 32 constructor (
d18d6478 33 protected formValidatorService: FormValidatorService,
d8e689b8 34 private route: ActivatedRoute,
7ddd02c9 35 private router: Router,
f8b2c1b4 36 private notifier: Notifier,
15a7387d 37 private videoService: VideoService,
6200d8d9 38 private loadingBar: LoadingBarService,
b5b68755
C
39 private videoCaptionService: VideoCaptionService,
40 private liveVideoService: LiveVideoService
9df52d66 41 ) {
df98563e 42 super()
4b2f33f3 43 }
dc8bc31b 44
df98563e 45 ngOnInit () {
d18d6478 46 this.buildForm({})
d8e689b8 47
0ba9696c
C
48 const { videoData } = this.route.snapshot.data
49 const { video, videoChannels, videoCaptions, liveVideo } = videoData
50
51 this.video = new VideoEdit(video)
52 this.videoDetails = video
53
54 this.userVideoChannels = videoChannels
55 this.videoCaptions = videoCaptions
56 this.liveVideo = liveVideo
57
58 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
59 }
60
61 onFormBuilt () {
62 hydrateFormFromVideo(this.form, this.video, true)
63
64 if (this.liveVideo) {
65 this.form.patchValue({
66 saveReplay: this.liveVideo.saveReplay,
67 permanentLive: this.liveVideo.permanentLive
68 })
69 }
e822fdae
C
70 }
71
674a66bb
C
72 @HostListener('window:beforeunload', [ '$event' ])
73 onUnload (event: any) {
74 const { text, canDeactivate } = this.canDeactivate()
75
76 if (canDeactivate) return
77
78 event.returnValue = text
79 return text
80 }
81
82 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
83 if (this.updateDone === true) return { canDeactivate: true }
84
66357162 85 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 86
772d5642 87 for (const caption of this.videoCaptions) {
674a66bb 88 if (caption.action) return { canDeactivate: false, text }
772d5642
C
89 }
90
674a66bb 91 return { canDeactivate: this.formChanged === false, text }
772d5642
C
92 }
93
ddb62a85
C
94 isWaitTranscodingEnabled () {
95 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
96 return false
97 }
98
99 if (this.liveVideo && this.form.value['saveReplay'] !== true) {
100 return false
101 }
102
103 return true
104 }
105
cc4bf76c
C
106 async update () {
107 await this.waitPendingCheck()
108 this.forceCheck()
109
110 if (!this.form.valid || this.isUpdatingVideo === true) {
df98563e 111 return
c24ac1c1
C
112 }
113
df98563e 114 this.video.patch(this.form.value)
d8e689b8 115
a02b93ce 116 this.loadingBar.useRef().start()
68e24d72 117 this.isUpdatingVideo = true
40e87e9e
C
118
119 // Update the video
d8e689b8 120 this.videoService.updateVideo(this.video)
40e87e9e
C
121 .pipe(
122 // Then update captions
b5b68755
C
123 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
124
125 switchMap(() => {
126 if (!this.liveVideo) return of(undefined)
127
fd0fdc46 128 const liveVideoUpdate: LiveVideoUpdate = {
5d84d717
C
129 saveReplay: !!this.form.value.saveReplay,
130 permanentLive: !!this.form.value.permanentLive
fd0fdc46
C
131 }
132
fd0fdc46
C
133 // Don't update live attributes if they did not change
134 const liveChanged = Object.keys(liveVideoUpdate)
135 .some(key => this.liveVideo[key] !== liveVideoUpdate[key])
136 if (!liveChanged) return of(undefined)
137
b5b68755
C
138 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
139 })
40e87e9e 140 )
1378c0d3
C
141 .subscribe({
142 next: () => {
772d5642 143 this.updateDone = true
40e87e9e 144 this.isUpdatingVideo = false
a02b93ce 145 this.loadingBar.useRef().complete()
66357162 146 this.notifier.success($localize`Video updated.`)
d4a8e7a6 147 this.router.navigateByUrl(Video.buildWatchUrl(this.video))
40e87e9e
C
148 },
149
1378c0d3 150 error: err => {
a02b93ce 151 this.loadingBar.useRef().complete()
40e87e9e 152 this.isUpdatingVideo = false
f8b2c1b4 153 this.notifier.error(err.message)
40e87e9e
C
154 console.error(err)
155 }
1378c0d3 156 })
dc8bc31b 157 }
e54163c2 158
7294aab0
C
159 hydratePluginFieldsFromVideo () {
160 if (!this.video.pluginData) return
161
162 this.form.patchValue({
163 pluginData: this.video.pluginData
164 })
165 }
d4a8e7a6
C
166
167 getVideoUrl () {
168 return Video.buildWatchUrl(this.videoDetails)
169 }
dc8bc31b 170}