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
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),
63 this.videoNotFound = true
70 // Remove player if it exists
71 if (this.videoNotFound === false) {
72 videojs(this.playerElement).dispose()
75 // Unsubscribe subscriptions
76 this.paramsSub.unsubscribe()
80 if (this.isUserLoggedIn() === false) return
81 // Already liked this video
82 if (this.userRating === 'like') return
84 this.videoService.setVideoLike(this.video.id)
87 // Update the video like attribute
88 this.updateVideoRating(this.userRating, 'like')
89 this.userRating = 'like'
92 err => this.notificationsService.error('Error', err.message)
97 if (this.isUserLoggedIn() === false) return
98 // Already disliked this video
99 if (this.userRating === 'dislike') return
101 this.videoService.setVideoDislike(this.video.id)
104 // Update the video dislike attribute
105 this.updateVideoRating(this.userRating, 'dislike')
106 this.userRating = 'dislike'
109 err => this.notificationsService.error('Error', err.message)
113 removeVideo (event: Event) {
114 event.preventDefault()
116 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
118 if (res === false) return
120 this.videoService.removeVideo(this.video.id)
123 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
124 // Go back to the video-list.
125 this.router.navigate(['/videos/list'])
128 error => this.notificationsService.error('Error', error.text)
134 blacklistVideo (event: Event) {
135 event.preventDefault()
137 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
139 if (res === false) return
141 this.videoBlacklistService.blacklistVideo(this.video.id)
144 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
145 this.router.navigate(['/videos/list'])
148 error => this.notificationsService.error('Error', error.text)
154 showReportModal (event: Event) {
155 event.preventDefault()
156 this.videoReportModal.show()
160 this.videoShareModal.show()
163 showDownloadModal (event: Event) {
164 event.preventDefault()
165 this.videoDownloadModal.show()
169 return this.authService.isLoggedIn()
172 canUserUpdateVideo () {
173 return this.video.isUpdatableBy(this.authService.getUser())
176 isVideoRemovable () {
177 return this.video.isRemovableBy(this.authService.getUser())
180 isVideoBlacklistable () {
181 return this.video.isBlackistableBy(this.authService.getUser())
184 private handleError (err: any) {
185 const errorMessage: string = typeof err === 'string' ? err : err.message
188 if (errorMessage.indexOf('http error') !== -1) {
189 message = 'Cannot fetch video from server, maybe down.'
191 message = errorMessage
194 this.notificationsService.error('Error', message)
197 private checkUserRating () {
198 // Unlogged users do not have ratings
199 if (this.isUserLoggedIn() === false) return
201 this.videoService.getUserVideoRating(this.video.id)
205 this.userRating = ratingObject.rating
209 err => this.notificationsService.error('Error', err.message)
213 private onVideoFetched (video: Video) {
217 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
218 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
220 observable = Observable.of(true)
223 observable.subscribe(
226 return this.router.navigate([ '/videos/list' ])
229 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
231 const videojsOptions = {
236 videoFiles: this.video.files,
237 playerElement: this.playerElement,
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)