1 import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Observable } from 'rxjs/Observable'
4 import { Subscription } from 'rxjs/Subscription'
6 import videojs from 'video.js'
7 import '../../../assets/player/peertube-videojs-plugin'
9 import { MetaService } from '@ngx-meta/core'
10 import { NotificationsService } from 'angular2-notifications'
12 import { AuthService, ConfirmService } from '../../core'
13 import { VideoDownloadComponent } from './video-download.component'
14 import { VideoShareComponent } from './video-share.component'
15 import { VideoReportComponent } from './video-report.component'
16 import { VideoDetails, VideoService, MarkdownService } from '../shared'
17 import { VideoBlacklistService } from '../../shared'
18 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
19 import { BehaviorSubject } from 'rxjs/BehaviorSubject'
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
35 player: videojs.Player
36 playerElement: HTMLMediaElement
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 = ''
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 this.paramsSub = this.route.params.subscribe(routeParams => {
66 let uuid = routeParams['uuid']
67 this.videoService.getVideo(uuid).subscribe(
68 video => this.onVideoFetched(video),
71 this.videoNotFound = true
79 // Remove player if it exists
80 if (this.videoPlayerLoaded === true) {
81 videojs(this.playerElement).dispose()
84 // Unsubscribe subscriptions
85 this.paramsSub.unsubscribe()
89 if (this.isUserLoggedIn() === false) return
90 // Already liked this video
91 if (this.userRating === 'like') return
93 this.videoService.setVideoLike(this.video.id)
96 // Update the video like attribute
97 this.updateVideoRating(this.userRating, 'like')
98 this.userRating = 'like'
101 err => this.notificationsService.error('Error', err.message)
106 if (this.isUserLoggedIn() === false) return
107 // Already disliked this video
108 if (this.userRating === 'dislike') return
110 this.videoService.setVideoDislike(this.video.id)
113 // Update the video dislike attribute
114 this.updateVideoRating(this.userRating, 'dislike')
115 this.userRating = 'dislike'
118 err => this.notificationsService.error('Error', err.message)
122 removeVideo (event: Event) {
123 event.preventDefault()
125 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
127 if (res === false) return
129 this.videoService.removeVideo(this.video.id)
132 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
133 // Go back to the video-list.
134 this.router.navigate(['/videos/list'])
137 error => this.notificationsService.error('Error', error.text)
143 blacklistVideo (event: Event) {
144 event.preventDefault()
146 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
148 if (res === false) return
150 this.videoBlacklistService.blacklistVideo(this.video.id)
153 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
154 this.router.navigate(['/videos/list'])
157 error => this.notificationsService.error('Error', error.text)
163 showMoreDescription () {
164 if (this.completeVideoDescription === undefined) {
165 return this.loadCompleteDescription()
168 this.updateVideoDescription(this.completeVideoDescription)
169 this.completeDescriptionShown = true
172 showLessDescription () {
174 this.updateVideoDescription(this.shortVideoDescription)
175 this.completeDescriptionShown = false
178 loadCompleteDescription () {
179 this.descriptionLoading = true
181 this.videoService.loadCompleteDescription(this.video.descriptionPath)
184 this.completeDescriptionShown = true
185 this.descriptionLoading = false
187 this.shortVideoDescription = this.video.description
188 this.completeVideoDescription = description
190 this.updateVideoDescription(this.completeVideoDescription)
194 this.descriptionLoading = false
195 this.notificationsService.error('Error', error.text)
200 showReportModal (event: Event) {
201 event.preventDefault()
202 this.videoReportModal.show()
206 this.videoShareModal.show()
209 showDownloadModal (event: Event) {
210 event.preventDefault()
211 this.videoDownloadModal.show()
215 return this.authService.isLoggedIn()
218 canUserUpdateVideo () {
219 return this.video.isUpdatableBy(this.authService.getUser())
222 isVideoRemovable () {
223 return this.video.isRemovableBy(this.authService.getUser())
226 isVideoBlacklistable () {
227 return this.video.isBlackistableBy(this.authService.getUser())
230 private updateVideoDescription (description: string) {
231 this.video.description = description
232 this.setVideoDescriptionHTML()
235 private setVideoDescriptionHTML () {
236 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
239 private handleError (err: any) {
240 const errorMessage: string = typeof err === 'string' ? err : err.message
243 if (errorMessage.indexOf('http error') !== -1) {
244 message = 'Cannot fetch video from server, maybe down.'
246 message = errorMessage
249 this.notificationsService.error('Error', message)
252 private checkUserRating () {
253 // Unlogged users do not have ratings
254 if (this.isUserLoggedIn() === false) return
256 this.videoService.getUserVideoRating(this.video.id)
260 this.userRating = ratingObject.rating
264 err => this.notificationsService.error('Error', err.message)
268 private onVideoFetched (video: VideoDetails) {
272 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
273 observable = this.confirmService.confirm(
274 'This video contains mature or explicit content. Are you sure you want to watch it?',
275 'Mature or explicit content'
278 observable = Observable.of(true)
281 observable.subscribe(
285 return this.router.navigate([ '/videos/list' ])
288 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
290 const videojsOptions = {
295 videoFiles: this.video.files,
296 playerElement: this.playerElement,
303 this.videoPlayerLoaded = true
306 videojs(this.playerElement, videojsOptions, function () {
308 this.on('customError', (event, data) => {
309 self.handleError(data.err)
312 this.on('torrentInfo', (event, data) => {
313 self.downloadSpeed = data.downloadSpeed
314 self.numPeers = data.numPeers
315 self.uploadSpeed = data.uploadSpeed
319 this.setVideoDescriptionHTML()
321 this.setOpenGraphTags()
322 this.checkUserRating()
327 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
328 let likesToIncrement = 0
329 let dislikesToIncrement = 0
332 if (oldRating === 'like') likesToIncrement--
333 if (oldRating === 'dislike') dislikesToIncrement--
336 if (newRating === 'like') likesToIncrement++
337 if (newRating === 'dislike') dislikesToIncrement++
339 this.video.likes += likesToIncrement
340 this.video.dislikes += dislikesToIncrement
343 private setOpenGraphTags () {
344 this.metaService.setTitle(this.video.name)
346 this.metaService.setTag('og:type', 'video')
348 this.metaService.setTag('og:title', this.video.name)
349 this.metaService.setTag('name', this.video.name)
351 this.metaService.setTag('og:description', this.video.description)
352 this.metaService.setTag('description', this.video.description)
354 this.metaService.setTag('og:image', this.video.previewPath)
356 this.metaService.setTag('og:duration', this.video.duration.toString())
358 this.metaService.setTag('og:site_name', 'PeerTube')
360 this.metaService.setTag('og:url', window.location.href)
361 this.metaService.setTag('url', window.location.href)