]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Use async/await in lib and initializers
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
a685e25c 1import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e
C
2import { ActivatedRoute, Router } from '@angular/router'
3import { Observable } from 'rxjs/Observable'
4import { Subscription } from 'rxjs/Subscription'
5
80624154 6import videojs from 'video.js'
aa8b6df4
C
7import '../../../assets/player/peertube-videojs-plugin'
8
8b13c289 9import { MetaService } from '@ngx-meta/core'
df98563e
C
10import { NotificationsService } from 'angular2-notifications'
11
12import { AuthService, ConfirmService } from '../../core'
a96aed15 13import { VideoDownloadComponent } from './video-download.component'
df98563e
C
14import { VideoShareComponent } from './video-share.component'
15import { VideoReportComponent } from './video-report.component'
154898b0 16import { Video, VideoService } from '../shared'
35bf0c83 17import { VideoBlacklistService } from '../../shared'
aa8b6df4 18import { UserVideoRateType, VideoRateType } from '../../../../../shared'
dc8bc31b 19
dc8bc31b
C
20@Component({
21 selector: 'my-video-watch',
ec8d8440
C
22 templateUrl: './video-watch.component.html',
23 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b 24})
0629423c 25export class VideoWatchComponent implements OnInit, OnDestroy {
a96aed15 26 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
df98563e
C
27 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
28 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
29
30 downloadSpeed: number
31 error = false
32 loading = false
33 numPeers: number
34 player: videojs.Player
33c4972d 35 playerElement: HTMLMediaElement
df98563e 36 uploadSpeed: number
154898b0 37 userRating: UserVideoRateType = null
df98563e 38 video: Video = null
efee3505 39 videoPlayerLoaded = false
f1013131 40 videoNotFound = false
df98563e 41
df98563e 42 private paramsSub: Subscription
df98563e
C
43
44 constructor (
4fd8aa32 45 private elementRef: ElementRef,
0629423c 46 private route: ActivatedRoute,
92fb909c 47 private router: Router,
d3ef341a 48 private videoService: VideoService,
35bf0c83 49 private videoBlacklistService: VideoBlacklistService,
92fb909c 50 private confirmService: ConfirmService,
3ec343a4 51 private metaService: MetaService,
7ddd02c9
C
52 private authService: AuthService,
53 private notificationsService: NotificationsService
d3ef341a 54 ) {}
dc8bc31b 55
df98563e 56 ngOnInit () {
13fc89f4 57 this.paramsSub = this.route.params.subscribe(routeParams => {
0a6658fd
C
58 let uuid = routeParams['uuid']
59 this.videoService.getVideo(uuid).subscribe(
92fb909c
C
60 video => this.onVideoFetched(video),
61
f1013131
C
62 error => {
63 this.videoNotFound = true
64 console.error(error)
65 }
df98563e
C
66 )
67 })
d1992b93
C
68 }
69
df98563e 70 ngOnDestroy () {
2ed6a0ae 71 // Remove player if it exists
efee3505 72 if (this.videoPlayerLoaded === true) {
2ed6a0ae
C
73 videojs(this.playerElement).dispose()
74 }
067e3f84 75
13fc89f4 76 // Unsubscribe subscriptions
df98563e 77 this.paramsSub.unsubscribe()
dc8bc31b 78 }
98b01bac 79
df98563e
C
80 setLike () {
81 if (this.isUserLoggedIn() === false) return
d38b8281 82 // Already liked this video
df98563e 83 if (this.userRating === 'like') return
d38b8281
C
84
85 this.videoService.setVideoLike(this.video.id)
86 .subscribe(
87 () => {
88 // Update the video like attribute
df98563e
C
89 this.updateVideoRating(this.userRating, 'like')
90 this.userRating = 'like'
d38b8281
C
91 },
92
bfb3a98f 93 err => this.notificationsService.error('Error', err.message)
df98563e 94 )
d38b8281
C
95 }
96
df98563e
C
97 setDislike () {
98 if (this.isUserLoggedIn() === false) return
d38b8281 99 // Already disliked this video
df98563e 100 if (this.userRating === 'dislike') return
d38b8281
C
101
102 this.videoService.setVideoDislike(this.video.id)
103 .subscribe(
104 () => {
105 // Update the video dislike attribute
df98563e
C
106 this.updateVideoRating(this.userRating, 'dislike')
107 this.userRating = 'dislike'
d38b8281
C
108 },
109
bfb3a98f 110 err => this.notificationsService.error('Error', err.message)
df98563e 111 )
d38b8281
C
112 }
113
df98563e
C
114 removeVideo (event: Event) {
115 event.preventDefault()
ab683a8e 116
198b205c
GS
117 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
118 res => {
df98563e 119 if (res === false) return
198b205c
GS
120
121 this.videoService.removeVideo(this.video.id)
ab683a8e
C
122 .subscribe(
123 status => {
df98563e 124 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
ab683a8e 125 // Go back to the video-list.
df98563e 126 this.router.navigate(['/videos/list'])
ab683a8e
C
127 },
128
129 error => this.notificationsService.error('Error', error.text)
df98563e 130 )
198b205c 131 }
df98563e 132 )
198b205c
GS
133 }
134
df98563e
C
135 blacklistVideo (event: Event) {
136 event.preventDefault()
ab683a8e 137
198b205c
GS
138 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
139 res => {
df98563e 140 if (res === false) return
198b205c 141
35bf0c83
C
142 this.videoBlacklistService.blacklistVideo(this.video.id)
143 .subscribe(
144 status => {
145 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
146 this.router.navigate(['/videos/list'])
147 },
198b205c 148
35bf0c83
C
149 error => this.notificationsService.error('Error', error.text)
150 )
198b205c 151 }
df98563e 152 )
198b205c
GS
153 }
154
df98563e
C
155 showReportModal (event: Event) {
156 event.preventDefault()
157 this.videoReportModal.show()
4f8c0eb0
C
158 }
159
df98563e
C
160 showShareModal () {
161 this.videoShareModal.show()
99cc4f49
C
162 }
163
a96aed15 164 showDownloadModal (event: Event) {
df98563e 165 event.preventDefault()
a96aed15 166 this.videoDownloadModal.show()
99cc4f49
C
167 }
168
df98563e
C
169 isUserLoggedIn () {
170 return this.authService.isLoggedIn()
4f8c0eb0
C
171 }
172
df98563e
C
173 canUserUpdateVideo () {
174 return this.video.isUpdatableBy(this.authService.getUser())
d8e689b8
C
175 }
176
df98563e
C
177 isVideoRemovable () {
178 return this.video.isRemovableBy(this.authService.getUser())
198b205c
GS
179 }
180
df98563e
C
181 isVideoBlacklistable () {
182 return this.video.isBlackistableBy(this.authService.getUser())
198b205c
GS
183 }
184
0c31c33d
C
185 private handleError (err: any) {
186 const errorMessage: string = typeof err === 'string' ? err : err.message
187 let message = ''
188
189 if (errorMessage.indexOf('http error') !== -1) {
190 message = 'Cannot fetch video from server, maybe down.'
191 } else {
192 message = errorMessage
193 }
194
195 this.notificationsService.error('Error', message)
196 }
197
df98563e 198 private checkUserRating () {
d38b8281 199 // Unlogged users do not have ratings
df98563e 200 if (this.isUserLoggedIn() === false) return
d38b8281
C
201
202 this.videoService.getUserVideoRating(this.video.id)
203 .subscribe(
b632e904 204 ratingObject => {
d38b8281 205 if (ratingObject) {
df98563e 206 this.userRating = ratingObject.rating
d38b8281
C
207 }
208 },
209
bfb3a98f 210 err => this.notificationsService.error('Error', err.message)
df98563e 211 )
d38b8281
C
212 }
213
df98563e
C
214 private onVideoFetched (video: Video) {
215 this.video = video
92fb909c 216
df98563e 217 let observable
92fb909c 218 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
df98563e 219 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
92fb909c 220 } else {
df98563e 221 observable = Observable.of(true)
92fb909c
C
222 }
223
224 observable.subscribe(
225 res => {
226 if (res === false) {
efee3505 227
df98563e 228 return this.router.navigate([ '/videos/list' ])
92fb909c
C
229 }
230
aa8b6df4
C
231 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
232
233 const videojsOptions = {
234 controls: true,
235 autoplay: true,
236 plugins: {
237 peertube: {
238 videoFiles: this.video.files,
239 playerElement: this.playerElement,
240 autoplay: true,
241 peerTubeLink: false
242 }
243 }
244 }
245
efee3505
C
246 this.videoPlayerLoaded = true
247
aa8b6df4
C
248 const self = this
249 videojs(this.playerElement, videojsOptions, function () {
250 self.player = this
251 this.on('customError', (event, data) => {
252 self.handleError(data.err)
253 })
254
255 this.on('torrentInfo', (event, data) => {
256 self.downloadSpeed = data.downloadSpeed
257 self.numPeers = data.numPeers
258 self.uploadSpeed = data.uploadSpeed
259 })
260 })
261
df98563e 262 this.setOpenGraphTags()
df98563e 263 this.checkUserRating()
92fb909c 264 }
df98563e 265 )
92fb909c
C
266 }
267
154898b0 268 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
269 let likesToIncrement = 0
270 let dislikesToIncrement = 0
d38b8281
C
271
272 if (oldRating) {
df98563e
C
273 if (oldRating === 'like') likesToIncrement--
274 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
275 }
276
df98563e
C
277 if (newRating === 'like') likesToIncrement++
278 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 279
df98563e
C
280 this.video.likes += likesToIncrement
281 this.video.dislikes += dislikesToIncrement
d38b8281
C
282 }
283
df98563e
C
284 private setOpenGraphTags () {
285 this.metaService.setTitle(this.video.name)
758b996d 286
df98563e 287 this.metaService.setTag('og:type', 'video')
3ec343a4 288
df98563e
C
289 this.metaService.setTag('og:title', this.video.name)
290 this.metaService.setTag('name', this.video.name)
3ec343a4 291
df98563e
C
292 this.metaService.setTag('og:description', this.video.description)
293 this.metaService.setTag('description', this.video.description)
3ec343a4 294
d38309c3 295 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 296
df98563e 297 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 298
df98563e 299 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 300
df98563e
C
301 this.metaService.setTag('og:url', window.location.href)
302 this.metaService.setTag('url', window.location.href)
3ec343a4 303 }
dc8bc31b 304}