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 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 { MarkdownService, VideoDetails, VideoService } from '../shared'
13 import { VideoDownloadComponent } from './video-download.component'
14 import { VideoReportComponent } from './video-report.component'
15 import { VideoShareComponent } from './video-share.component'
18 selector: 'my-video-watch',
19 templateUrl: './video-watch.component.html',
20 styleUrls: [ './video-watch.component.scss' ]
22 export class VideoWatchComponent implements OnInit, OnDestroy {
23 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
24 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
25 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
31 player: videojs.Player
32 playerElement: HTMLMediaElement
34 userRating: UserVideoRateType = null
35 video: VideoDetails = null
36 videoPlayerLoaded = false
38 descriptionLoading = false
40 completeDescriptionShown = false
41 completeVideoDescription: string
42 shortVideoDescription: string
43 videoHTMLDescription = ''
45 private paramsSub: Subscription
48 private elementRef: ElementRef,
49 private route: ActivatedRoute,
50 private router: Router,
51 private videoService: VideoService,
52 private videoBlacklistService: VideoBlacklistService,
53 private confirmService: ConfirmService,
54 private metaService: MetaService,
55 private authService: AuthService,
56 private notificationsService: NotificationsService,
57 private markdownService: MarkdownService
61 this.paramsSub = this.route.params.subscribe(routeParams => {
62 let uuid = routeParams['uuid']
63 this.videoService.getVideo(uuid).subscribe(
64 video => this.onVideoFetched(video),
67 this.videoNotFound = true
75 // Remove player if it exists
76 if (this.videoPlayerLoaded === true) {
77 videojs(this.playerElement).dispose()
80 // Unsubscribe subscriptions
81 this.paramsSub.unsubscribe()
85 if (this.isUserLoggedIn() === false) return
86 // Already liked this video
87 if (this.userRating === 'like') return
89 this.videoService.setVideoLike(this.video.id)
92 // Update the video like attribute
93 this.updateVideoRating(this.userRating, 'like')
94 this.userRating = 'like'
97 err => this.notificationsService.error('Error', err.message)
102 if (this.isUserLoggedIn() === false) return
103 // Already disliked this video
104 if (this.userRating === 'dislike') return
106 this.videoService.setVideoDislike(this.video.id)
109 // Update the video dislike attribute
110 this.updateVideoRating(this.userRating, 'dislike')
111 this.userRating = 'dislike'
114 err => this.notificationsService.error('Error', err.message)
118 removeVideo (event: Event) {
119 event.preventDefault()
121 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
123 if (res === false) return
125 this.videoService.removeVideo(this.video.id)
128 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
129 // Go back to the video-list.
130 this.router.navigate(['/videos/list'])
133 error => this.notificationsService.error('Error', error.text)
139 blacklistVideo (event: Event) {
140 event.preventDefault()
142 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
144 if (res === false) return
146 this.videoBlacklistService.blacklistVideo(this.video.id)
149 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
150 this.router.navigate(['/videos/list'])
153 error => this.notificationsService.error('Error', error.text)
159 showMoreDescription () {
160 if (this.completeVideoDescription === undefined) {
161 return this.loadCompleteDescription()
164 this.updateVideoDescription(this.completeVideoDescription)
165 this.completeDescriptionShown = true
168 showLessDescription () {
170 this.updateVideoDescription(this.shortVideoDescription)
171 this.completeDescriptionShown = false
174 loadCompleteDescription () {
175 this.descriptionLoading = true
177 this.videoService.loadCompleteDescription(this.video.descriptionPath)
180 this.completeDescriptionShown = true
181 this.descriptionLoading = false
183 this.shortVideoDescription = this.video.description
184 this.completeVideoDescription = description
186 this.updateVideoDescription(this.completeVideoDescription)
190 this.descriptionLoading = false
191 this.notificationsService.error('Error', error.text)
196 showReportModal (event: Event) {
197 event.preventDefault()
198 this.videoReportModal.show()
202 this.videoShareModal.show()
205 showDownloadModal (event: Event) {
206 event.preventDefault()
207 this.videoDownloadModal.show()
211 return this.authService.isLoggedIn()
214 canUserUpdateVideo () {
215 return this.video.isUpdatableBy(this.authService.getUser())
218 isVideoRemovable () {
219 return this.video.isRemovableBy(this.authService.getUser())
222 isVideoBlacklistable () {
223 return this.video.isBlackistableBy(this.authService.getUser())
226 private updateVideoDescription (description: string) {
227 this.video.description = description
228 this.setVideoDescriptionHTML()
231 private setVideoDescriptionHTML () {
232 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
235 private handleError (err: any) {
236 const errorMessage: string = typeof err === 'string' ? err : err.message
239 if (errorMessage.indexOf('http error') !== -1) {
240 message = 'Cannot fetch video from server, maybe down.'
242 message = errorMessage
245 this.notificationsService.error('Error', message)
248 private checkUserRating () {
249 // Unlogged users do not have ratings
250 if (this.isUserLoggedIn() === false) return
252 this.videoService.getUserVideoRating(this.video.id)
256 this.userRating = ratingObject.rating
260 err => this.notificationsService.error('Error', err.message)
264 private onVideoFetched (video: VideoDetails) {
268 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
269 observable = this.confirmService.confirm(
270 'This video contains mature or explicit content. Are you sure you want to watch it?',
271 'Mature or explicit content'
274 observable = Observable.of(true)
277 observable.subscribe(
281 return this.router.navigate([ '/videos/list' ])
284 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
286 const videojsOptions = {
291 videoFiles: this.video.files,
292 playerElement: this.playerElement,
299 this.videoPlayerLoaded = true
302 videojs(this.playerElement, videojsOptions, function () {
304 this.on('customError', (event, data) => {
305 self.handleError(data.err)
308 this.on('torrentInfo', (event, data) => {
309 self.downloadSpeed = data.downloadSpeed
310 self.numPeers = data.numPeers
311 self.uploadSpeed = data.uploadSpeed
315 this.setVideoDescriptionHTML()
317 this.setOpenGraphTags()
318 this.checkUserRating()
320 this.prepareViewAdd()
325 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
326 let likesToIncrement = 0
327 let dislikesToIncrement = 0
330 if (oldRating === 'like') likesToIncrement--
331 if (oldRating === 'dislike') dislikesToIncrement--
334 if (newRating === 'like') likesToIncrement++
335 if (newRating === 'dislike') dislikesToIncrement++
337 this.video.likes += likesToIncrement
338 this.video.dislikes += dislikesToIncrement
341 private setOpenGraphTags () {
342 this.metaService.setTitle(this.video.name)
344 this.metaService.setTag('og:type', 'video')
346 this.metaService.setTag('og:title', this.video.name)
347 this.metaService.setTag('name', this.video.name)
349 this.metaService.setTag('og:description', this.video.description)
350 this.metaService.setTag('description', this.video.description)
352 this.metaService.setTag('og:image', this.video.previewPath)
354 this.metaService.setTag('og:duration', this.video.duration.toString())
356 this.metaService.setTag('og:site_name', 'PeerTube')
358 this.metaService.setTag('og:url', window.location.href)
359 this.metaService.setTag('url', window.location.href)
362 private prepareViewAdd () {
363 // After 30 seconds (or 3/4 of the video), increment add a view
364 let viewTimeoutSeconds = 30
365 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
369 .viewVideo(this.video.uuid)
372 }, viewTimeoutSeconds * 1000)