]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
Force live type specification in first step
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-go-live.component.ts
1 import { forkJoin } from 'rxjs'
2 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
5 import { scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
8 import { LiveVideoService } from '@app/shared/shared-video-live'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode } 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-go-live.component.scss',
19 './video-send.scss'
20 ]
21 })
22 export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
25
26 firstStepPermanentLive: boolean
27
28 isInUpdateForm = false
29
30 liveVideo: LiveVideo
31
32 videoId: number
33 videoUUID: string
34 videoShortUUID: string
35
36 error: string
37
38 constructor (
39 protected formValidatorService: FormValidatorService,
40 protected loadingBar: LoadingBarService,
41 protected notifier: Notifier,
42 protected authService: AuthService,
43 protected serverService: ServerService,
44 protected videoService: VideoService,
45 protected videoCaptionService: VideoCaptionService,
46 private liveVideoService: LiveVideoService,
47 private router: Router,
48 private hooks: HooksService
49 ) {
50 super()
51 }
52
53 ngOnInit () {
54 super.ngOnInit()
55 }
56
57 ngAfterViewInit () {
58 this.hooks.runAction('action:go-live.init', 'video-edit')
59 }
60
61 canDeactivate () {
62 return { canDeactivate: true }
63 }
64
65 goLive () {
66 const name = 'Live'
67
68 const video: LiveVideoCreate = {
69 name,
70 privacy: this.highestPrivacy,
71 nsfw: this.serverConfig.instance.isNSFW,
72 waitTranscoding: true,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 permanentLive: this.firstStepPermanentLive,
76 saveReplay: this.firstStepPermanentLive === false && this.isReplayAllowed(),
77 channelId: this.firstStepChannelId
78 }
79
80 // Go live in private mode, but correctly fill the update form with the first user choice
81 const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
82 this.form.patchValue(toPatch)
83
84 this.liveVideoService.goLive(video)
85 .subscribe({
86 next: res => {
87 this.videoId = res.video.id
88 this.videoUUID = res.video.uuid
89 this.videoShortUUID = res.video.shortUUID
90 this.isInUpdateForm = true
91
92 this.firstStepDone.emit(name)
93
94 this.fetchVideoLive()
95 },
96
97 error: err => {
98 this.firstStepError.emit()
99
100 let message = err.message
101
102 const error = err.body as PeerTubeProblemDocument
103
104 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
105 message = $localize`Cannot create live because this instance have too many created lives`
106 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
107 message = $localize`Cannot create live because you created too many lives`
108 }
109
110 this.notifier.error(message)
111 }
112 })
113 }
114
115 updateSecondStep () {
116 if (this.checkForm() === false) {
117 return
118 }
119
120 const video = new VideoEdit()
121 video.patch(this.form.value)
122 video.id = this.videoId
123 video.uuid = this.videoUUID
124 video.shortUUID = this.videoShortUUID
125
126 const liveVideoUpdate: LiveVideoUpdate = {
127 saveReplay: this.form.value.saveReplay,
128 permanentLive: this.form.value.permanentLive
129 }
130
131 // Update the video
132 forkJoin([
133 this.updateVideoAndCaptions(video),
134
135 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
136 ]).subscribe({
137 next: () => {
138 this.notifier.success($localize`Live published.`)
139
140 this.router.navigateByUrl(Video.buildWatchUrl(video))
141 },
142
143 error: err => {
144 this.error = err.message
145 scrollToTop()
146 console.error(err)
147 }
148 })
149 }
150
151 getMaxLiveDuration () {
152 return this.serverConfig.live.maxDuration / 1000
153 }
154
155 isWaitTranscodingEnabled () {
156 return this.form.value['saveReplay'] === true
157 }
158
159 getNormalLiveDescription () {
160 if (this.isReplayAllowed()) {
161 return $localize`Stream only once and save a replay of your live`
162 }
163
164 return $localize`Stream only once`
165 }
166
167 getPermanentLiveDescription () {
168 if (this.isReplayAllowed()) {
169 return $localize`Stream multiple times, replays can't be saved`
170 }
171
172 return $localize`Stream multiple times using the same URL`
173 }
174
175 private isReplayAllowed () {
176 return this.serverConfig.live.allowReplay
177 }
178
179 private fetchVideoLive () {
180 this.liveVideoService.getVideoLive(this.videoId)
181 .subscribe({
182 next: liveVideo => {
183 this.liveVideo = liveVideo
184 },
185
186 error: err => {
187 this.firstStepError.emit()
188 this.notifier.error(err.message)
189 }
190 })
191 }
192 }