]>
Commit | Line | Data |
---|---|---|
f8c00564 | 1 | import { of } from 'rxjs' |
0ba9696c | 2 | import { switchMap } from 'rxjs/operators' |
21e493d4 | 3 | import { SelectChannelItem } from 'src/types/select-options-item.model' |
674a66bb | 4 | import { Component, HostListener, OnInit } from '@angular/core' |
df98563e | 5 | import { ActivatedRoute, Router } from '@angular/router' |
f8b2c1b4 | 6 | import { Notifier } from '@app/core' |
21e493d4 | 7 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' |
d4a8e7a6 | 8 | import { Video, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main' |
f8c00564 | 9 | import { LiveVideoService } from '@app/shared/shared-video-live' |
67ed6552 | 10 | import { LoadingBarService } from '@ngx-loading-bar/core' |
b5b68755 | 11 | import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models' |
c6c0fa6c | 12 | import { hydrateFormFromVideo } from './shared/video-edit-utils' |
2e401e85 | 13 | import { VideoSource } from '@shared/models/videos/video-source' |
1553e15d | 14 | |
dc8bc31b | 15 | @Component({ |
d8e689b8 | 16 | selector: 'my-videos-update', |
80958c78 | 17 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 18 | templateUrl: './video-update.component.html' |
dc8bc31b | 19 | }) |
d8e689b8 | 20 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
0146f351 | 21 | videoEdit: VideoEdit |
ddb62a85 | 22 | videoDetails: VideoDetails |
2e401e85 | 23 | videoSource: VideoSource |
c6c0fa6c C |
24 | userVideoChannels: SelectChannelItem[] = [] |
25 | videoCaptions: VideoCaptionEdit[] = [] | |
a5cf76af | 26 | liveVideo: LiveVideo |
4b2f33f3 | 27 | |
68e24d72 | 28 | isUpdatingVideo = false |
4afec735 | 29 | forbidScheduledPublication = false |
14e2014a | 30 | waitTranscodingEnabled = true |
dc8bc31b | 31 | |
772d5642 C |
32 | private updateDone = false |
33 | ||
df98563e | 34 | constructor ( |
d18d6478 | 35 | protected formValidatorService: FormValidatorService, |
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 | ||
ddb62a85 C |
98 | isWaitTranscodingEnabled () { |
99 | if (this.videoDetails.getFiles().length > 1) { // Already transcoded | |
100 | return false | |
101 | } | |
102 | ||
103 | if (this.liveVideo && this.form.value['saveReplay'] !== true) { | |
104 | return false | |
105 | } | |
106 | ||
107 | return true | |
108 | } | |
109 | ||
cc4bf76c C |
110 | async update () { |
111 | await this.waitPendingCheck() | |
112 | this.forceCheck() | |
113 | ||
114 | if (!this.form.valid || this.isUpdatingVideo === true) { | |
df98563e | 115 | return |
c24ac1c1 C |
116 | } |
117 | ||
0146f351 | 118 | this.videoEdit.patch(this.form.value) |
d8e689b8 | 119 | |
a02b93ce | 120 | this.loadingBar.useRef().start() |
68e24d72 | 121 | this.isUpdatingVideo = true |
40e87e9e C |
122 | |
123 | // Update the video | |
0146f351 | 124 | this.videoService.updateVideo(this.videoEdit) |
40e87e9e C |
125 | .pipe( |
126 | // Then update captions | |
0146f351 | 127 | switchMap(() => this.videoCaptionService.updateCaptions(this.videoEdit.id, this.videoCaptions)), |
b5b68755 C |
128 | |
129 | switchMap(() => { | |
130 | if (!this.liveVideo) return of(undefined) | |
131 | ||
fd0fdc46 | 132 | const liveVideoUpdate: LiveVideoUpdate = { |
5d84d717 | 133 | saveReplay: !!this.form.value.saveReplay, |
f443a746 C |
134 | permanentLive: !!this.form.value.permanentLive, |
135 | latencyMode: this.form.value.latencyMode | |
fd0fdc46 C |
136 | } |
137 | ||
fd0fdc46 C |
138 | // Don't update live attributes if they did not change |
139 | const liveChanged = Object.keys(liveVideoUpdate) | |
140 | .some(key => this.liveVideo[key] !== liveVideoUpdate[key]) | |
141 | if (!liveChanged) return of(undefined) | |
142 | ||
0146f351 | 143 | return this.liveVideoService.updateLive(this.videoEdit.id, liveVideoUpdate) |
b5b68755 | 144 | }) |
40e87e9e | 145 | ) |
1378c0d3 C |
146 | .subscribe({ |
147 | next: () => { | |
772d5642 | 148 | this.updateDone = true |
40e87e9e | 149 | this.isUpdatingVideo = false |
a02b93ce | 150 | this.loadingBar.useRef().complete() |
66357162 | 151 | this.notifier.success($localize`Video updated.`) |
0146f351 | 152 | this.router.navigateByUrl(Video.buildWatchUrl(this.videoEdit)) |
40e87e9e C |
153 | }, |
154 | ||
1378c0d3 | 155 | error: err => { |
a02b93ce | 156 | this.loadingBar.useRef().complete() |
40e87e9e | 157 | this.isUpdatingVideo = false |
f8b2c1b4 | 158 | this.notifier.error(err.message) |
40e87e9e C |
159 | console.error(err) |
160 | } | |
1378c0d3 | 161 | }) |
dc8bc31b | 162 | } |
e54163c2 | 163 | |
7294aab0 | 164 | hydratePluginFieldsFromVideo () { |
0146f351 | 165 | if (!this.videoEdit.pluginData) return |
7294aab0 C |
166 | |
167 | this.form.patchValue({ | |
0146f351 | 168 | pluginData: this.videoEdit.pluginData |
7294aab0 C |
169 | }) |
170 | } | |
d4a8e7a6 C |
171 | |
172 | getVideoUrl () { | |
173 | return Video.buildWatchUrl(this.videoDetails) | |
174 | } | |
dc8bc31b | 175 | } |