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