1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Observable } from 'rxjs/Observable'
4 import { Subscription } from 'rxjs/Subscription'
6 import videojs from 'video.js'
7 import { MetaService } from '@ngx-meta/core'
8 import { NotificationsService } from 'angular2-notifications'
10 import { AuthService, ConfirmService } from '../../core'
11 import { VideoMagnetComponent } from './video-magnet.component'
12 import { VideoShareComponent } from './video-share.component'
13 import { VideoReportComponent } from './video-report.component'
14 import { Video, VideoService } from '../shared'
15 import { WebTorrentService } from './webtorrent.service'
16 import { UserVideoRateType, VideoRateType, UserVideoRate } from '../../../../../shared'
19 selector: 'my-video-watch',
20 templateUrl: './video-watch.component.html',
21 styleUrls: [ './video-watch.component.scss' ]
23 export class VideoWatchComponent implements OnInit, OnDestroy {
24 private static LOADTIME_TOO_LONG = 20000
26 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent
27 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
28 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
34 player: videojs.Player
35 playerElement: Element
37 userRating: UserVideoRateType = null
41 private errorTimer: number
42 private paramsSub: Subscription
43 private errorsSub: Subscription
44 private warningsSub: Subscription
45 private torrentInfosInterval: number
48 private elementRef: ElementRef,
49 private ngZone: NgZone,
50 private route: ActivatedRoute,
51 private router: Router,
52 private videoService: VideoService,
53 private confirmService: ConfirmService,
54 private metaService: MetaService,
55 private webTorrentService: WebTorrentService,
56 private authService: AuthService,
57 private notificationsService: NotificationsService
61 this.paramsSub = this.route.params.subscribe(routeParams => {
62 let id = routeParams['id']
63 this.videoService.getVideo(id).subscribe(
64 video => this.onVideoFetched(video),
68 this.videoNotFound = true
73 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
75 const videojsOptions = {
81 videojs(this.playerElement, videojsOptions, function () {
85 this.errorsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.error('Error', err.message))
86 this.warningsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.alert('Warning', err.message))
90 // Remove WebTorrent stuff
91 console.log('Removing video from webtorrent.')
92 window.clearInterval(this.torrentInfosInterval)
93 window.clearTimeout(this.errorTimer)
95 if (this.video !== null && this.webTorrentService.has(this.video.magnetUri)) {
96 this.webTorrentService.remove(this.video.magnetUri)
100 videojs(this.playerElement).dispose()
102 // Unsubscribe subscriptions
103 this.paramsSub.unsubscribe()
104 this.errorsSub.unsubscribe()
105 this.warningsSub.unsubscribe()
111 // We are loading the video
114 console.log('Adding ' + this.video.magnetUri + '.')
116 // The callback might never return if there are network issues
117 // So we create a timer to inform the user the load is abnormally long
118 this.errorTimer = window.setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG)
120 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
121 // Clear the error timer
122 window.clearTimeout(this.errorTimer)
123 // Maybe the error was fired by the timer, so reset it
126 // We are not loading the video anymore
129 console.log('Added ' + this.video.magnetUri + '.')
130 torrent.files[0].renderTo(this.playerElement, (err) => {
132 this.notificationsService.error('Error', 'Cannot append the file in the video element.')
136 // Hack to "simulate" src link in video.js >= 6
137 // If no, we can't play the video after pausing it
138 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
139 (this.player as any).src = () => true
144 this.runInProgress(torrent)
149 if (this.isUserLoggedIn() === false) return
150 // Already liked this video
151 if (this.userRating === 'like') return
153 this.videoService.setVideoLike(this.video.id)
156 // Update the video like attribute
157 this.updateVideoRating(this.userRating, 'like')
158 this.userRating = 'like'
161 err => this.notificationsService.error('Error', err.text)
166 if (this.isUserLoggedIn() === false) return
167 // Already disliked this video
168 if (this.userRating === 'dislike') return
170 this.videoService.setVideoDislike(this.video.id)
173 // Update the video dislike attribute
174 this.updateVideoRating(this.userRating, 'dislike')
175 this.userRating = 'dislike'
178 err => this.notificationsService.error('Error', err.text)
182 removeVideo (event: Event) {
183 event.preventDefault()
185 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
187 if (res === false) return
189 this.videoService.removeVideo(this.video.id)
192 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
193 // Go back to the video-list.
194 this.router.navigate(['/videos/list'])
197 error => this.notificationsService.error('Error', error.text)
203 blacklistVideo (event: Event) {
204 event.preventDefault()
206 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
208 if (res === false) return
210 this.videoService.blacklistVideo(this.video.id)
213 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
214 this.router.navigate(['/videos/list'])
217 error => this.notificationsService.error('Error', error.text)
223 showReportModal (event: Event) {
224 event.preventDefault()
225 this.videoReportModal.show()
229 this.videoShareModal.show()
232 showMagnetUriModal (event: Event) {
233 event.preventDefault()
234 this.videoMagnetModal.show()
238 return this.authService.isLoggedIn()
241 canUserUpdateVideo () {
242 return this.video.isUpdatableBy(this.authService.getUser())
245 isVideoRemovable () {
246 return this.video.isRemovableBy(this.authService.getUser())
249 isVideoBlacklistable () {
250 return this.video.isBlackistableBy(this.authService.getUser())
253 private checkUserRating () {
254 // Unlogged users do not have ratings
255 if (this.isUserLoggedIn() === false) return
257 this.videoService.getUserVideoRating(this.video.id)
261 this.userRating = ratingObject.rating
265 err => this.notificationsService.error('Error', err.text)
269 private onVideoFetched (video: Video) {
273 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
274 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
276 observable = Observable.of(true)
279 observable.subscribe(
282 return this.router.navigate([ '/videos/list' ])
285 this.setOpenGraphTags()
287 this.checkUserRating()
292 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
293 let likesToIncrement = 0
294 let dislikesToIncrement = 0
297 if (oldRating === 'like') likesToIncrement--
298 if (oldRating === 'dislike') dislikesToIncrement--
301 if (newRating === 'like') likesToIncrement++
302 if (newRating === 'dislike') dislikesToIncrement++
304 this.video.likes += likesToIncrement
305 this.video.dislikes += dislikesToIncrement
308 private loadTooLong () {
310 console.error('The video load seems to be abnormally long.')
313 private setOpenGraphTags () {
314 this.metaService.setTitle(this.video.name)
316 this.metaService.setTag('og:type', 'video')
318 this.metaService.setTag('og:title', this.video.name)
319 this.metaService.setTag('name', this.video.name)
321 this.metaService.setTag('og:description', this.video.description)
322 this.metaService.setTag('description', this.video.description)
324 this.metaService.setTag('og:image', this.video.thumbnailPath)
326 this.metaService.setTag('og:duration', this.video.duration.toString())
328 this.metaService.setTag('og:site_name', 'PeerTube')
330 this.metaService.setTag('og:url', window.location.href)
331 this.metaService.setTag('url', window.location.href)
334 private runInProgress (torrent: any) {
335 // Refresh each second
336 this.torrentInfosInterval = window.setInterval(() => {
337 this.ngZone.run(() => {
338 this.downloadSpeed = torrent.downloadSpeed
339 this.numPeers = torrent.numPeers
340 this.uploadSpeed = torrent.uploadSpeed