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