1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
4 import { MetaService } from '@ngx-meta/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { Observable } from 'rxjs/Observable'
7 import { Subscription } from 'rxjs/Subscription'
8 import * as videojs from 'video.js'
9 import 'videojs-hotkeys'
10 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
11 import '../../../assets/player/peertube-videojs-plugin'
12 import { AuthService, ConfirmService } from '../../core'
13 import { VideoBlacklistService } from '../../shared'
14 import { Account } from '../../shared/account/account.model'
15 import { VideoDetails } from '../../shared/video/video-details.model'
16 import { Video } from '../../shared/video/video.model'
17 import { VideoService } from '../../shared/video/video.service'
18 import { MarkdownService } from '../shared'
19 import { VideoDownloadComponent } from './modal/video-download.component'
20 import { VideoReportComponent } from './modal/video-report.component'
21 import { VideoShareComponent } from './modal/video-share.component'
24 selector: 'my-video-watch',
25 templateUrl: './video-watch.component.html',
26 styleUrls: [ './video-watch.component.scss' ]
28 export class VideoWatchComponent implements OnInit, OnDestroy {
29 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
30 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
31 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
32 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
34 otherVideosDisplayed: Video[] = []
37 player: videojs.Player
38 playerElement: HTMLVideoElement
39 userRating: UserVideoRateType = null
40 video: VideoDetails = null
41 videoPlayerLoaded = false
43 descriptionLoading = false
45 completeDescriptionShown = false
46 completeVideoDescription: string
47 shortVideoDescription: string
48 videoHTMLDescription = ''
49 likesBarTooltipText = ''
51 private otherVideos: Video[] = []
52 private paramsSub: Subscription
55 private elementRef: ElementRef,
56 private route: ActivatedRoute,
57 private router: Router,
58 private videoService: VideoService,
59 private videoBlacklistService: VideoBlacklistService,
60 private confirmService: ConfirmService,
61 private metaService: MetaService,
62 private authService: AuthService,
63 private notificationsService: NotificationsService,
64 private markdownService: MarkdownService,
69 return this.authService.getUser()
73 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
76 this.otherVideos = data.videos
77 this.updateOtherVideosDisplayed()
80 err => console.error(err)
83 this.paramsSub = this.route.params.subscribe(routeParams => {
84 if (this.videoPlayerLoaded) {
88 const uuid = routeParams['uuid']
89 // Video did not changed
90 if (this.video && this.video.uuid === uuid) return
92 this.videoService.getVideo(uuid).subscribe(
93 video => this.onVideoFetched(video),
96 this.videoNotFound = true
104 // Remove player if it exists
105 if (this.videoPlayerLoaded === true) {
106 videojs(this.playerElement).dispose()
109 // Unsubscribe subscriptions
110 this.paramsSub.unsubscribe()
114 if (this.isUserLoggedIn() === false) return
115 if (this.userRating === 'like') {
116 // Already liked this video
117 this.setRating('none')
119 this.setRating('like')
124 if (this.isUserLoggedIn() === false) return
125 if (this.userRating === 'dislike') {
126 // Already disliked this video
127 this.setRating('none')
129 this.setRating('dislike')
133 blacklistVideo (event: Event) {
134 event.preventDefault()
136 this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist').subscribe(
138 if (res === false) return
140 this.videoBlacklistService.blacklistVideo(this.video.id)
143 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
144 this.router.navigate(['/videos/list'])
147 error => this.notificationsService.error('Error', error.message)
153 showMoreDescription () {
154 if (this.completeVideoDescription === undefined) {
155 return this.loadCompleteDescription()
158 this.updateVideoDescription(this.completeVideoDescription)
159 this.completeDescriptionShown = true
162 showLessDescription () {
163 this.updateVideoDescription(this.shortVideoDescription)
164 this.completeDescriptionShown = false
167 loadCompleteDescription () {
168 this.descriptionLoading = true
170 this.videoService.loadCompleteDescription(this.video.descriptionPath)
173 this.completeDescriptionShown = true
174 this.descriptionLoading = false
176 this.shortVideoDescription = this.video.description
177 this.completeVideoDescription = description
179 this.updateVideoDescription(this.completeVideoDescription)
183 this.descriptionLoading = false
184 this.notificationsService.error('Error', error.message)
189 showReportModal (event: Event) {
190 event.preventDefault()
191 this.videoReportModal.show()
194 showSupportModal () {
195 this.videoSupportModal.show()
199 this.videoShareModal.show()
202 showDownloadModal (event: Event) {
203 event.preventDefault()
204 this.videoDownloadModal.show()
208 return this.authService.isLoggedIn()
211 isVideoUpdatable () {
212 return this.video.isUpdatableBy(this.authService.getUser())
215 isVideoBlacklistable () {
216 return this.video.isBlackistableBy(this.user)
220 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
224 if (!this.video) return ''
226 return this.video.previewUrl
230 if (!this.video || Array.isArray(this.video.tags) === false) return []
232 return this.video.tags.join(', ')
235 isVideoRemovable () {
236 return this.video.isRemovableBy(this.authService.getUser())
239 removeVideo (event: Event) {
240 event.preventDefault()
242 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
245 if (res === false) return
247 this.videoService.removeVideo(this.video.id)
250 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
252 // Go back to the video-list.
253 this.router.navigate([ '/videos/list' ])
256 error => this.notificationsService.error('Error', error.message)
262 private updateVideoDescription (description: string) {
263 this.video.description = description
264 this.setVideoDescriptionHTML()
267 private setVideoDescriptionHTML () {
268 if (!this.video.description) {
269 this.videoHTMLDescription = ''
273 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
276 private setVideoLikesBarTooltipText () {
277 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
280 private handleError (err: any) {
281 const errorMessage: string = typeof err === 'string' ? err : err.message
284 if (errorMessage.indexOf('http error') !== -1) {
285 message = 'Cannot fetch video from server, maybe down.'
287 message = errorMessage
290 this.notificationsService.error('Error', message)
293 private checkUserRating () {
294 // Unlogged users do not have ratings
295 if (this.isUserLoggedIn() === false) return
297 this.videoService.getUserVideoRating(this.video.id)
301 this.userRating = ratingObject.rating
305 err => this.notificationsService.error('Error', err.message)
309 private onVideoFetched (video: VideoDetails) {
312 this.updateOtherVideosDisplayed()
315 if (this.video.isVideoNSFWForUser(this.user)) {
316 observable = this.confirmService.confirm(
317 'This video contains mature or explicit content. Are you sure you want to watch it?',
318 'Mature or explicit content'
321 observable = Observable.of(true)
324 observable.subscribe(
328 return this.router.navigate([ '/videos/list' ])
331 // Player was already loaded
332 if (this.videoPlayerLoaded !== true) {
333 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
335 // If autoplay is true, we don't really need a poster
336 if (this.isAutoplay() === false) {
337 this.playerElement.poster = this.video.previewUrl
340 const videojsOptions = {
342 autoplay: this.isAutoplay(),
345 videoFiles: this.video.files,
346 playerElement: this.playerElement,
348 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
349 videoDuration: this.video.duration
352 enableVolumeScroll: false
357 this.videoPlayerLoaded = true
360 this.zone.runOutsideAngular(() => {
361 videojs(this.playerElement, videojsOptions, function () {
363 this.on('customError', (event, data) => {
364 self.handleError(data.err)
369 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
370 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
373 this.setVideoDescriptionHTML()
374 this.setVideoLikesBarTooltipText()
376 this.setOpenGraphTags()
377 this.checkUserRating()
382 private setRating (nextRating) {
384 switch (nextRating) {
386 method = this.videoService.setVideoLike
389 method = this.videoService.setVideoDislike
392 method = this.videoService.unsetVideoLike
396 method.call(this.videoService, this.video.id)
399 // Update the video like attribute
400 this.updateVideoRating(this.userRating, nextRating)
401 this.userRating = nextRating
403 err => this.notificationsService.error('Error', err.message)
407 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
408 let likesToIncrement = 0
409 let dislikesToIncrement = 0
412 if (oldRating === 'like') likesToIncrement--
413 if (oldRating === 'dislike') dislikesToIncrement--
416 if (newRating === 'like') likesToIncrement++
417 if (newRating === 'dislike') dislikesToIncrement++
419 this.video.likes += likesToIncrement
420 this.video.dislikes += dislikesToIncrement
423 private updateOtherVideosDisplayed () {
424 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
425 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
429 private setOpenGraphTags () {
430 this.metaService.setTitle(this.video.name)
432 this.metaService.setTag('og:type', 'video')
434 this.metaService.setTag('og:title', this.video.name)
435 this.metaService.setTag('name', this.video.name)
437 this.metaService.setTag('og:description', this.video.description)
438 this.metaService.setTag('description', this.video.description)
440 this.metaService.setTag('og:image', this.video.previewPath)
442 this.metaService.setTag('og:duration', this.video.duration.toString())
444 this.metaService.setTag('og:site_name', 'PeerTube')
446 this.metaService.setTag('og:url', window.location.href)
447 this.metaService.setTag('url', window.location.href)
450 private isAutoplay () {
452 if (!this.user) return true
454 // Be sure the autoPlay is set to false
455 return this.user.autoPlayVideo !== false