1 import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { MetaService } from '@ngx-meta/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { VideoService } from 'app/shared/video/video.service'
6 import { Observable } from 'rxjs/Observable'
7 import { Subscription } from 'rxjs/Subscription'
8 import videojs from 'video.js'
9 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
10 import '../../../assets/player/peertube-videojs-plugin'
11 import { AuthService, ConfirmService } from '../../core'
12 import { VideoBlacklistService } from '../../shared'
13 import { MarkdownService } from '../shared'
14 import { VideoDownloadComponent } from './video-download.component'
15 import { VideoReportComponent } from './video-report.component'
16 import { VideoShareComponent } from './video-share.component'
17 import { VideoDetails } from '../../shared/video/video-details.model'
20 selector: 'my-video-watch',
21 templateUrl: './video-watch.component.html',
22 styleUrls: [ './video-watch.component.scss' ]
24 export class VideoWatchComponent implements OnInit, OnDestroy {
25 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
26 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
27 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
33 player: videojs.Player
34 playerElement: HTMLMediaElement
36 userRating: UserVideoRateType = null
37 video: VideoDetails = null
38 videoPlayerLoaded = false
40 descriptionLoading = false
42 completeDescriptionShown = false
43 completeVideoDescription: string
44 shortVideoDescription: string
45 videoHTMLDescription = ''
47 private paramsSub: Subscription
50 private elementRef: ElementRef,
51 private route: ActivatedRoute,
52 private router: Router,
53 private videoService: VideoService,
54 private videoBlacklistService: VideoBlacklistService,
55 private confirmService: ConfirmService,
56 private metaService: MetaService,
57 private authService: AuthService,
58 private notificationsService: NotificationsService,
59 private markdownService: MarkdownService
63 this.paramsSub = this.route.params.subscribe(routeParams => {
64 let uuid = routeParams['uuid']
65 this.videoService.getVideo(uuid).subscribe(
66 video => this.onVideoFetched(video),
69 this.videoNotFound = true
77 // Remove player if it exists
78 if (this.videoPlayerLoaded === true) {
79 videojs(this.playerElement).dispose()
82 // Unsubscribe subscriptions
83 this.paramsSub.unsubscribe()
87 if (this.isUserLoggedIn() === false) return
88 // Already liked this video
89 if (this.userRating === 'like') return
91 this.videoService.setVideoLike(this.video.id)
94 // Update the video like attribute
95 this.updateVideoRating(this.userRating, 'like')
96 this.userRating = 'like'
99 err => this.notificationsService.error('Error', err.message)
104 if (this.isUserLoggedIn() === false) return
105 // Already disliked this video
106 if (this.userRating === 'dislike') return
108 this.videoService.setVideoDislike(this.video.id)
111 // Update the video dislike attribute
112 this.updateVideoRating(this.userRating, 'dislike')
113 this.userRating = 'dislike'
116 err => this.notificationsService.error('Error', err.message)
120 removeVideo (event: Event) {
121 event.preventDefault()
123 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
125 if (res === false) return
127 this.videoService.removeVideo(this.video.id)
130 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
131 // Go back to the video-list.
132 this.router.navigate(['/videos/list'])
135 error => this.notificationsService.error('Error', error.text)
141 blacklistVideo (event: Event) {
142 event.preventDefault()
144 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
146 if (res === false) return
148 this.videoBlacklistService.blacklistVideo(this.video.id)
151 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
152 this.router.navigate(['/videos/list'])
155 error => this.notificationsService.error('Error', error.text)
161 showMoreDescription () {
162 if (this.completeVideoDescription === undefined) {
163 return this.loadCompleteDescription()
166 this.updateVideoDescription(this.completeVideoDescription)
167 this.completeDescriptionShown = true
170 showLessDescription () {
172 this.updateVideoDescription(this.shortVideoDescription)
173 this.completeDescriptionShown = false
176 loadCompleteDescription () {
177 this.descriptionLoading = true
179 this.videoService.loadCompleteDescription(this.video.descriptionPath)
182 this.completeDescriptionShown = true
183 this.descriptionLoading = false
185 this.shortVideoDescription = this.video.description
186 this.completeVideoDescription = description
188 this.updateVideoDescription(this.completeVideoDescription)
192 this.descriptionLoading = false
193 this.notificationsService.error('Error', error.text)
198 showReportModal (event: Event) {
199 event.preventDefault()
200 this.videoReportModal.show()
204 this.videoShareModal.show()
207 showDownloadModal (event: Event) {
208 event.preventDefault()
209 this.videoDownloadModal.show()
213 return this.authService.isLoggedIn()
216 canUserUpdateVideo () {
217 return this.video.isUpdatableBy(this.authService.getUser())
220 isVideoRemovable () {
221 return this.video.isRemovableBy(this.authService.getUser())
224 isVideoBlacklistable () {
225 return this.video.isBlackistableBy(this.authService.getUser())
228 private updateVideoDescription (description: string) {
229 this.video.description = description
230 this.setVideoDescriptionHTML()
233 private setVideoDescriptionHTML () {
234 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
237 private handleError (err: any) {
238 const errorMessage: string = typeof err === 'string' ? err : err.message
241 if (errorMessage.indexOf('http error') !== -1) {
242 message = 'Cannot fetch video from server, maybe down.'
244 message = errorMessage
247 this.notificationsService.error('Error', message)
250 private checkUserRating () {
251 // Unlogged users do not have ratings
252 if (this.isUserLoggedIn() === false) return
254 this.videoService.getUserVideoRating(this.video.id)
258 this.userRating = ratingObject.rating
262 err => this.notificationsService.error('Error', err.message)
266 private onVideoFetched (video: VideoDetails) {
270 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
271 observable = this.confirmService.confirm(
272 'This video contains mature or explicit content. Are you sure you want to watch it?',
273 'Mature or explicit content'
276 observable = Observable.of(true)
279 observable.subscribe(
283 return this.router.navigate([ '/videos/list' ])
286 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
288 const videojsOptions = {
293 videoFiles: this.video.files,
294 playerElement: this.playerElement,
301 this.videoPlayerLoaded = true
304 videojs(this.playerElement, videojsOptions, function () {
306 this.on('customError', (event, data) => {
307 self.handleError(data.err)
310 this.on('torrentInfo', (event, data) => {
311 self.downloadSpeed = data.downloadSpeed
312 self.numPeers = data.numPeers
313 self.uploadSpeed = data.uploadSpeed
317 this.setVideoDescriptionHTML()
319 this.setOpenGraphTags()
320 this.checkUserRating()
322 this.prepareViewAdd()
327 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
328 let likesToIncrement = 0
329 let dislikesToIncrement = 0
332 if (oldRating === 'like') likesToIncrement--
333 if (oldRating === 'dislike') dislikesToIncrement--
336 if (newRating === 'like') likesToIncrement++
337 if (newRating === 'dislike') dislikesToIncrement++
339 this.video.likes += likesToIncrement
340 this.video.dislikes += dislikesToIncrement
343 private setOpenGraphTags () {
344 this.metaService.setTitle(this.video.name)
346 this.metaService.setTag('og:type', 'video')
348 this.metaService.setTag('og:title', this.video.name)
349 this.metaService.setTag('name', this.video.name)
351 this.metaService.setTag('og:description', this.video.description)
352 this.metaService.setTag('description', this.video.description)
354 this.metaService.setTag('og:image', this.video.previewPath)
356 this.metaService.setTag('og:duration', this.video.duration.toString())
358 this.metaService.setTag('og:site_name', 'PeerTube')
360 this.metaService.setTag('og:url', window.location.href)
361 this.metaService.setTag('url', window.location.href)
364 private prepareViewAdd () {
365 // After 30 seconds (or 3/4 of the video), increment add a view
366 let viewTimeoutSeconds = 30
367 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
371 .viewVideo(this.video.uuid)
374 }, viewTimeoutSeconds * 1000)