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