]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-update.component.ts
Update translations
[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'
5c5bcea2 7import { FormReactive, FormReactiveService } 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
dc8bc31b 31
772d5642
C
32 private updateDone = false
33
df98563e 34 constructor (
5c5bcea2 35 protected formReactiveService: FormReactiveService,
d8e689b8 36 private route: ActivatedRoute,
7ddd02c9 37 private router: Router,
f8b2c1b4 38 private notifier: Notifier,
15a7387d 39 private videoService: VideoService,
6200d8d9 40 private loadingBar: LoadingBarService,
b5b68755
C
41 private videoCaptionService: VideoCaptionService,
42 private liveVideoService: LiveVideoService
9df52d66 43 ) {
df98563e 44 super()
4b2f33f3 45 }
dc8bc31b 46
df98563e 47 ngOnInit () {
d18d6478 48 this.buildForm({})
d8e689b8 49
0ba9696c 50 const { videoData } = this.route.snapshot.data
2e401e85 51 const { video, videoChannels, videoCaptions, videoSource, liveVideo } = videoData
0ba9696c 52
0ba9696c 53 this.videoDetails = video
0146f351 54 this.videoEdit = new VideoEdit(this.videoDetails)
0ba9696c
C
55
56 this.userVideoChannels = videoChannels
57 this.videoCaptions = videoCaptions
2e401e85 58 this.videoSource = videoSource
0ba9696c
C
59 this.liveVideo = liveVideo
60
0146f351 61 this.forbidScheduledPublication = this.videoEdit.privacy !== VideoPrivacy.PRIVATE
0ba9696c
C
62 }
63
64 onFormBuilt () {
0146f351 65 hydrateFormFromVideo(this.form, this.videoEdit, true)
0ba9696c
C
66
67 if (this.liveVideo) {
68 this.form.patchValue({
69 saveReplay: this.liveVideo.saveReplay,
f443a746 70 latencyMode: this.liveVideo.latencyMode,
0ba9696c
C
71 permanentLive: this.liveVideo.permanentLive
72 })
73 }
e822fdae
C
74 }
75
674a66bb
C
76 @HostListener('window:beforeunload', [ '$event' ])
77 onUnload (event: any) {
78 const { text, canDeactivate } = this.canDeactivate()
79
80 if (canDeactivate) return
81
82 event.returnValue = text
83 return text
84 }
85
86 canDeactivate (): { canDeactivate: boolean, text?: string } {
772d5642
C
87 if (this.updateDone === true) return { canDeactivate: true }
88
66357162 89 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
674a66bb 90
772d5642 91 for (const caption of this.videoCaptions) {
674a66bb 92 if (caption.action) return { canDeactivate: false, text }
772d5642
C
93 }
94
674a66bb 95 return { canDeactivate: this.formChanged === false, text }
772d5642
C
96 }
97
080f1402 98 isWaitTranscodingHidden () {
ddb62a85 99 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
080f1402 100 return true
ddb62a85
C
101 }
102
080f1402 103 return false
ddb62a85
C
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
0146f351 114 this.videoEdit.patch(this.form.value)
d8e689b8 115
a02b93ce 116 this.loadingBar.useRef().start()
68e24d72 117 this.isUpdatingVideo = true
40e87e9e
C
118
119 // Update the video
0146f351 120 this.videoService.updateVideo(this.videoEdit)
40e87e9e
C
121 .pipe(
122 // Then update captions
0146f351 123 switchMap(() => this.videoCaptionService.updateCaptions(this.videoEdit.id, this.videoCaptions)),
b5b68755
C
124
125 switchMap(() => {
126 if (!this.liveVideo) return of(undefined)
127
fd0fdc46 128 const liveVideoUpdate: LiveVideoUpdate = {
5d84d717 129 saveReplay: !!this.form.value.saveReplay,
f443a746
C
130 permanentLive: !!this.form.value.permanentLive,
131 latencyMode: this.form.value.latencyMode
fd0fdc46
C
132 }
133
fd0fdc46
C
134 // Don't update live attributes if they did not change
135 const liveChanged = Object.keys(liveVideoUpdate)
136 .some(key => this.liveVideo[key] !== liveVideoUpdate[key])
137 if (!liveChanged) return of(undefined)
138
0146f351 139 return this.liveVideoService.updateLive(this.videoEdit.id, liveVideoUpdate)
b5b68755 140 })
40e87e9e 141 )
1378c0d3
C
142 .subscribe({
143 next: () => {
772d5642 144 this.updateDone = true
40e87e9e 145 this.isUpdatingVideo = false
a02b93ce 146 this.loadingBar.useRef().complete()
66357162 147 this.notifier.success($localize`Video updated.`)
0146f351 148 this.router.navigateByUrl(Video.buildWatchUrl(this.videoEdit))
40e87e9e
C
149 },
150
1378c0d3 151 error: err => {
a02b93ce 152 this.loadingBar.useRef().complete()
40e87e9e 153 this.isUpdatingVideo = false
f8b2c1b4 154 this.notifier.error(err.message)
42b40636 155 logger.error(err)
40e87e9e 156 }
1378c0d3 157 })
dc8bc31b 158 }
e54163c2 159
7294aab0 160 hydratePluginFieldsFromVideo () {
0146f351 161 if (!this.videoEdit.pluginData) return
7294aab0
C
162
163 this.form.patchValue({
0146f351 164 pluginData: this.videoEdit.pluginData
7294aab0
C
165 })
166 }
d4a8e7a6
C
167
168 getVideoUrl () {
169 return Video.buildWatchUrl(this.videoDetails)
170 }
dc8bc31b 171}