]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Merge remote-tracking branch 'weblate/develop' into develop
[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
4afec735 27 forbidScheduledPublication = 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
4afec735 58 this.forbidScheduledPublication = this.video.privacy !== VideoPrivacy.PRIVATE
0ba9696c
C
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,
f443a746 67 latencyMode: this.liveVideo.latencyMode,
0ba9696c
C
68 permanentLive: this.liveVideo.permanentLive
69 })
70 }
e822fdae
C
71 }
72
674a66bb
C
73 @HostListener('window:beforeunload', [ '$event' ])
74 onUnload (event: any) {
75 const { text, canDeactivate } = this.canDeactivate()
76
77 if (canDeactivate) return
78
79 event.returnValue = text
80 return text
81 }
82
83 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
84 if (this.updateDone === true) return { canDeactivate: true }
85
66357162 86 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 87
772d5642 88 for (const caption of this.videoCaptions) {
674a66bb 89 if (caption.action) return { canDeactivate: false, text }
772d5642
C
90 }
91
674a66bb 92 return { canDeactivate: this.formChanged === false, text }
772d5642
C
93 }
94
ddb62a85
C
95 isWaitTranscodingEnabled () {
96 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
97 return false
98 }
99
100 if (this.liveVideo && this.form.value['saveReplay'] !== true) {
101 return false
102 }
103
104 return true
105 }
106
cc4bf76c
C
107 async update () {
108 await this.waitPendingCheck()
109 this.forceCheck()
110
111 if (!this.form.valid || this.isUpdatingVideo === true) {
df98563e 112 return
c24ac1c1
C
113 }
114
df98563e 115 this.video.patch(this.form.value)
d8e689b8 116
a02b93ce 117 this.loadingBar.useRef().start()
68e24d72 118 this.isUpdatingVideo = true
40e87e9e
C
119
120 // Update the video
d8e689b8 121 this.videoService.updateVideo(this.video)
40e87e9e
C
122 .pipe(
123 // Then update captions
b5b68755
C
124 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)),
125
126 switchMap(() => {
127 if (!this.liveVideo) return of(undefined)
128
fd0fdc46 129 const liveVideoUpdate: LiveVideoUpdate = {
5d84d717 130 saveReplay: !!this.form.value.saveReplay,
f443a746
C
131 permanentLive: !!this.form.value.permanentLive,
132 latencyMode: this.form.value.latencyMode
fd0fdc46
C
133 }
134
fd0fdc46
C
135 // Don't update live attributes if they did not change
136 const liveChanged = Object.keys(liveVideoUpdate)
137 .some(key => this.liveVideo[key] !== liveVideoUpdate[key])
138 if (!liveChanged) return of(undefined)
139
b5b68755
C
140 return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate)
141 })
40e87e9e 142 )
1378c0d3
C
143 .subscribe({
144 next: () => {
772d5642 145 this.updateDone = true
40e87e9e 146 this.isUpdatingVideo = false
a02b93ce 147 this.loadingBar.useRef().complete()
66357162 148 this.notifier.success($localize`Video updated.`)
d4a8e7a6 149 this.router.navigateByUrl(Video.buildWatchUrl(this.video))
40e87e9e
C
150 },
151
1378c0d3 152 error: err => {
a02b93ce 153 this.loadingBar.useRef().complete()
40e87e9e 154 this.isUpdatingVideo = false
f8b2c1b4 155 this.notifier.error(err.message)
40e87e9e
C
156 console.error(err)
157 }
1378c0d3 158 })
dc8bc31b 159 }
e54163c2 160
7294aab0
C
161 hydratePluginFieldsFromVideo () {
162 if (!this.video.pluginData) return
163
164 this.form.patchValue({
165 pluginData: this.video.pluginData
166 })
167 }
d4a8e7a6
C
168
169 getVideoUrl () {
170 return Video.buildWatchUrl(this.videoDetails)
171 }
dc8bc31b 172}