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
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 this.completeDescriptionShown = true
164 if (this.completeVideoDescription === undefined) {
165 return this.loadCompleteDescription()
168 this.updateVideoDescription(this.completeVideoDescription)
171 showLessDescription () {
172 this.completeDescriptionShown = false
174 this.updateVideoDescription(this.shortVideoDescription)
177 loadCompleteDescription () {
178 this.videoService.loadCompleteDescription(this.video.descriptionPath)
181 this.shortVideoDescription = this.video.description
182 this.completeVideoDescription = description
184 this.updateVideoDescription(this.completeVideoDescription)
187 error => this.notificationsService.error('Error', error.text)
191 showReportModal (event: Event) {
192 event.preventDefault()
193 this.videoReportModal.show()
197 this.videoShareModal.show()
200 showDownloadModal (event: Event) {
201 event.preventDefault()
202 this.videoDownloadModal.show()
206 return this.authService.isLoggedIn()
209 canUserUpdateVideo () {
210 return this.video.isUpdatableBy(this.authService.getUser())
213 isVideoRemovable () {
214 return this.video.isRemovableBy(this.authService.getUser())
217 isVideoBlacklistable () {
218 return this.video.isBlackistableBy(this.authService.getUser())
221 private updateVideoDescription (description: string) {
222 this.video.description = description
223 this.setVideoDescriptionHTML()
226 private setVideoDescriptionHTML () {
227 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
230 private handleError (err: any) {
231 const errorMessage: string = typeof err === 'string' ? err : err.message
234 if (errorMessage.indexOf('http error') !== -1) {
235 message = 'Cannot fetch video from server, maybe down.'
237 message = errorMessage
240 this.notificationsService.error('Error', message)
243 private checkUserRating () {
244 // Unlogged users do not have ratings
245 if (this.isUserLoggedIn() === false) return
247 this.videoService.getUserVideoRating(this.video.id)
251 this.userRating = ratingObject.rating
255 err => this.notificationsService.error('Error', err.message)
259 private onVideoFetched (video: VideoDetails) {
263 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
264 observable = this.confirmService.confirm(
265 'This video contains mature or explicit content. Are you sure you want to watch it?',
266 'Mature or explicit content'
269 observable = Observable.of(true)
272 observable.subscribe(
276 return this.router.navigate([ '/videos/list' ])
279 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
281 const videojsOptions = {
286 videoFiles: this.video.files,
287 playerElement: this.playerElement,
294 this.videoPlayerLoaded = true
297 videojs(this.playerElement, videojsOptions, function () {
299 this.on('customError', (event, data) => {
300 self.handleError(data.err)
303 this.on('torrentInfo', (event, data) => {
304 self.downloadSpeed = data.downloadSpeed
305 self.numPeers = data.numPeers
306 self.uploadSpeed = data.uploadSpeed
310 this.setVideoDescriptionHTML()
312 this.setOpenGraphTags()
313 this.checkUserRating()
318 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
319 let likesToIncrement = 0
320 let dislikesToIncrement = 0
323 if (oldRating === 'like') likesToIncrement--
324 if (oldRating === 'dislike') dislikesToIncrement--
327 if (newRating === 'like') likesToIncrement++
328 if (newRating === 'dislike') dislikesToIncrement++
330 this.video.likes += likesToIncrement
331 this.video.dislikes += dislikesToIncrement
334 private setOpenGraphTags () {
335 this.metaService.setTitle(this.video.name)
337 this.metaService.setTag('og:type', 'video')
339 this.metaService.setTag('og:title', this.video.name)
340 this.metaService.setTag('name', this.video.name)
342 this.metaService.setTag('og:description', this.video.description)
343 this.metaService.setTag('description', this.video.description)
345 this.metaService.setTag('og:image', this.video.previewPath)
347 this.metaService.setTag('og:duration', this.video.duration.toString())
349 this.metaService.setTag('og:site_name', 'PeerTube')
351 this.metaService.setTag('og:url', window.location.href)
352 this.metaService.setTag('url', window.location.href)