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