1 import { of } from 'rxjs'
2 import { switchMap } from 'rxjs/operators'
3 import { SelectChannelItem } from 'src/types/select-options-item.model'
4 import { Component, HostListener, OnInit } from '@angular/core'
5 import { ActivatedRoute, Router } from '@angular/router'
6 import { Notifier } from '@app/core'
7 import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
8 import { Video, VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main'
9 import { LiveVideoService } from '@app/shared/shared-video-live'
10 import { LoadingBarService } from '@ngx-loading-bar/core'
11 import { logger } from '@root-helpers/logger'
12 import { pick, simpleObjectsDeepEqual } from '@shared/core-utils'
13 import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
14 import { VideoSource } from '@shared/models/videos/video-source'
15 import { hydrateFormFromVideo } from './shared/video-edit-utils'
18 selector: 'my-videos-update',
19 styleUrls: [ './shared/video-edit.component.scss' ],
20 templateUrl: './video-update.component.html'
22 export class VideoUpdateComponent extends FormReactive implements OnInit {
24 videoDetails: VideoDetails
25 videoSource: VideoSource
26 userVideoChannels: SelectChannelItem[] = []
27 videoCaptions: VideoCaptionEdit[] = []
30 isUpdatingVideo = false
31 forbidScheduledPublication = false
33 private updateDone = false
36 protected formReactiveService: FormReactiveService,
37 private route: ActivatedRoute,
38 private router: Router,
39 private notifier: Notifier,
40 private videoService: VideoService,
41 private loadingBar: LoadingBarService,
42 private videoCaptionService: VideoCaptionService,
43 private liveVideoService: LiveVideoService
51 const { videoData } = this.route.snapshot.data
52 const { video, videoChannels, videoCaptions, videoSource, liveVideo } = videoData
54 this.videoDetails = video
55 this.videoEdit = new VideoEdit(this.videoDetails)
57 this.userVideoChannels = videoChannels
58 this.videoCaptions = videoCaptions
59 this.videoSource = videoSource
60 this.liveVideo = liveVideo
62 this.forbidScheduledPublication = this.videoEdit.privacy !== VideoPrivacy.PRIVATE
66 hydrateFormFromVideo(this.form, this.videoEdit, true)
69 this.form.patchValue({
70 saveReplay: this.liveVideo.saveReplay,
71 replayPrivacy: this.liveVideo.replaySettings ? this.liveVideo.replaySettings.privacy : VideoPrivacy.PRIVATE,
72 latencyMode: this.liveVideo.latencyMode,
73 permanentLive: this.liveVideo.permanentLive
78 @HostListener('window:beforeunload', [ '$event' ])
79 onUnload (event: any) {
80 const { text, canDeactivate } = this.canDeactivate()
82 if (canDeactivate) return
84 event.returnValue = text
88 canDeactivate (): { canDeactivate: boolean, text?: string } {
89 if (this.updateDone === true) return { canDeactivate: true }
91 const text = $localize`You have unsaved changes! If you leave, your changes will be lost.`
93 for (const caption of this.videoCaptions) {
94 if (caption.action) return { canDeactivate: false, text }
97 return { canDeactivate: this.formChanged === false, text }
100 isWaitTranscodingHidden () {
101 if (this.videoDetails.getFiles().length > 1) { // Already transcoded
109 await this.waitPendingCheck()
112 if (!this.form.valid || this.isUpdatingVideo === true) {
116 this.videoEdit.patch(this.form.value)
118 this.loadingBar.useRef().start()
119 this.isUpdatingVideo = true
122 this.videoService.updateVideo(this.videoEdit)
124 // Then update captions
125 switchMap(() => this.videoCaptionService.updateCaptions(this.videoEdit.id, this.videoCaptions)),
128 if (!this.liveVideo) return of(undefined)
130 const saveReplay = !!this.form.value.saveReplay
131 const replaySettings = saveReplay
132 ? { privacy: this.form.value.replayPrivacy }
135 const liveVideoUpdate: LiveVideoUpdate = {
138 permanentLive: !!this.form.value.permanentLive,
139 latencyMode: this.form.value.latencyMode
142 // Don't update live attributes if they did not change
143 const baseVideo = pick(this.liveVideo, Object.keys(liveVideoUpdate) as (keyof LiveVideoUpdate)[])
144 const liveChanged = !simpleObjectsDeepEqual(baseVideo, liveVideoUpdate)
145 if (!liveChanged) return of(undefined)
147 return this.liveVideoService.updateLive(this.videoEdit.id, liveVideoUpdate)
152 this.updateDone = true
153 this.isUpdatingVideo = false
154 this.loadingBar.useRef().complete()
155 this.notifier.success($localize`Video updated.`)
156 this.router.navigateByUrl(Video.buildWatchUrl(this.videoEdit))
160 this.loadingBar.useRef().complete()
161 this.isUpdatingVideo = false
162 this.notifier.error(err.message)
168 hydratePluginFieldsFromVideo () {
169 if (!this.videoEdit.pluginData) return
171 this.form.patchValue({
172 pluginData: this.videoEdit.pluginData
177 return Video.buildWatchUrl(this.videoDetails)