]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
Merge branch 'constant-registry' into develop
[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'
d4a8e7a6 3import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
c6c0fa6c 4import { Router } from '@angular/router'
2e257e36 5import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
c6c0fa6c
C
6import { scrollToTop } from '@app/helpers'
7import { FormValidatorService } from '@app/shared/shared-forms'
d4a8e7a6 8import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
f8c00564 9import { LiveVideoService } from '@app/shared/shared-video-live'
c6c0fa6c 10import { LoadingBarService } from '@ngx-loading-bar/core'
e030bfb5 11import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode, VideoPrivacy } from '@shared/models'
c6c0fa6c
C
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})
2e257e36 22export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
c6c0fa6c
C
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
25
26 isInUpdateForm = false
27
a5cf76af 28 liveVideo: LiveVideo
c6c0fa6c
C
29 videoId: number
30 videoUUID: string
31 error: string
32
c6c0fa6c
C
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,
a5cf76af 41 private liveVideoService: LiveVideoService,
2e257e36
C
42 private router: Router,
43 private hooks: HooksService
c6c0fa6c
C
44 ) {
45 super()
46 }
47
48 ngOnInit () {
49 super.ngOnInit()
50 }
51
2e257e36
C
52 ngAfterViewInit () {
53 this.hooks.runAction('action:go-live.init', 'video-edit')
54 }
55
c6c0fa6c
C
56 canDeactivate () {
57 return { canDeactivate: true }
58 }
59
60 goLive () {
d487a997
C
61 const name = 'Live'
62
b5b68755 63 const video: LiveVideoCreate = {
d487a997 64 name,
a3f45a2a 65 privacy: this.highestPrivacy,
c6c0fa6c
C
66 nsfw: this.serverConfig.instance.isNSFW,
67 waitTranscoding: true,
68 commentsEnabled: true,
69 downloadEnabled: true,
70 channelId: this.firstStepChannelId
71 }
72
c6c0fa6c
C
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
a5cf76af 77 this.liveVideoService.goLive(video).subscribe(
c6c0fa6c
C
78 res => {
79 this.videoId = res.video.id
80 this.videoUUID = res.video.uuid
81 this.isInUpdateForm = true
82
f8c00564
C
83 this.firstStepDone.emit(name)
84
c6c0fa6c
C
85 this.fetchVideoLive()
86 },
87
88 err => {
89 this.firstStepError.emit()
a056ca48
C
90
91 let message = err.message
92
e030bfb5
C
93 const error = err.body as PeerTubeProblemDocument
94
95 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
a056ca48 96 message = $localize`Cannot create live because this instance have too many created lives`
e030bfb5 97 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
a056ca48
C
98 message = $localize`Cannot create live because you created too many lives`
99 }
100
101 this.notifier.error(message)
c6c0fa6c
C
102 }
103 )
104 }
105
106 updateSecondStep () {
107 if (this.checkForm() === false) {
108 return
109 }
110
111 const video = new VideoEdit()
112 video.patch(this.form.value)
113 video.id = this.videoId
114 video.uuid = this.videoUUID
115
b5b68755 116 const liveVideoUpdate: LiveVideoUpdate = {
bb4ba6d9
C
117 saveReplay: this.form.value.saveReplay,
118 permanentLive: this.form.value.permanentLive
b5b68755
C
119 }
120
c6c0fa6c 121 // Update the video
b5b68755
C
122 forkJoin([
123 this.updateVideoAndCaptions(video),
c6c0fa6c 124
b5b68755
C
125 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
126 ]).subscribe(
127 () => {
128 this.notifier.success($localize`Live published.`)
129
d4a8e7a6 130 this.router.navigateByUrl(Video.buildWatchUrl(video))
b5b68755 131 },
c6c0fa6c 132
b5b68755
C
133 err => {
134 this.error = err.message
135 scrollToTop()
136 console.error(err)
137 }
138 )
139 }
c6c0fa6c 140
b5b68755
C
141 getMaxLiveDuration () {
142 return this.serverConfig.live.maxDuration / 1000
c6c0fa6c
C
143 }
144
ddb62a85
C
145 isWaitTranscodingEnabled () {
146 return this.form.value['saveReplay'] === true
147 }
148
c6c0fa6c 149 private fetchVideoLive () {
a5cf76af 150 this.liveVideoService.getVideoLive(this.videoId)
c6c0fa6c 151 .subscribe(
a5cf76af
C
152 liveVideo => {
153 this.liveVideo = liveVideo
c6c0fa6c
C
154 },
155
156 err => {
157 this.firstStepError.emit()
158 this.notifier.error(err.message)
159 }
160 )
161 }
162}