]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
Add max lives limit
[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'
a5cf76af 8import { LiveVideoService, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
c6c0fa6c 9import { LoadingBarService } from '@ngx-loading-bar/core'
a056ca48 10import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, ServerErrorCode, VideoPrivacy } from '@shared/models'
c6c0fa6c
C
11import { 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})
21export class VideoGoLiveComponent extends VideoSend implements OnInit, CanComponentDeactivate {
22 @Output() firstStepDone = new EventEmitter<string>()
23 @Output() firstStepError = new EventEmitter<void>()
24
25 isInUpdateForm = false
26
a5cf76af 27 liveVideo: LiveVideo
c6c0fa6c
C
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,
a5cf76af 42 private liveVideoService: LiveVideoService,
c6c0fa6c
C
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 () {
b5b68755 57 const video: LiveVideoCreate = {
c6c0fa6c
C
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
a5cf76af 73 this.liveVideoService.goLive(video).subscribe(
c6c0fa6c
C
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()
a056ca48
C
84
85 let message = err.message
86
87 if (err.body?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
88 message = $localize`Cannot create live because this instance have too many created lives`
89 } else if (err.body?.code) {
90 message = $localize`Cannot create live because you created too many lives`
91 }
92
93 this.notifier.error(message)
c6c0fa6c
C
94 }
95 )
96 }
97
98 updateSecondStep () {
99 if (this.checkForm() === false) {
100 return
101 }
102
103 const video = new VideoEdit()
104 video.patch(this.form.value)
105 video.id = this.videoId
106 video.uuid = this.videoUUID
107
b5b68755
C
108 const liveVideoUpdate: LiveVideoUpdate = {
109 saveReplay: this.form.value.saveReplay
110 }
111
c6c0fa6c 112 // Update the video
b5b68755
C
113 forkJoin([
114 this.updateVideoAndCaptions(video),
c6c0fa6c 115
b5b68755
C
116 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
117 ]).subscribe(
118 () => {
119 this.notifier.success($localize`Live published.`)
120
121 this.router.navigate(['/videos/watch', video.uuid])
122 },
c6c0fa6c 123
b5b68755
C
124 err => {
125 this.error = err.message
126 scrollToTop()
127 console.error(err)
128 }
129 )
130 }
c6c0fa6c 131
b5b68755
C
132 getMaxLiveDuration () {
133 return this.serverConfig.live.maxDuration / 1000
c6c0fa6c
C
134 }
135
136 private fetchVideoLive () {
a5cf76af 137 this.liveVideoService.getVideoLive(this.videoId)
c6c0fa6c 138 .subscribe(
a5cf76af
C
139 liveVideo => {
140 this.liveVideo = liveVideo
c6c0fa6c
C
141 },
142
143 err => {
144 this.firstStepError.emit()
145 this.notifier.error(err.message)
146 }
147 )
148 }
149}