1 import { Component, ElementRef, NgZone, 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 { Observable } from 'rxjs/Observable'
6 import { Subscription } from 'rxjs/Subscription'
7 import * as videojs from 'video.js'
8 import 'videojs-hotkeys'
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 { Account } from '../../shared/account/account.model'
14 import { VideoDetails } from '../../shared/video/video-details.model'
15 import { Video } from '../../shared/video/video.model'
16 import { VideoService } from '../../shared/video/video.service'
17 import { MarkdownService } from '../shared'
18 import { VideoDownloadComponent } from './modal/video-download.component'
19 import { VideoReportComponent } from './modal/video-report.component'
20 import { VideoShareComponent } from './modal/video-share.component'
23 selector: 'my-video-watch',
24 templateUrl: './video-watch.component.html',
25 styleUrls: [ './video-watch.component.scss' ]
27 export class VideoWatchComponent implements OnInit, OnDestroy {
28 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
29 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
30 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
32 otherVideosDisplayed: Video[] = []
35 player: videojs.Player
36 playerElement: HTMLVideoElement
37 userRating: UserVideoRateType = null
38 video: VideoDetails = null
39 videoPlayerLoaded = false
41 descriptionLoading = false
43 completeDescriptionShown = false
44 completeVideoDescription: string
45 shortVideoDescription: string
46 videoHTMLDescription = ''
47 likesBarTooltipText = ''
49 private otherVideos: Video[] = []
50 private paramsSub: Subscription
53 private elementRef: ElementRef,
54 private route: ActivatedRoute,
55 private router: Router,
56 private videoService: VideoService,
57 private videoBlacklistService: VideoBlacklistService,
58 private confirmService: ConfirmService,
59 private metaService: MetaService,
60 private authService: AuthService,
61 private notificationsService: NotificationsService,
62 private markdownService: MarkdownService,
67 return this.authService.getUser()
71 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
74 this.otherVideos = data.videos
75 this.updateOtherVideosDisplayed()
78 err => console.error(err)
81 this.paramsSub = this.route.params.subscribe(routeParams => {
82 if (this.videoPlayerLoaded) {
86 const uuid = routeParams['uuid']
87 // Video did not changed
88 if (this.video && this.video.uuid === uuid) return
90 this.videoService.getVideo(uuid).subscribe(
91 video => this.onVideoFetched(video),
94 this.videoNotFound = true
102 // Remove player if it exists
103 if (this.videoPlayerLoaded === true) {
104 videojs(this.playerElement).dispose()
107 // Unsubscribe subscriptions
108 this.paramsSub.unsubscribe()
112 if (this.isUserLoggedIn() === false) return
113 if (this.userRating === 'like') {
114 // Already liked this video
115 this.setRating('none')
117 this.setRating('like')
122 if (this.isUserLoggedIn() === false) return
123 if (this.userRating === 'dislike') {
124 // Already disliked this video
125 this.setRating('none')
127 this.setRating('dislike')
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.message)
151 showMoreDescription () {
152 if (this.completeVideoDescription === undefined) {
153 return this.loadCompleteDescription()
156 this.updateVideoDescription(this.completeVideoDescription)
157 this.completeDescriptionShown = true
160 showLessDescription () {
161 this.updateVideoDescription(this.shortVideoDescription)
162 this.completeDescriptionShown = false
165 loadCompleteDescription () {
166 this.descriptionLoading = true
168 this.videoService.loadCompleteDescription(this.video.descriptionPath)
171 this.completeDescriptionShown = true
172 this.descriptionLoading = false
174 this.shortVideoDescription = this.video.description
175 this.completeVideoDescription = description
177 this.updateVideoDescription(this.completeVideoDescription)
181 this.descriptionLoading = false
182 this.notificationsService.error('Error', error.message)
187 showReportModal (event: Event) {
188 event.preventDefault()
189 this.videoReportModal.show()
193 this.videoShareModal.show()
196 showDownloadModal (event: Event) {
197 event.preventDefault()
198 this.videoDownloadModal.show()
202 return this.authService.isLoggedIn()
205 isVideoUpdatable () {
206 return this.video.isUpdatableBy(this.authService.getUser())
209 isVideoBlacklistable () {
210 return this.video.isBlackistableBy(this.user)
214 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
218 if (!this.video) return ''
220 return this.video.previewUrl
224 if (!this.video || Array.isArray(this.video.tags) === false) return []
226 return this.video.tags.join(', ')
229 isVideoRemovable () {
230 return this.video.isRemovableBy(this.authService.getUser())
233 removeVideo (event: Event) {
234 event.preventDefault()
236 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
239 if (res === false) return
241 this.videoService.removeVideo(this.video.id)
244 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
246 // Go back to the video-list.
247 this.router.navigate([ '/videos/list' ])
250 error => this.notificationsService.error('Error', error.message)
256 private updateVideoDescription (description: string) {
257 this.video.description = description
258 this.setVideoDescriptionHTML()
261 private setVideoDescriptionHTML () {
262 if (!this.video.description) {
263 this.videoHTMLDescription = ''
267 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
270 private setVideoLikesBarTooltipText () {
271 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
274 private handleError (err: any) {
275 const errorMessage: string = typeof err === 'string' ? err : err.message
278 if (errorMessage.indexOf('http error') !== -1) {
279 message = 'Cannot fetch video from server, maybe down.'
281 message = errorMessage
284 this.notificationsService.error('Error', message)
287 private checkUserRating () {
288 // Unlogged users do not have ratings
289 if (this.isUserLoggedIn() === false) return
291 this.videoService.getUserVideoRating(this.video.id)
295 this.userRating = ratingObject.rating
299 err => this.notificationsService.error('Error', err.message)
303 private onVideoFetched (video: VideoDetails) {
306 this.updateOtherVideosDisplayed()
309 if (this.video.isVideoNSFWForUser(this.user)) {
310 observable = this.confirmService.confirm(
311 'This video contains mature or explicit content. Are you sure you want to watch it?',
312 'Mature or explicit content'
315 observable = Observable.of(true)
318 observable.subscribe(
322 return this.router.navigate([ '/videos/list' ])
325 // Player was already loaded
326 if (this.videoPlayerLoaded !== true) {
327 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
329 // If autoplay is true, we don't really need a poster
330 if (this.isAutoplay() === false) {
331 this.playerElement.poster = this.video.previewUrl
334 const videojsOptions = {
336 autoplay: this.isAutoplay(),
339 videoFiles: this.video.files,
340 playerElement: this.playerElement,
342 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
343 videoDuration: this.video.duration
346 enableVolumeScroll: false
351 this.videoPlayerLoaded = true
354 this.zone.runOutsideAngular(() => {
355 videojs(this.playerElement, videojsOptions, function () {
357 this.on('customError', (event, data) => {
358 self.handleError(data.err)
363 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
364 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
367 this.setVideoDescriptionHTML()
368 this.setVideoLikesBarTooltipText()
370 this.setOpenGraphTags()
371 this.checkUserRating()
376 private setRating (nextRating) {
378 switch (nextRating) {
380 method = this.videoService.setVideoLike
383 method = this.videoService.setVideoDislike
386 method = this.videoService.unsetVideoLike
390 method.call(this.videoService, this.video.id)
393 // Update the video like attribute
394 this.updateVideoRating(this.userRating, nextRating)
395 this.userRating = nextRating
397 err => this.notificationsService.error('Error', err.message)
401 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
402 let likesToIncrement = 0
403 let dislikesToIncrement = 0
406 if (oldRating === 'like') likesToIncrement--
407 if (oldRating === 'dislike') dislikesToIncrement--
410 if (newRating === 'like') likesToIncrement++
411 if (newRating === 'dislike') dislikesToIncrement++
413 this.video.likes += likesToIncrement
414 this.video.dislikes += dislikesToIncrement
417 private updateOtherVideosDisplayed () {
418 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
419 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
423 private setOpenGraphTags () {
424 this.metaService.setTitle(this.video.name)
426 this.metaService.setTag('og:type', 'video')
428 this.metaService.setTag('og:title', this.video.name)
429 this.metaService.setTag('name', this.video.name)
431 this.metaService.setTag('og:description', this.video.description)
432 this.metaService.setTag('description', this.video.description)
434 this.metaService.setTag('og:image', this.video.previewPath)
436 this.metaService.setTag('og:duration', this.video.duration.toString())
438 this.metaService.setTag('og:site_name', 'PeerTube')
440 this.metaService.setTag('og:url', window.location.href)
441 this.metaService.setTag('url', window.location.href)
444 private isAutoplay () {
446 if (!this.user) return true
448 // Be sure the autoPlay is set to false
449 return this.user.autoPlayVideo !== false