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 let uuid = routeParams['uuid']
87 this.videoService.getVideo(uuid).subscribe(
88 video => this.onVideoFetched(video),
91 this.videoNotFound = true
99 // Remove player if it exists
100 if (this.videoPlayerLoaded === true) {
101 videojs(this.playerElement).dispose()
104 // Unsubscribe subscriptions
105 this.paramsSub.unsubscribe()
109 if (this.isUserLoggedIn() === false) return
110 if (this.userRating === 'like') {
111 // Already liked this video
112 this.setRating('none')
114 this.setRating('like')
119 if (this.isUserLoggedIn() === false) return
120 if (this.userRating === 'dislike') {
121 // Already disliked this video
122 this.setRating('none')
124 this.setRating('dislike')
128 blacklistVideo (event: Event) {
129 event.preventDefault()
131 this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist').subscribe(
133 if (res === false) return
135 this.videoBlacklistService.blacklistVideo(this.video.id)
138 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
139 this.router.navigate(['/videos/list'])
142 error => this.notificationsService.error('Error', error.message)
148 showMoreDescription () {
149 if (this.completeVideoDescription === undefined) {
150 return this.loadCompleteDescription()
153 this.updateVideoDescription(this.completeVideoDescription)
154 this.completeDescriptionShown = true
157 showLessDescription () {
158 this.updateVideoDescription(this.shortVideoDescription)
159 this.completeDescriptionShown = false
162 loadCompleteDescription () {
163 this.descriptionLoading = true
165 this.videoService.loadCompleteDescription(this.video.descriptionPath)
168 this.completeDescriptionShown = true
169 this.descriptionLoading = false
171 this.shortVideoDescription = this.video.description
172 this.completeVideoDescription = description
174 this.updateVideoDescription(this.completeVideoDescription)
178 this.descriptionLoading = false
179 this.notificationsService.error('Error', error.message)
184 showReportModal (event: Event) {
185 event.preventDefault()
186 this.videoReportModal.show()
190 this.videoShareModal.show()
193 showDownloadModal (event: Event) {
194 event.preventDefault()
195 this.videoDownloadModal.show()
199 return this.authService.isLoggedIn()
202 isVideoUpdatable () {
203 return this.video.isUpdatableBy(this.authService.getUser())
206 isVideoBlacklistable () {
207 return this.video.isBlackistableBy(this.user)
211 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
215 if (!this.video) return ''
217 return this.video.previewUrl
221 if (!this.video || Array.isArray(this.video.tags) === false) return []
223 return this.video.tags.join(', ')
226 isVideoRemovable () {
227 return this.video.isRemovableBy(this.authService.getUser())
230 removeVideo (event: Event) {
231 event.preventDefault()
233 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
236 if (res === false) return
238 this.videoService.removeVideo(this.video.id)
241 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
243 // Go back to the video-list.
244 this.router.navigate([ '/videos/list' ])
247 error => this.notificationsService.error('Error', error.message)
253 private updateVideoDescription (description: string) {
254 this.video.description = description
255 this.setVideoDescriptionHTML()
258 private setVideoDescriptionHTML () {
259 if (!this.video.description) {
260 this.videoHTMLDescription = ''
264 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
267 private setVideoLikesBarTooltipText () {
268 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
271 private handleError (err: any) {
272 const errorMessage: string = typeof err === 'string' ? err : err.message
275 if (errorMessage.indexOf('http error') !== -1) {
276 message = 'Cannot fetch video from server, maybe down.'
278 message = errorMessage
281 this.notificationsService.error('Error', message)
284 private checkUserRating () {
285 // Unlogged users do not have ratings
286 if (this.isUserLoggedIn() === false) return
288 this.videoService.getUserVideoRating(this.video.id)
292 this.userRating = ratingObject.rating
296 err => this.notificationsService.error('Error', err.message)
300 private onVideoFetched (video: VideoDetails) {
303 this.updateOtherVideosDisplayed()
306 if (this.video.isVideoNSFWForUser(this.user)) {
307 observable = this.confirmService.confirm(
308 'This video contains mature or explicit content. Are you sure you want to watch it?',
309 'Mature or explicit content'
312 observable = Observable.of(true)
315 observable.subscribe(
319 return this.router.navigate([ '/videos/list' ])
322 // Player was already loaded
323 if (this.videoPlayerLoaded !== true) {
324 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
326 // If autoplay is true, we don't really need a poster
327 if (this.isAutoplay() === false) {
328 this.playerElement.poster = this.video.previewUrl
331 const videojsOptions = {
333 autoplay: this.isAutoplay(),
336 videoFiles: this.video.files,
337 playerElement: this.playerElement,
339 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
340 videoDuration: this.video.duration
343 enableVolumeScroll: false
348 this.videoPlayerLoaded = true
351 this.zone.runOutsideAngular(() => {
352 videojs(this.playerElement, videojsOptions, function () {
354 this.on('customError', (event, data) => {
355 self.handleError(data.err)
360 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
361 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
364 this.setVideoDescriptionHTML()
365 this.setVideoLikesBarTooltipText()
367 this.setOpenGraphTags()
368 this.checkUserRating()
373 private setRating (nextRating) {
375 switch (nextRating) {
377 method = this.videoService.setVideoLike
380 method = this.videoService.setVideoDislike
383 method = this.videoService.unsetVideoLike
387 method.call(this.videoService, this.video.id)
390 // Update the video like attribute
391 this.updateVideoRating(this.userRating, nextRating)
392 this.userRating = nextRating
394 err => this.notificationsService.error('Error', err.message)
398 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
399 let likesToIncrement = 0
400 let dislikesToIncrement = 0
403 if (oldRating === 'like') likesToIncrement--
404 if (oldRating === 'dislike') dislikesToIncrement--
407 if (newRating === 'like') likesToIncrement++
408 if (newRating === 'dislike') dislikesToIncrement++
410 this.video.likes += likesToIncrement
411 this.video.dislikes += dislikesToIncrement
414 private updateOtherVideosDisplayed () {
415 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
416 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
420 private setOpenGraphTags () {
421 this.metaService.setTitle(this.video.name)
423 this.metaService.setTag('og:type', 'video')
425 this.metaService.setTag('og:title', this.video.name)
426 this.metaService.setTag('name', this.video.name)
428 this.metaService.setTag('og:description', this.video.description)
429 this.metaService.setTag('description', this.video.description)
431 this.metaService.setTag('og:image', this.video.previewPath)
433 this.metaService.setTag('og:duration', this.video.duration.toString())
435 this.metaService.setTag('og:site_name', 'PeerTube')
437 this.metaService.setTag('og:url', window.location.href)
438 this.metaService.setTag('url', window.location.href)
441 private isAutoplay () {
443 if (!this.user) return true
445 // Be sure the autoPlay is set to false
446 return this.user.autoPlayVideo !== false