1 import { Component, ElementRef, 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 { VideoDownloadComponent } from './video-download.component'
14 import { VideoShareComponent } from './video-share.component'
15 import { VideoReportComponent } from './video-report.component'
16 import { Video, VideoService } from '../shared'
17 import { VideoBlacklistService } from '../../shared'
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('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
27 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
28 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
34 player: videojs.Player
35 playerElement: HTMLMediaElement
37 userRating: UserVideoRateType = null
39 videoPlayerLoaded = false
41 private paramsSub: Subscription
44 private elementRef: ElementRef,
45 private route: ActivatedRoute,
46 private router: Router,
47 private videoService: VideoService,
48 private videoBlacklistService: VideoBlacklistService,
49 private confirmService: ConfirmService,
50 private metaService: MetaService,
51 private authService: AuthService,
52 private notificationsService: NotificationsService
56 this.paramsSub = this.route.params.subscribe(routeParams => {
57 let uuid = routeParams['uuid']
58 this.videoService.getVideo(uuid).subscribe(
59 video => this.onVideoFetched(video),
61 error => console.error(error)
67 // Remove player if it exists
68 if (this.videoPlayerLoaded === true) {
69 videojs(this.playerElement).dispose()
72 // Unsubscribe subscriptions
73 this.paramsSub.unsubscribe()
77 if (this.isUserLoggedIn() === false) return
78 // Already liked this video
79 if (this.userRating === 'like') return
81 this.videoService.setVideoLike(this.video.id)
84 // Update the video like attribute
85 this.updateVideoRating(this.userRating, 'like')
86 this.userRating = 'like'
89 err => this.notificationsService.error('Error', err.message)
94 if (this.isUserLoggedIn() === false) return
95 // Already disliked this video
96 if (this.userRating === 'dislike') return
98 this.videoService.setVideoDislike(this.video.id)
101 // Update the video dislike attribute
102 this.updateVideoRating(this.userRating, 'dislike')
103 this.userRating = 'dislike'
106 err => this.notificationsService.error('Error', err.message)
110 removeVideo (event: Event) {
111 event.preventDefault()
113 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
115 if (res === false) return
117 this.videoService.removeVideo(this.video.id)
120 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
121 // Go back to the video-list.
122 this.router.navigate(['/videos/list'])
125 error => this.notificationsService.error('Error', error.text)
131 blacklistVideo (event: Event) {
132 event.preventDefault()
134 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
136 if (res === false) return
138 this.videoBlacklistService.blacklistVideo(this.video.id)
141 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
142 this.router.navigate(['/videos/list'])
145 error => this.notificationsService.error('Error', error.text)
151 showReportModal (event: Event) {
152 event.preventDefault()
153 this.videoReportModal.show()
157 this.videoShareModal.show()
160 showDownloadModal (event: Event) {
161 event.preventDefault()
162 this.videoDownloadModal.show()
166 return this.authService.isLoggedIn()
169 canUserUpdateVideo () {
170 return this.video.isUpdatableBy(this.authService.getUser())
173 isVideoRemovable () {
174 return this.video.isRemovableBy(this.authService.getUser())
177 isVideoBlacklistable () {
178 return this.video.isBlackistableBy(this.authService.getUser())
181 private handleError (err: any) {
182 const errorMessage: string = typeof err === 'string' ? err : err.message
185 if (errorMessage.indexOf('http error') !== -1) {
186 message = 'Cannot fetch video from server, maybe down.'
188 message = errorMessage
191 this.notificationsService.error('Error', message)
194 private checkUserRating () {
195 // Unlogged users do not have ratings
196 if (this.isUserLoggedIn() === false) return
198 this.videoService.getUserVideoRating(this.video.id)
202 this.userRating = ratingObject.rating
206 err => this.notificationsService.error('Error', err.message)
210 private onVideoFetched (video: Video) {
214 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
215 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
217 observable = Observable.of(true)
220 observable.subscribe(
224 return this.router.navigate([ '/videos/list' ])
227 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
229 const videojsOptions = {
234 videoFiles: this.video.files,
235 playerElement: this.playerElement,
242 this.videoPlayerLoaded = true
245 videojs(this.playerElement, videojsOptions, function () {
247 this.on('customError', (event, data) => {
248 self.handleError(data.err)
251 this.on('torrentInfo', (event, data) => {
252 self.downloadSpeed = data.downloadSpeed
253 self.numPeers = data.numPeers
254 self.uploadSpeed = data.uploadSpeed
258 this.setOpenGraphTags()
259 this.checkUserRating()
264 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
265 let likesToIncrement = 0
266 let dislikesToIncrement = 0
269 if (oldRating === 'like') likesToIncrement--
270 if (oldRating === 'dislike') dislikesToIncrement--
273 if (newRating === 'like') likesToIncrement++
274 if (newRating === 'dislike') dislikesToIncrement++
276 this.video.likes += likesToIncrement
277 this.video.dislikes += dislikesToIncrement
280 private setOpenGraphTags () {
281 this.metaService.setTitle(this.video.name)
283 this.metaService.setTag('og:type', 'video')
285 this.metaService.setTag('og:title', this.video.name)
286 this.metaService.setTag('name', this.video.name)
288 this.metaService.setTag('og:description', this.video.description)
289 this.metaService.setTag('description', this.video.description)
291 this.metaService.setTag('og:image', this.video.previewPath)
293 this.metaService.setTag('og:duration', this.video.duration.toString())
295 this.metaService.setTag('og:site_name', 'PeerTube')
297 this.metaService.setTag('og:url', window.location.href)
298 this.metaService.setTag('url', window.location.href)