]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
Add modal to display live information
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-go-live.component.ts
1
2 import { forkJoin } from 'rxjs'
3 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core'
6 import { scrollToTop } from '@app/helpers'
7 import { FormValidatorService } from '@app/shared/shared-forms'
8 import { LiveVideoService, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoPrivacy } from '@shared/models'
11 import { VideoSend } from './video-send'
12
13 @Component({
14 selector: 'my-video-go-live',
15 templateUrl: './video-go-live.component.html',
16 styleUrls: [
17 '../shared/video-edit.component.scss',
18 './video-send.scss'
19 ]
20 })
21 export class VideoGoLiveComponent extends VideoSend implements OnInit, CanComponentDeactivate {
22 @Output() firstStepDone = new EventEmitter<string>()
23 @Output() firstStepError = new EventEmitter<void>()
24
25 isInUpdateForm = false
26
27 liveVideo: LiveVideo
28 videoId: number
29 videoUUID: string
30 error: string
31
32 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
33
34 constructor (
35 protected formValidatorService: FormValidatorService,
36 protected loadingBar: LoadingBarService,
37 protected notifier: Notifier,
38 protected authService: AuthService,
39 protected serverService: ServerService,
40 protected videoService: VideoService,
41 protected videoCaptionService: VideoCaptionService,
42 private liveVideoService: LiveVideoService,
43 private router: Router
44 ) {
45 super()
46 }
47
48 ngOnInit () {
49 super.ngOnInit()
50 }
51
52 canDeactivate () {
53 return { canDeactivate: true }
54 }
55
56 goLive () {
57 const video: LiveVideoCreate = {
58 name: 'Live',
59 privacy: VideoPrivacy.PRIVATE,
60 nsfw: this.serverConfig.instance.isNSFW,
61 waitTranscoding: true,
62 commentsEnabled: true,
63 downloadEnabled: true,
64 channelId: this.firstStepChannelId
65 }
66
67 this.firstStepDone.emit(name)
68
69 // Go live in private mode, but correctly fill the update form with the first user choice
70 const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
71 this.form.patchValue(toPatch)
72
73 this.liveVideoService.goLive(video).subscribe(
74 res => {
75 this.videoId = res.video.id
76 this.videoUUID = res.video.uuid
77 this.isInUpdateForm = true
78
79 this.fetchVideoLive()
80 },
81
82 err => {
83 this.firstStepError.emit()
84 this.notifier.error(err.message)
85 }
86 )
87 }
88
89 updateSecondStep () {
90 if (this.checkForm() === false) {
91 return
92 }
93
94 const video = new VideoEdit()
95 video.patch(this.form.value)
96 video.id = this.videoId
97 video.uuid = this.videoUUID
98
99 const liveVideoUpdate: LiveVideoUpdate = {
100 saveReplay: this.form.value.saveReplay
101 }
102
103 // Update the video
104 forkJoin([
105 this.updateVideoAndCaptions(video),
106
107 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
108 ]).subscribe(
109 () => {
110 this.notifier.success($localize`Live published.`)
111
112 this.router.navigate(['/videos/watch', video.uuid])
113 },
114
115 err => {
116 this.error = err.message
117 scrollToTop()
118 console.error(err)
119 }
120 )
121 }
122
123 getMaxLiveDuration () {
124 return this.serverConfig.live.maxDuration / 1000
125 }
126
127 private fetchVideoLive () {
128 this.liveVideoService.getVideoLive(this.videoId)
129 .subscribe(
130 liveVideo => {
131 this.liveVideo = liveVideo
132 },
133
134 err => {
135 this.firstStepError.emit()
136 this.notifier.error(err.message)
137 }
138 )
139 }
140 }