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 async blacklistVideo (event: Event) {
134 event.preventDefault()
136 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
137 if (res === false) return
139 this.videoBlacklistService.blacklistVideo(this.video.id)
142 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
143 this.router.navigate(['/videos/list'])
146 error => this.notificationsService.error('Error', error.message)
150 showMoreDescription () {
151 if (this.completeVideoDescription === undefined) {
152 return this.loadCompleteDescription()
155 this.updateVideoDescription(this.completeVideoDescription)
156 this.completeDescriptionShown = true
159 showLessDescription () {
160 this.updateVideoDescription(this.shortVideoDescription)
161 this.completeDescriptionShown = false
164 loadCompleteDescription () {
165 this.descriptionLoading = true
167 this.videoService.loadCompleteDescription(this.video.descriptionPath)
170 this.completeDescriptionShown = true
171 this.descriptionLoading = false
173 this.shortVideoDescription = this.video.description
174 this.completeVideoDescription = description
176 this.updateVideoDescription(this.completeVideoDescription)
180 this.descriptionLoading = false
181 this.notificationsService.error('Error', error.message)
186 showReportModal (event: Event) {
187 event.preventDefault()
188 this.videoReportModal.show()
191 showSupportModal () {
192 this.videoSupportModal.show()
196 this.videoShareModal.show()
199 showDownloadModal (event: Event) {
200 event.preventDefault()
201 this.videoDownloadModal.show()
205 return this.authService.isLoggedIn()
208 isVideoUpdatable () {
209 return this.video.isUpdatableBy(this.authService.getUser())
212 isVideoBlacklistable () {
213 return this.video.isBlackistableBy(this.user)
217 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
221 if (!this.video) return ''
223 return this.video.previewUrl
227 if (!this.video || Array.isArray(this.video.tags) === false) return []
229 return this.video.tags.join(', ')
232 isVideoRemovable () {
233 return this.video.isRemovableBy(this.authService.getUser())
236 async removeVideo (event: Event) {
237 event.preventDefault()
239 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
240 if (res === false) return
242 this.videoService.removeVideo(this.video.id)
245 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
247 // Go back to the video-list.
248 this.router.navigate([ '/videos/list' ])
251 error => this.notificationsService.error('Error', error.message)
255 private updateVideoDescription (description: string) {
256 this.video.description = description
257 this.setVideoDescriptionHTML()
260 private setVideoDescriptionHTML () {
261 if (!this.video.description) {
262 this.videoHTMLDescription = ''
266 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
269 private setVideoLikesBarTooltipText () {
270 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
273 private handleError (err: any) {
274 const errorMessage: string = typeof err === 'string' ? err : err.message
275 if (!errorMessage) return
279 if (errorMessage.indexOf('http error') !== -1) {
280 message = 'Cannot fetch video from server, maybe down.'
282 message = errorMessage
285 this.notificationsService.error('Error', message)
288 private checkUserRating () {
289 // Unlogged users do not have ratings
290 if (this.isUserLoggedIn() === false) return
292 this.videoService.getUserVideoRating(this.video.id)
296 this.userRating = ratingObject.rating
300 err => this.notificationsService.error('Error', err.message)
304 private onVideoFetched (video: VideoDetails) {
307 this.updateOtherVideosDisplayed()
310 if (this.video.isVideoNSFWForUser(this.user)) {
311 observable = this.confirmService.confirm(
312 'This video contains mature or explicit content. Are you sure you want to watch it?',
313 'Mature or explicit content'
316 observable = Observable.of(true)
319 observable.subscribe(
323 return this.router.navigate([ '/videos/list' ])
326 // Player was already loaded
327 if (this.videoPlayerLoaded !== true) {
328 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
330 // If autoplay is true, we don't really need a poster
331 if (this.isAutoplay() === false) {
332 this.playerElement.poster = this.video.previewUrl
335 const videojsOptions = {
337 autoplay: this.isAutoplay(),
340 videoFiles: this.video.files,
341 playerElement: this.playerElement,
343 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
344 videoDuration: this.video.duration
347 enableVolumeScroll: false
352 this.videoPlayerLoaded = true
355 this.zone.runOutsideAngular(() => {
356 videojs(this.playerElement, videojsOptions, function () {
358 this.on('customError', (event, data) => self.handleError(data.err))
362 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
363 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
366 this.setVideoDescriptionHTML()
367 this.setVideoLikesBarTooltipText()
369 this.setOpenGraphTags()
370 this.checkUserRating()
375 private setRating (nextRating) {
377 switch (nextRating) {
379 method = this.videoService.setVideoLike
382 method = this.videoService.setVideoDislike
385 method = this.videoService.unsetVideoLike
389 method.call(this.videoService, this.video.id)
392 // Update the video like attribute
393 this.updateVideoRating(this.userRating, nextRating)
394 this.userRating = nextRating
396 err => this.notificationsService.error('Error', err.message)
400 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
401 let likesToIncrement = 0
402 let dislikesToIncrement = 0
405 if (oldRating === 'like') likesToIncrement--
406 if (oldRating === 'dislike') dislikesToIncrement--
409 if (newRating === 'like') likesToIncrement++
410 if (newRating === 'dislike') dislikesToIncrement++
412 this.video.likes += likesToIncrement
413 this.video.dislikes += dislikesToIncrement
414 this.video.buildLikeAndDislikePercents()
416 this.setVideoLikesBarTooltipText()
419 private updateOtherVideosDisplayed () {
420 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
421 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
425 private setOpenGraphTags () {
426 this.metaService.setTitle(this.video.name)
428 this.metaService.setTag('og:type', 'video')
430 this.metaService.setTag('og:title', this.video.name)
431 this.metaService.setTag('name', this.video.name)
433 this.metaService.setTag('og:description', this.video.description)
434 this.metaService.setTag('description', this.video.description)
436 this.metaService.setTag('og:image', this.video.previewPath)
438 this.metaService.setTag('og:duration', this.video.duration.toString())
440 this.metaService.setTag('og:site_name', 'PeerTube')
442 this.metaService.setTag('og:url', window.location.href)
443 this.metaService.setTag('url', window.location.href)
446 private isAutoplay () {
448 if (!this.user) return true
450 // Be sure the autoPlay is set to false
451 return this.user.autoPlayVideo !== false