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[] = []
36 player: videojs.Player
37 playerElement: HTMLVideoElement
38 userRating: UserVideoRateType = null
39 video: VideoDetails = null
40 videoPlayerLoaded = false
42 descriptionLoading = false
44 completeDescriptionShown = false
45 completeVideoDescription: string
46 shortVideoDescription: string
47 videoHTMLDescription = ''
48 likesBarTooltipText = ''
50 private otherVideos: Video[] = []
51 private paramsSub: Subscription
54 private elementRef: ElementRef,
55 private route: ActivatedRoute,
56 private router: Router,
57 private videoService: VideoService,
58 private videoBlacklistService: VideoBlacklistService,
59 private confirmService: ConfirmService,
60 private metaService: MetaService,
61 private authService: AuthService,
62 private notificationsService: NotificationsService,
63 private markdownService: MarkdownService,
68 return this.authService.getUser()
72 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
74 data => this.otherVideos = data.videos,
75 err => console.error(err)
78 this.paramsSub = this.route.params.subscribe(routeParams => {
79 if (this.videoPlayerLoaded) {
83 let uuid = routeParams['uuid']
84 this.videoService.getVideo(uuid).subscribe(
85 video => this.onVideoFetched(video),
88 this.videoNotFound = true
96 // Remove player if it exists
97 if (this.videoPlayerLoaded === true) {
98 videojs(this.playerElement).dispose()
101 // Unsubscribe subscriptions
102 this.paramsSub.unsubscribe()
106 if (this.isUserLoggedIn() === false) return
107 if (this.userRating === 'like') {
108 // Already liked this video
109 this.setRating('none')
111 this.setRating('like')
116 if (this.isUserLoggedIn() === false) return
117 if (this.userRating === 'dislike') {
118 // Already disliked this video
119 this.setRating('none')
121 this.setRating('dislike')
125 blacklistVideo (event: Event) {
126 event.preventDefault()
128 this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist').subscribe(
130 if (res === false) return
132 this.videoBlacklistService.blacklistVideo(this.video.id)
135 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
136 this.router.navigate(['/videos/list'])
139 error => this.notificationsService.error('Error', error.message)
145 showMoreDescription () {
146 if (this.completeVideoDescription === undefined) {
147 return this.loadCompleteDescription()
150 this.updateVideoDescription(this.completeVideoDescription)
151 this.completeDescriptionShown = true
154 showLessDescription () {
155 this.updateVideoDescription(this.shortVideoDescription)
156 this.completeDescriptionShown = false
159 loadCompleteDescription () {
160 this.descriptionLoading = true
162 this.videoService.loadCompleteDescription(this.video.descriptionPath)
165 this.completeDescriptionShown = true
166 this.descriptionLoading = false
168 this.shortVideoDescription = this.video.description
169 this.completeVideoDescription = description
171 this.updateVideoDescription(this.completeVideoDescription)
175 this.descriptionLoading = false
176 this.notificationsService.error('Error', error.message)
181 showReportModal (event: Event) {
182 event.preventDefault()
183 this.videoReportModal.show()
187 this.videoShareModal.show()
190 showDownloadModal (event: Event) {
191 event.preventDefault()
192 this.videoDownloadModal.show()
196 return this.authService.isLoggedIn()
199 isVideoUpdatable () {
200 return this.video.isUpdatableBy(this.authService.getUser())
203 isVideoBlacklistable () {
204 return this.video.isBlackistableBy(this.user)
208 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
212 if (!this.video || Array.isArray(this.video.tags) === false) return []
214 return this.video.tags.join(', ')
217 isVideoRemovable () {
218 return this.video.isRemovableBy(this.authService.getUser())
221 removeVideo (event: Event) {
222 event.preventDefault()
224 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
227 if (res === false) return
229 this.videoService.removeVideo(this.video.id)
232 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
234 // Go back to the video-list.
235 this.router.navigate([ '/videos/list' ])
238 error => this.notificationsService.error('Error', error.message)
244 private updateVideoDescription (description: string) {
245 this.video.description = description
246 this.setVideoDescriptionHTML()
249 private setVideoDescriptionHTML () {
250 if (!this.video.description) {
251 this.videoHTMLDescription = ''
255 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
258 private setVideoLikesBarTooltipText () {
259 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
262 private handleError (err: any) {
263 const errorMessage: string = typeof err === 'string' ? err : err.message
266 if (errorMessage.indexOf('http error') !== -1) {
267 message = 'Cannot fetch video from server, maybe down.'
269 message = errorMessage
272 this.notificationsService.error('Error', message)
275 private checkUserRating () {
276 // Unlogged users do not have ratings
277 if (this.isUserLoggedIn() === false) return
279 this.videoService.getUserVideoRating(this.video.id)
283 this.userRating = ratingObject.rating
287 err => this.notificationsService.error('Error', err.message)
291 private onVideoFetched (video: VideoDetails) {
294 if (this.otherVideos.length > 0) {
295 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
299 if (this.video.isVideoNSFWForUser(this.user)) {
300 observable = this.confirmService.confirm(
301 'This video contains mature or explicit content. Are you sure you want to watch it?',
302 'Mature or explicit content'
305 observable = Observable.of(true)
308 observable.subscribe(
312 return this.router.navigate([ '/videos/list' ])
315 // Player was already loaded
316 if (this.videoPlayerLoaded !== true) {
317 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
319 // If autoplay is true, we don't really need a poster
320 if (this.isAutoplay() === false) {
321 this.playerElement.poster = this.video.previewUrl
324 const videojsOptions = {
326 autoplay: this.isAutoplay(),
329 videoFiles: this.video.files,
330 playerElement: this.playerElement,
334 enableVolumeScroll: false
339 this.videoPlayerLoaded = true
342 this.zone.runOutsideAngular(() => {
343 videojs(this.playerElement, videojsOptions, function () {
345 this.on('customError', (event, data) => {
346 self.handleError(data.err)
351 (this.player as any).setVideoFiles(this.video.files)
354 this.setVideoDescriptionHTML()
355 this.setVideoLikesBarTooltipText()
357 this.setOpenGraphTags()
358 this.checkUserRating()
360 this.prepareViewAdd()
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 setOpenGraphTags () {
407 this.metaService.setTitle(this.video.name)
409 this.metaService.setTag('og:type', 'video')
411 this.metaService.setTag('og:title', this.video.name)
412 this.metaService.setTag('name', this.video.name)
414 this.metaService.setTag('og:description', this.video.description)
415 this.metaService.setTag('description', this.video.description)
417 this.metaService.setTag('og:image', this.video.previewPath)
419 this.metaService.setTag('og:duration', this.video.duration.toString())
421 this.metaService.setTag('og:site_name', 'PeerTube')
423 this.metaService.setTag('og:url', window.location.href)
424 this.metaService.setTag('url', window.location.href)
427 private prepareViewAdd () {
428 // After 30 seconds (or 3/4 of the video), increment add a view
429 let viewTimeoutSeconds = 30
430 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
434 .viewVideo(this.video.uuid)
437 }, viewTimeoutSeconds * 1000)
440 private isAutoplay () {
442 if (!this.user) return true
444 // Be sure the autoPlay is set to false
445 return this.user.autoPlayVideo !== false