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 { VideoDetails, VideoService, MarkdownService } 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
38 video: VideoDetails = null
39 videoPlayerLoaded = false
41 videoHTMLDescription = ''
43 private paramsSub: Subscription
46 private elementRef: ElementRef,
47 private route: ActivatedRoute,
48 private router: Router,
49 private videoService: VideoService,
50 private videoBlacklistService: VideoBlacklistService,
51 private confirmService: ConfirmService,
52 private metaService: MetaService,
53 private authService: AuthService,
54 private notificationsService: NotificationsService,
55 private markdownService: MarkdownService
59 this.paramsSub = this.route.params.subscribe(routeParams => {
60 let uuid = routeParams['uuid']
61 this.videoService.getVideo(uuid).subscribe(
62 video => this.onVideoFetched(video),
65 this.videoNotFound = true
73 // Remove player if it exists
74 if (this.videoPlayerLoaded === true) {
75 videojs(this.playerElement).dispose()
78 // Unsubscribe subscriptions
79 this.paramsSub.unsubscribe()
83 if (this.isUserLoggedIn() === false) return
84 // Already liked this video
85 if (this.userRating === 'like') return
87 this.videoService.setVideoLike(this.video.id)
90 // Update the video like attribute
91 this.updateVideoRating(this.userRating, 'like')
92 this.userRating = 'like'
95 err => this.notificationsService.error('Error', err.message)
100 if (this.isUserLoggedIn() === false) return
101 // Already disliked this video
102 if (this.userRating === 'dislike') return
104 this.videoService.setVideoDislike(this.video.id)
107 // Update the video dislike attribute
108 this.updateVideoRating(this.userRating, 'dislike')
109 this.userRating = 'dislike'
112 err => this.notificationsService.error('Error', err.message)
116 removeVideo (event: Event) {
117 event.preventDefault()
119 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
121 if (res === false) return
123 this.videoService.removeVideo(this.video.id)
126 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
127 // Go back to the video-list.
128 this.router.navigate(['/videos/list'])
131 error => this.notificationsService.error('Error', error.text)
137 blacklistVideo (event: Event) {
138 event.preventDefault()
140 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
142 if (res === false) return
144 this.videoBlacklistService.blacklistVideo(this.video.id)
147 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
148 this.router.navigate(['/videos/list'])
151 error => this.notificationsService.error('Error', error.text)
157 showReportModal (event: Event) {
158 event.preventDefault()
159 this.videoReportModal.show()
163 this.videoShareModal.show()
166 showDownloadModal (event: Event) {
167 event.preventDefault()
168 this.videoDownloadModal.show()
172 return this.authService.isLoggedIn()
175 canUserUpdateVideo () {
176 return this.video.isUpdatableBy(this.authService.getUser())
179 isVideoRemovable () {
180 return this.video.isRemovableBy(this.authService.getUser())
183 isVideoBlacklistable () {
184 return this.video.isBlackistableBy(this.authService.getUser())
187 private handleError (err: any) {
188 const errorMessage: string = typeof err === 'string' ? err : err.message
191 if (errorMessage.indexOf('http error') !== -1) {
192 message = 'Cannot fetch video from server, maybe down.'
194 message = errorMessage
197 this.notificationsService.error('Error', message)
200 private checkUserRating () {
201 // Unlogged users do not have ratings
202 if (this.isUserLoggedIn() === false) return
204 this.videoService.getUserVideoRating(this.video.id)
208 this.userRating = ratingObject.rating
212 err => this.notificationsService.error('Error', err.message)
216 private onVideoFetched (video: VideoDetails) {
220 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
221 observable = this.confirmService.confirm(
222 'This video contains mature or explicit content. Are you sure you want to watch it?',
223 'Mature or explicit content'
226 observable = Observable.of(true)
229 observable.subscribe(
233 return this.router.navigate([ '/videos/list' ])
236 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
238 const videojsOptions = {
243 videoFiles: this.video.files,
244 playerElement: this.playerElement,
251 this.videoPlayerLoaded = true
254 videojs(this.playerElement, videojsOptions, function () {
256 this.on('customError', (event, data) => {
257 self.handleError(data.err)
260 this.on('torrentInfo', (event, data) => {
261 self.downloadSpeed = data.downloadSpeed
262 self.numPeers = data.numPeers
263 self.uploadSpeed = data.uploadSpeed
267 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
269 this.setOpenGraphTags()
270 this.checkUserRating()
275 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
276 let likesToIncrement = 0
277 let dislikesToIncrement = 0
280 if (oldRating === 'like') likesToIncrement--
281 if (oldRating === 'dislike') dislikesToIncrement--
284 if (newRating === 'like') likesToIncrement++
285 if (newRating === 'dislike') dislikesToIncrement++
287 this.video.likes += likesToIncrement
288 this.video.dislikes += dislikesToIncrement
291 private setOpenGraphTags () {
292 this.metaService.setTitle(this.video.name)
294 this.metaService.setTag('og:type', 'video')
296 this.metaService.setTag('og:title', this.video.name)
297 this.metaService.setTag('name', this.video.name)
299 this.metaService.setTag('og:description', this.video.description)
300 this.metaService.setTag('description', this.video.description)
302 this.metaService.setTag('og:image', this.video.previewPath)
304 this.metaService.setTag('og:duration', this.video.duration.toString())
306 this.metaService.setTag('og:site_name', 'PeerTube')
308 this.metaService.setTag('og:url', window.location.href)
309 this.metaService.setTag('url', window.location.href)