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 '../../../assets/player/peertube-videojs-plugin'
9 import { MetaService } from '@ngx-meta/core'
10 import { NotificationsService } from 'angular2-notifications'
12 import { AuthService, ConfirmService } from '../../core'
13 import { VideoMagnetComponent } from './video-magnet.component'
14 import { VideoShareComponent } from './video-share.component'
15 import { VideoReportComponent } from './video-report.component'
16 import { Video, VideoService } from '../shared'
17 import { WebTorrentService } from './webtorrent.service'
18 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
21 selector: 'my-video-watch',
22 templateUrl: './video-watch.component.html',
23 styleUrls: [ './video-watch.component.scss' ]
25 export class VideoWatchComponent implements OnInit, OnDestroy {
26 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent
27 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
28 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
34 player: videojs.Player
35 playerElement: HTMLMediaElement
37 userRating: UserVideoRateType = null
41 private paramsSub: Subscription
44 private elementRef: ElementRef,
45 private route: ActivatedRoute,
46 private router: Router,
47 private videoService: VideoService,
48 private confirmService: ConfirmService,
49 private metaService: MetaService,
50 private authService: AuthService,
51 private notificationsService: NotificationsService
55 this.paramsSub = this.route.params.subscribe(routeParams => {
56 let uuid = routeParams['uuid']
57 this.videoService.getVideo(uuid).subscribe(
58 video => this.onVideoFetched(video),
62 this.videoNotFound = true
69 // Remove player if it exists
70 if (this.videoNotFound === false) {
71 videojs(this.playerElement).dispose()
74 // Unsubscribe subscriptions
75 this.paramsSub.unsubscribe()
79 if (this.isUserLoggedIn() === false) return
80 // Already liked this video
81 if (this.userRating === 'like') return
83 this.videoService.setVideoLike(this.video.id)
86 // Update the video like attribute
87 this.updateVideoRating(this.userRating, 'like')
88 this.userRating = 'like'
91 err => this.notificationsService.error('Error', err.message)
96 if (this.isUserLoggedIn() === false) return
97 // Already disliked this video
98 if (this.userRating === 'dislike') return
100 this.videoService.setVideoDislike(this.video.id)
103 // Update the video dislike attribute
104 this.updateVideoRating(this.userRating, 'dislike')
105 this.userRating = 'dislike'
108 err => this.notificationsService.error('Error', err.message)
112 removeVideo (event: Event) {
113 event.preventDefault()
115 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
117 if (res === false) return
119 this.videoService.removeVideo(this.video.id)
122 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
123 // Go back to the video-list.
124 this.router.navigate(['/videos/list'])
127 error => this.notificationsService.error('Error', error.text)
133 blacklistVideo (event: Event) {
134 event.preventDefault()
136 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
138 if (res === false) return
140 this.videoService.blacklistVideo(this.video.id)
143 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
144 this.router.navigate(['/videos/list'])
147 error => this.notificationsService.error('Error', error.text)
153 showReportModal (event: Event) {
154 event.preventDefault()
155 this.videoReportModal.show()
159 this.videoShareModal.show()
162 showMagnetUriModal (event: Event) {
163 event.preventDefault()
164 this.videoMagnetModal.show()
168 return this.authService.isLoggedIn()
171 canUserUpdateVideo () {
172 return this.video.isUpdatableBy(this.authService.getUser())
175 isVideoRemovable () {
176 return this.video.isRemovableBy(this.authService.getUser())
179 isVideoBlacklistable () {
180 return this.video.isBlackistableBy(this.authService.getUser())
183 private handleError (err: any) {
184 const errorMessage: string = typeof err === 'string' ? err : err.message
187 if (errorMessage.indexOf('http error') !== -1) {
188 message = 'Cannot fetch video from server, maybe down.'
190 message = errorMessage
193 this.notificationsService.error('Error', message)
196 private checkUserRating () {
197 // Unlogged users do not have ratings
198 if (this.isUserLoggedIn() === false) return
200 this.videoService.getUserVideoRating(this.video.id)
204 this.userRating = ratingObject.rating
208 err => this.notificationsService.error('Error', err.message)
212 private onVideoFetched (video: Video) {
216 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
217 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
219 observable = Observable.of(true)
222 observable.subscribe(
225 return this.router.navigate([ '/videos/list' ])
228 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
230 const videojsOptions = {
235 videoFiles: this.video.files,
236 playerElement: this.playerElement,
244 videojs(this.playerElement, videojsOptions, function () {
246 this.on('customError', (event, data) => {
247 self.handleError(data.err)
250 this.on('torrentInfo', (event, data) => {
251 self.downloadSpeed = data.downloadSpeed
252 self.numPeers = data.numPeers
253 self.uploadSpeed = data.uploadSpeed
257 this.setOpenGraphTags()
258 this.checkUserRating()
263 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
264 let likesToIncrement = 0
265 let dislikesToIncrement = 0
268 if (oldRating === 'like') likesToIncrement--
269 if (oldRating === 'dislike') dislikesToIncrement--
272 if (newRating === 'like') likesToIncrement++
273 if (newRating === 'dislike') dislikesToIncrement++
275 this.video.likes += likesToIncrement
276 this.video.dislikes += dislikesToIncrement
279 private setOpenGraphTags () {
280 this.metaService.setTitle(this.video.name)
282 this.metaService.setTag('og:type', 'video')
284 this.metaService.setTag('og:title', this.video.name)
285 this.metaService.setTag('name', this.video.name)
287 this.metaService.setTag('og:description', this.video.description)
288 this.metaService.setTag('description', this.video.description)
290 this.metaService.setTag('og:image', this.video.previewPath)
292 this.metaService.setTag('og:duration', this.video.duration.toString())
294 this.metaService.setTag('og:site_name', 'PeerTube')
296 this.metaService.setTag('og:url', window.location.href)
297 this.metaService.setTag('url', window.location.href)