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