]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import { forkJoin } from 'rxjs' | |
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' | |
8 | import { LiveVideoService, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main' | |
9 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
10 | import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, ServerErrorCode, VideoPrivacy } from '@shared/models' | |
11 | import { VideoSend } from './video-send' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-video-go-live', | |
15 | templateUrl: './video-go-live.component.html', | |
16 | styleUrls: [ | |
17 | '../shared/video-edit.component.scss', | |
18 | './video-send.scss' | |
19 | ] | |
20 | }) | |
21 | export class VideoGoLiveComponent extends VideoSend implements OnInit, CanComponentDeactivate { | |
22 | @Output() firstStepDone = new EventEmitter<string>() | |
23 | @Output() firstStepError = new EventEmitter<void>() | |
24 | ||
25 | isInUpdateForm = false | |
26 | ||
27 | liveVideo: LiveVideo | |
28 | videoId: number | |
29 | videoUUID: string | |
30 | error: string | |
31 | ||
32 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC | |
33 | ||
34 | constructor ( | |
35 | protected formValidatorService: FormValidatorService, | |
36 | protected loadingBar: LoadingBarService, | |
37 | protected notifier: Notifier, | |
38 | protected authService: AuthService, | |
39 | protected serverService: ServerService, | |
40 | protected videoService: VideoService, | |
41 | protected videoCaptionService: VideoCaptionService, | |
42 | private liveVideoService: LiveVideoService, | |
43 | private router: Router | |
44 | ) { | |
45 | super() | |
46 | } | |
47 | ||
48 | ngOnInit () { | |
49 | super.ngOnInit() | |
50 | } | |
51 | ||
52 | canDeactivate () { | |
53 | return { canDeactivate: true } | |
54 | } | |
55 | ||
56 | goLive () { | |
57 | const video: LiveVideoCreate = { | |
58 | name: 'Live', | |
59 | privacy: VideoPrivacy.PRIVATE, | |
60 | nsfw: this.serverConfig.instance.isNSFW, | |
61 | waitTranscoding: true, | |
62 | commentsEnabled: true, | |
63 | downloadEnabled: true, | |
64 | channelId: this.firstStepChannelId | |
65 | } | |
66 | ||
67 | this.firstStepDone.emit(name) | |
68 | ||
69 | // Go live in private mode, but correctly fill the update form with the first user choice | |
70 | const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId }) | |
71 | this.form.patchValue(toPatch) | |
72 | ||
73 | this.liveVideoService.goLive(video).subscribe( | |
74 | res => { | |
75 | this.videoId = res.video.id | |
76 | this.videoUUID = res.video.uuid | |
77 | this.isInUpdateForm = true | |
78 | ||
79 | this.fetchVideoLive() | |
80 | }, | |
81 | ||
82 | err => { | |
83 | this.firstStepError.emit() | |
84 | ||
85 | let message = err.message | |
86 | ||
87 | if (err.body?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) { | |
88 | message = $localize`Cannot create live because this instance have too many created lives` | |
89 | } else if (err.body?.code) { | |
90 | message = $localize`Cannot create live because you created too many lives` | |
91 | } | |
92 | ||
93 | this.notifier.error(message) | |
94 | } | |
95 | ) | |
96 | } | |
97 | ||
98 | updateSecondStep () { | |
99 | if (this.checkForm() === false) { | |
100 | return | |
101 | } | |
102 | ||
103 | const video = new VideoEdit() | |
104 | video.patch(this.form.value) | |
105 | video.id = this.videoId | |
106 | video.uuid = this.videoUUID | |
107 | ||
108 | const liveVideoUpdate: LiveVideoUpdate = { | |
109 | saveReplay: this.form.value.saveReplay | |
110 | } | |
111 | ||
112 | // Update the video | |
113 | forkJoin([ | |
114 | this.updateVideoAndCaptions(video), | |
115 | ||
116 | this.liveVideoService.updateLive(this.videoId, liveVideoUpdate) | |
117 | ]).subscribe( | |
118 | () => { | |
119 | this.notifier.success($localize`Live published.`) | |
120 | ||
121 | this.router.navigate(['/videos/watch', video.uuid]) | |
122 | }, | |
123 | ||
124 | err => { | |
125 | this.error = err.message | |
126 | scrollToTop() | |
127 | console.error(err) | |
128 | } | |
129 | ) | |
130 | } | |
131 | ||
132 | getMaxLiveDuration () { | |
133 | return this.serverConfig.live.maxDuration / 1000 | |
134 | } | |
135 | ||
136 | private fetchVideoLive () { | |
137 | this.liveVideoService.getVideoLive(this.videoId) | |
138 | .subscribe( | |
139 | liveVideo => { | |
140 | this.liveVideo = liveVideo | |
141 | }, | |
142 | ||
143 | err => { | |
144 | this.firstStepError.emit() | |
145 | this.notifier.error(err.message) | |
146 | } | |
147 | ) | |
148 | } | |
149 | } |