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 { VideoService } from 'app/shared/video/video.service'
6 import { Observable } from 'rxjs/Observable'
7 import { Subscription } from 'rxjs/Subscription'
8 import videojs from 'video.js'
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 { MarkdownService } from '../shared'
14 import { VideoDownloadComponent } from './video-download.component'
15 import { VideoReportComponent } from './video-report.component'
16 import { VideoShareComponent } from './video-share.component'
17 import { VideoDetails } from '../../shared/video/video-details.model'
20 selector: 'my-video-watch',
21 templateUrl: './video-watch.component.html',
22 styleUrls: [ './video-watch.component.scss' ]
24 export class VideoWatchComponent implements OnInit, OnDestroy {
25 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
26 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
27 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
31 player: videojs.Player
32 playerElement: HTMLMediaElement
33 userRating: UserVideoRateType = null
34 video: VideoDetails = null
35 videoPlayerLoaded = false
37 descriptionLoading = false
39 completeDescriptionShown = false
40 completeVideoDescription: string
41 shortVideoDescription: string
42 videoHTMLDescription = ''
44 private paramsSub: Subscription
47 private elementRef: ElementRef,
48 private route: ActivatedRoute,
49 private router: Router,
50 private videoService: VideoService,
51 private videoBlacklistService: VideoBlacklistService,
52 private confirmService: ConfirmService,
53 private metaService: MetaService,
54 private authService: AuthService,
55 private notificationsService: NotificationsService,
56 private markdownService: MarkdownService
60 this.paramsSub = this.route.params.subscribe(routeParams => {
61 let uuid = routeParams['uuid']
62 this.videoService.getVideo(uuid).subscribe(
63 video => this.onVideoFetched(video),
66 this.videoNotFound = true
74 // Remove player if it exists
75 if (this.videoPlayerLoaded === true) {
76 videojs(this.playerElement).dispose()
79 // Unsubscribe subscriptions
80 this.paramsSub.unsubscribe()
84 if (this.isUserLoggedIn() === false) return
85 // Already liked this video
86 if (this.userRating === 'like') return
88 this.videoService.setVideoLike(this.video.id)
91 // Update the video like attribute
92 this.updateVideoRating(this.userRating, 'like')
93 this.userRating = 'like'
96 err => this.notificationsService.error('Error', err.message)
101 if (this.isUserLoggedIn() === false) return
102 // Already disliked this video
103 if (this.userRating === 'dislike') return
105 this.videoService.setVideoDislike(this.video.id)
108 // Update the video dislike attribute
109 this.updateVideoRating(this.userRating, 'dislike')
110 this.userRating = 'dislike'
113 err => this.notificationsService.error('Error', err.message)
117 removeVideo (event: Event) {
118 event.preventDefault()
120 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
122 if (res === false) return
124 this.videoService.removeVideo(this.video.id)
127 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
128 // Go back to the video-list.
129 this.router.navigate(['/videos/list'])
132 error => this.notificationsService.error('Error', error.text)
138 blacklistVideo (event: Event) {
139 event.preventDefault()
141 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
143 if (res === false) return
145 this.videoBlacklistService.blacklistVideo(this.video.id)
148 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
149 this.router.navigate(['/videos/list'])
152 error => this.notificationsService.error('Error', error.text)
158 showMoreDescription () {
159 if (this.completeVideoDescription === undefined) {
160 return this.loadCompleteDescription()
163 this.updateVideoDescription(this.completeVideoDescription)
164 this.completeDescriptionShown = true
167 showLessDescription () {
169 this.updateVideoDescription(this.shortVideoDescription)
170 this.completeDescriptionShown = false
173 loadCompleteDescription () {
174 this.descriptionLoading = true
176 this.videoService.loadCompleteDescription(this.video.descriptionPath)
179 this.completeDescriptionShown = true
180 this.descriptionLoading = false
182 this.shortVideoDescription = this.video.description
183 this.completeVideoDescription = description
185 this.updateVideoDescription(this.completeVideoDescription)
189 this.descriptionLoading = false
190 this.notificationsService.error('Error', error.text)
195 showReportModal (event: Event) {
196 event.preventDefault()
197 this.videoReportModal.show()
201 this.videoShareModal.show()
204 showDownloadModal (event: Event) {
205 event.preventDefault()
206 this.videoDownloadModal.show()
210 return this.authService.isLoggedIn()
213 canUserUpdateVideo () {
214 return this.video.isUpdatableBy(this.authService.getUser())
217 isVideoRemovable () {
218 return this.video.isRemovableBy(this.authService.getUser())
221 isVideoBlacklistable () {
222 return this.video.isBlackistableBy(this.authService.getUser())
225 private updateVideoDescription (description: string) {
226 this.video.description = description
227 this.setVideoDescriptionHTML()
230 private setVideoDescriptionHTML () {
231 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
234 private handleError (err: any) {
235 const errorMessage: string = typeof err === 'string' ? err : err.message
238 if (errorMessage.indexOf('http error') !== -1) {
239 message = 'Cannot fetch video from server, maybe down.'
241 message = errorMessage
244 this.notificationsService.error('Error', message)
247 private checkUserRating () {
248 // Unlogged users do not have ratings
249 if (this.isUserLoggedIn() === false) return
251 this.videoService.getUserVideoRating(this.video.id)
255 this.userRating = ratingObject.rating
259 err => this.notificationsService.error('Error', err.message)
263 private onVideoFetched (video: VideoDetails) {
267 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
268 observable = this.confirmService.confirm(
269 'This video contains mature or explicit content. Are you sure you want to watch it?',
270 'Mature or explicit content'
273 observable = Observable.of(true)
276 observable.subscribe(
280 return this.router.navigate([ '/videos/list' ])
283 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
285 const videojsOptions = {
290 videoFiles: this.video.files,
291 playerElement: this.playerElement,
298 this.videoPlayerLoaded = true
301 videojs(this.playerElement, videojsOptions, function () {
303 this.on('customError', (event, data) => {
304 self.handleError(data.err)
308 this.setVideoDescriptionHTML()
310 this.setOpenGraphTags()
311 this.checkUserRating()
313 this.prepareViewAdd()
318 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
319 let likesToIncrement = 0
320 let dislikesToIncrement = 0
323 if (oldRating === 'like') likesToIncrement--
324 if (oldRating === 'dislike') dislikesToIncrement--
327 if (newRating === 'like') likesToIncrement++
328 if (newRating === 'dislike') dislikesToIncrement++
330 this.video.likes += likesToIncrement
331 this.video.dislikes += dislikesToIncrement
334 private setOpenGraphTags () {
335 this.metaService.setTitle(this.video.name)
337 this.metaService.setTag('og:type', 'video')
339 this.metaService.setTag('og:title', this.video.name)
340 this.metaService.setTag('name', this.video.name)
342 this.metaService.setTag('og:description', this.video.description)
343 this.metaService.setTag('description', this.video.description)
345 this.metaService.setTag('og:image', this.video.previewPath)
347 this.metaService.setTag('og:duration', this.video.duration.toString())
349 this.metaService.setTag('og:site_name', 'PeerTube')
351 this.metaService.setTag('og:url', window.location.href)
352 this.metaService.setTag('url', window.location.href)
355 private prepareViewAdd () {
356 // After 30 seconds (or 3/4 of the video), increment add a view
357 let viewTimeoutSeconds = 30
358 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
362 .viewVideo(this.video.uuid)
365 }, viewTimeoutSeconds * 1000)