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