1 import { Component, ElementRef, 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 { UserVideoRateType, VideoRateType } from '../../../../../shared'
9 import '../../../assets/player/peertube-videojs-plugin'
10 import { AuthService, ConfirmService } from '../../core'
11 import { VideoBlacklistService } from '../../shared'
12 import { Account } from '../../shared/account/account.model'
13 import { VideoDetails } from '../../shared/video/video-details.model'
14 import { Video } from '../../shared/video/video.model'
15 import { VideoService } from '../../shared/video/video.service'
16 import { MarkdownService } from '../shared'
17 import { VideoDownloadComponent } from './modal/video-download.component'
18 import { VideoReportComponent } from './modal/video-report.component'
19 import { VideoShareComponent } from './modal/video-share.component'
22 selector: 'my-video-watch',
23 templateUrl: './video-watch.component.html',
24 styleUrls: [ './video-watch.component.scss' ]
26 export class VideoWatchComponent implements OnInit, OnDestroy {
27 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
28 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
29 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
31 otherVideos: 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 paramsSub: Subscription
52 private elementRef: ElementRef,
53 private route: ActivatedRoute,
54 private router: Router,
55 private videoService: VideoService,
56 private videoBlacklistService: VideoBlacklistService,
57 private confirmService: ConfirmService,
58 private metaService: MetaService,
59 private authService: AuthService,
60 private notificationsService: NotificationsService,
61 private markdownService: MarkdownService
65 return this.authService.getUser()
69 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
71 data => this.otherVideos = data.videos,
73 err => console.error(err)
76 this.paramsSub = this.route.params.subscribe(routeParams => {
77 if (this.videoPlayerLoaded) {
81 let uuid = routeParams['uuid']
82 this.videoService.getVideo(uuid).subscribe(
83 video => this.onVideoFetched(video),
86 this.videoNotFound = true
94 // Remove player if it exists
95 if (this.videoPlayerLoaded === true) {
96 videojs(this.playerElement).dispose()
99 // Unsubscribe subscriptions
100 this.paramsSub.unsubscribe()
104 if (this.isUserLoggedIn() === false) return
105 // Already liked this video
106 if (this.userRating === 'like') return
108 this.videoService.setVideoLike(this.video.id)
111 // Update the video like attribute
112 this.updateVideoRating(this.userRating, 'like')
113 this.userRating = 'like'
116 err => this.notificationsService.error('Error', err.message)
121 if (this.isUserLoggedIn() === false) return
122 // Already disliked this video
123 if (this.userRating === 'dislike') return
125 this.videoService.setVideoDislike(this.video.id)
128 // Update the video dislike attribute
129 this.updateVideoRating(this.userRating, 'dislike')
130 this.userRating = 'dislike'
133 err => this.notificationsService.error('Error', err.message)
137 blacklistVideo (event: Event) {
138 event.preventDefault()
140 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
142 if (res === false) return
144 this.videoBlacklistService.blacklistVideo(this.video.id)
147 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
148 this.router.navigate(['/videos/list'])
151 error => this.notificationsService.error('Error', error.text)
157 showMoreDescription () {
158 if (this.completeVideoDescription === undefined) {
159 return this.loadCompleteDescription()
162 this.updateVideoDescription(this.completeVideoDescription)
163 this.completeDescriptionShown = true
166 showLessDescription () {
167 this.updateVideoDescription(this.shortVideoDescription)
168 this.completeDescriptionShown = false
171 loadCompleteDescription () {
172 this.descriptionLoading = true
174 this.videoService.loadCompleteDescription(this.video.descriptionPath)
177 this.completeDescriptionShown = true
178 this.descriptionLoading = false
180 this.shortVideoDescription = this.video.description
181 this.completeVideoDescription = description
183 this.updateVideoDescription(this.completeVideoDescription)
187 this.descriptionLoading = false
188 this.notificationsService.error('Error', error.text)
193 showReportModal (event: Event) {
194 event.preventDefault()
195 this.videoReportModal.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_PATH(this.video.account)
224 if (!this.video || Array.isArray(this.video.tags) === false) return []
226 return this.video.tags.join(', ')
229 isVideoRemovable () {
230 return this.video.isRemovableBy(this.authService.getUser())
233 removeVideo (event: Event) {
234 event.preventDefault()
236 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
239 if (res === false) return
241 this.videoService.removeVideo(this.video.id)
244 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
246 // Go back to the video-list.
247 this.router.navigate([ '/videos/list' ])
250 error => this.notificationsService.error('Error', error.text)
256 private updateVideoDescription (description: string) {
257 this.video.description = description
258 this.setVideoDescriptionHTML()
261 private setVideoDescriptionHTML () {
262 if (!this.video.description) {
263 this.videoHTMLDescription = ''
267 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
270 private setVideoLikesBarTooltipText () {
271 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
274 private handleError (err: any) {
275 const errorMessage: string = typeof err === 'string' ? err : err.message
278 if (errorMessage.indexOf('http error') !== -1) {
279 message = 'Cannot fetch video from server, maybe down.'
281 message = errorMessage
284 this.notificationsService.error('Error', message)
287 private checkUserRating () {
288 // Unlogged users do not have ratings
289 if (this.isUserLoggedIn() === false) return
291 this.videoService.getUserVideoRating(this.video.id)
295 this.userRating = ratingObject.rating
299 err => this.notificationsService.error('Error', err.message)
303 private onVideoFetched (video: VideoDetails) {
307 if (this.video.isVideoNSFWForUser(this.user)) {
308 observable = this.confirmService.confirm(
309 'This video contains mature or explicit content. Are you sure you want to watch it?',
310 'Mature or explicit content'
313 observable = Observable.of(true)
316 observable.subscribe(
320 return this.router.navigate([ '/videos/list' ])
323 // Player was already loaded
324 if (this.videoPlayerLoaded !== true) {
325 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
327 // If autoplay is true, we don't really need a poster
328 if (this.isAutoplay() === false) {
329 this.playerElement.poster = this.video.previewUrl
332 const videojsOptions = {
334 autoplay: this.isAutoplay(),
337 videoFiles: this.video.files,
338 playerElement: this.playerElement,
339 autoplay: this.isAutoplay(),
345 this.videoPlayerLoaded = true
348 videojs(this.playerElement, videojsOptions, function () {
350 this.on('customError', (event, data) => {
351 self.handleError(data.err)
355 (this.player as any).setVideoFiles(this.video.files)
358 this.setVideoDescriptionHTML()
359 this.setVideoLikesBarTooltipText()
361 this.setOpenGraphTags()
362 this.checkUserRating()
364 this.prepareViewAdd()
369 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
370 let likesToIncrement = 0
371 let dislikesToIncrement = 0
374 if (oldRating === 'like') likesToIncrement--
375 if (oldRating === 'dislike') dislikesToIncrement--
378 if (newRating === 'like') likesToIncrement++
379 if (newRating === 'dislike') dislikesToIncrement++
381 this.video.likes += likesToIncrement
382 this.video.dislikes += dislikesToIncrement
385 private setOpenGraphTags () {
386 this.metaService.setTitle(this.video.name)
388 this.metaService.setTag('og:type', 'video')
390 this.metaService.setTag('og:title', this.video.name)
391 this.metaService.setTag('name', this.video.name)
393 this.metaService.setTag('og:description', this.video.description)
394 this.metaService.setTag('description', this.video.description)
396 this.metaService.setTag('og:image', this.video.previewPath)
398 this.metaService.setTag('og:duration', this.video.duration.toString())
400 this.metaService.setTag('og:site_name', 'PeerTube')
402 this.metaService.setTag('og:url', window.location.href)
403 this.metaService.setTag('url', window.location.href)
406 private prepareViewAdd () {
407 // After 30 seconds (or 3/4 of the video), increment add a view
408 let viewTimeoutSeconds = 30
409 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
413 .viewVideo(this.video.uuid)
416 }, viewTimeoutSeconds * 1000)
419 private isAutoplay () {
421 if (!this.user) return true
423 // Be sure the autoPlay is set to false
424 return this.user.autoPlayVideo !== false