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 * as videojs from 'video.js'
7 import { MetaService } from '@nglibs/meta'
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, { autoplay: true }, (err) => {
132 this.notificationsService.error('Error', 'Cannot append the file in the video element.')
137 this.runInProgress(torrent)
142 if (this.isUserLoggedIn() === false) return
143 // Already liked this video
144 if (this.userRating === 'like') return
146 this.videoService.setVideoLike(this.video.id)
149 // Update the video like attribute
150 this.updateVideoRating(this.userRating, 'like')
151 this.userRating = 'like'
154 err => this.notificationsService.error('Error', err.text)
159 if (this.isUserLoggedIn() === false) return
160 // Already disliked this video
161 if (this.userRating === 'dislike') return
163 this.videoService.setVideoDislike(this.video.id)
166 // Update the video dislike attribute
167 this.updateVideoRating(this.userRating, 'dislike')
168 this.userRating = 'dislike'
171 err => this.notificationsService.error('Error', err.text)
175 removeVideo (event: Event) {
176 event.preventDefault()
178 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
180 if (res === false) return
182 this.videoService.removeVideo(this.video.id)
185 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
186 // Go back to the video-list.
187 this.router.navigate(['/videos/list'])
190 error => this.notificationsService.error('Error', error.text)
196 blacklistVideo (event: Event) {
197 event.preventDefault()
199 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
201 if (res === false) return
203 this.videoService.blacklistVideo(this.video.id)
206 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
207 this.router.navigate(['/videos/list'])
210 error => this.notificationsService.error('Error', error.text)
216 showReportModal (event: Event) {
217 event.preventDefault()
218 this.videoReportModal.show()
222 this.videoShareModal.show()
225 showMagnetUriModal (event: Event) {
226 event.preventDefault()
227 this.videoMagnetModal.show()
231 return this.authService.isLoggedIn()
234 canUserUpdateVideo () {
235 return this.video.isUpdatableBy(this.authService.getUser())
238 isVideoRemovable () {
239 return this.video.isRemovableBy(this.authService.getUser())
242 isVideoBlacklistable () {
243 return this.video.isBlackistableBy(this.authService.getUser())
246 private checkUserRating () {
247 // Unlogged users do not have ratings
248 if (this.isUserLoggedIn() === false) return
250 this.videoService.getUserVideoRating(this.video.id)
252 (ratingObject: UserVideoRate) => {
254 this.userRating = ratingObject.rating
258 err => this.notificationsService.error('Error', err.text)
262 private onVideoFetched (video: Video) {
266 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
267 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
269 observable = Observable.of(true)
272 observable.subscribe(
275 return this.router.navigate([ '/videos/list' ])
278 this.setOpenGraphTags()
280 this.checkUserRating()
285 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
286 let likesToIncrement = 0
287 let dislikesToIncrement = 0
290 if (oldRating === 'like') likesToIncrement--
291 if (oldRating === 'dislike') dislikesToIncrement--
294 if (newRating === 'like') likesToIncrement++
295 if (newRating === 'dislike') dislikesToIncrement++
297 this.video.likes += likesToIncrement
298 this.video.dislikes += dislikesToIncrement
301 private loadTooLong () {
303 console.error('The video load seems to be abnormally long.')
306 private setOpenGraphTags () {
307 this.metaService.setTitle(this.video.name)
309 this.metaService.setTag('og:type', 'video')
311 this.metaService.setTag('og:title', this.video.name)
312 this.metaService.setTag('name', this.video.name)
314 this.metaService.setTag('og:description', this.video.description)
315 this.metaService.setTag('description', this.video.description)
317 this.metaService.setTag('og:image', this.video.thumbnailPath)
319 this.metaService.setTag('og:duration', this.video.duration.toString())
321 this.metaService.setTag('og:site_name', 'PeerTube')
323 this.metaService.setTag('og:url', window.location.href)
324 this.metaService.setTag('url', window.location.href)
327 private runInProgress (torrent: any) {
328 // Refresh each second
329 this.torrentInfosInterval = window.setInterval(() => {
330 this.ngZone.run(() => {
331 this.downloadSpeed = torrent.downloadSpeed
332 this.numPeers = torrent.numPeers
333 this.uploadSpeed = torrent.uploadSpeed