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