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 || Array.isArray(this.video.tags) === false) return []
217 return this.video.tags.join(', ')
220 isVideoRemovable () {
221 return this.video.isRemovableBy(this.authService.getUser())
224 removeVideo (event: Event) {
225 event.preventDefault()
227 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
230 if (res === false) return
232 this.videoService.removeVideo(this.video.id)
235 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
237 // Go back to the video-list.
238 this.router.navigate([ '/videos/list' ])
241 error => this.notificationsService.error('Error', error.message)
247 private updateVideoDescription (description: string) {
248 this.video.description = description
249 this.setVideoDescriptionHTML()
252 private setVideoDescriptionHTML () {
253 if (!this.video.description) {
254 this.videoHTMLDescription = ''
258 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
261 private setVideoLikesBarTooltipText () {
262 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
265 private handleError (err: any) {
266 const errorMessage: string = typeof err === 'string' ? err : err.message
269 if (errorMessage.indexOf('http error') !== -1) {
270 message = 'Cannot fetch video from server, maybe down.'
272 message = errorMessage
275 this.notificationsService.error('Error', message)
278 private checkUserRating () {
279 // Unlogged users do not have ratings
280 if (this.isUserLoggedIn() === false) return
282 this.videoService.getUserVideoRating(this.video.id)
286 this.userRating = ratingObject.rating
290 err => this.notificationsService.error('Error', err.message)
294 private onVideoFetched (video: VideoDetails) {
297 this.updateOtherVideosDisplayed()
300 if (this.video.isVideoNSFWForUser(this.user)) {
301 observable = this.confirmService.confirm(
302 'This video contains mature or explicit content. Are you sure you want to watch it?',
303 'Mature or explicit content'
306 observable = Observable.of(true)
309 observable.subscribe(
313 return this.router.navigate([ '/videos/list' ])
316 // Player was already loaded
317 if (this.videoPlayerLoaded !== true) {
318 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
320 // If autoplay is true, we don't really need a poster
321 if (this.isAutoplay() === false) {
322 this.playerElement.poster = this.video.previewUrl
325 const videojsOptions = {
327 autoplay: this.isAutoplay(),
330 videoFiles: this.video.files,
331 playerElement: this.playerElement,
333 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid)
336 enableVolumeScroll: false
341 this.videoPlayerLoaded = true
344 this.zone.runOutsideAngular(() => {
345 videojs(this.playerElement, videojsOptions, function () {
347 this.on('customError', (event, data) => {
348 self.handleError(data.err)
353 this.player.peertube().setVideoFiles(this.video.files, this.videoService.getVideoViewUrl(this.video.uuid))
356 this.setVideoDescriptionHTML()
357 this.setVideoLikesBarTooltipText()
359 this.setOpenGraphTags()
360 this.checkUserRating()
365 private setRating (nextRating) {
367 switch (nextRating) {
369 method = this.videoService.setVideoLike
372 method = this.videoService.setVideoDislike
375 method = this.videoService.unsetVideoLike
379 method.call(this.videoService, this.video.id)
382 // Update the video like attribute
383 this.updateVideoRating(this.userRating, nextRating)
384 this.userRating = nextRating
386 err => this.notificationsService.error('Error', err.message)
390 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
391 let likesToIncrement = 0
392 let dislikesToIncrement = 0
395 if (oldRating === 'like') likesToIncrement--
396 if (oldRating === 'dislike') dislikesToIncrement--
399 if (newRating === 'like') likesToIncrement++
400 if (newRating === 'dislike') dislikesToIncrement++
402 this.video.likes += likesToIncrement
403 this.video.dislikes += dislikesToIncrement
406 private updateOtherVideosDisplayed () {
407 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
408 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
412 private setOpenGraphTags () {
413 this.metaService.setTitle(this.video.name)
415 this.metaService.setTag('og:type', 'video')
417 this.metaService.setTag('og:title', this.video.name)
418 this.metaService.setTag('name', this.video.name)
420 this.metaService.setTag('og:description', this.video.description)
421 this.metaService.setTag('description', this.video.description)
423 this.metaService.setTag('og:image', this.video.previewPath)
425 this.metaService.setTag('og:duration', this.video.duration.toString())
427 this.metaService.setTag('og:site_name', 'PeerTube')
429 this.metaService.setTag('og:url', window.location.href)
430 this.metaService.setTag('url', window.location.href)
433 private isAutoplay () {
435 if (!this.user) return true
437 // Be sure the autoPlay is set to false
438 return this.user.autoPlayVideo !== false