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 { 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 { MarkdownService } from '../shared'
17 import { VideoDownloadComponent } from './video-download.component'
18 import { VideoReportComponent } from './video-report.component'
19 import { VideoShareComponent } from './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: HTMLMediaElement
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 = ''
48 private paramsSub: Subscription
51 private elementRef: ElementRef,
52 private route: ActivatedRoute,
53 private router: Router,
54 private videoService: VideoService,
55 private videoBlacklistService: VideoBlacklistService,
56 private confirmService: ConfirmService,
57 private metaService: MetaService,
58 private authService: AuthService,
59 private notificationsService: NotificationsService,
60 private markdownService: MarkdownService
64 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
66 data => this.otherVideos = data.videos,
68 err => console.error(err)
71 this.paramsSub = this.route.params.subscribe(routeParams => {
72 let uuid = routeParams['uuid']
73 this.videoService.getVideo(uuid).subscribe(
74 video => this.onVideoFetched(video),
77 this.videoNotFound = true
85 // Remove player if it exists
86 if (this.videoPlayerLoaded === true) {
87 videojs(this.playerElement).dispose()
90 // Unsubscribe subscriptions
91 this.paramsSub.unsubscribe()
95 if (this.isUserLoggedIn() === false) return
96 // Already liked this video
97 if (this.userRating === 'like') return
99 this.videoService.setVideoLike(this.video.id)
102 // Update the video like attribute
103 this.updateVideoRating(this.userRating, 'like')
104 this.userRating = 'like'
107 err => this.notificationsService.error('Error', err.message)
112 if (this.isUserLoggedIn() === false) return
113 // Already disliked this video
114 if (this.userRating === 'dislike') return
116 this.videoService.setVideoDislike(this.video.id)
119 // Update the video dislike attribute
120 this.updateVideoRating(this.userRating, 'dislike')
121 this.userRating = 'dislike'
124 err => this.notificationsService.error('Error', err.message)
128 blacklistVideo (event: Event) {
129 event.preventDefault()
131 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
133 if (res === false) return
135 this.videoBlacklistService.blacklistVideo(this.video.id)
138 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
139 this.router.navigate(['/videos/list'])
142 error => this.notificationsService.error('Error', error.text)
148 showMoreDescription () {
149 if (this.completeVideoDescription === undefined) {
150 return this.loadCompleteDescription()
153 this.updateVideoDescription(this.completeVideoDescription)
154 this.completeDescriptionShown = true
157 showLessDescription () {
158 this.updateVideoDescription(this.shortVideoDescription)
159 this.completeDescriptionShown = false
162 loadCompleteDescription () {
163 this.descriptionLoading = true
165 this.videoService.loadCompleteDescription(this.video.descriptionPath)
168 this.completeDescriptionShown = true
169 this.descriptionLoading = false
171 this.shortVideoDescription = this.video.description
172 this.completeVideoDescription = description
174 this.updateVideoDescription(this.completeVideoDescription)
178 this.descriptionLoading = false
179 this.notificationsService.error('Error', error.text)
184 showReportModal (event: Event) {
185 event.preventDefault()
186 this.videoReportModal.show()
190 this.videoShareModal.show()
193 showDownloadModal (event: Event) {
194 event.preventDefault()
195 this.videoDownloadModal.show()
199 return this.authService.isLoggedIn()
202 isVideoBlacklistable () {
203 return this.video.isBlackistableBy(this.authService.getUser())
207 return Account.GET_ACCOUNT_AVATAR_PATH(this.video.account)
211 if (!this.video || Array.isArray(this.video.tags) === false) return []
213 return this.video.tags.join(', ')
216 private updateVideoDescription (description: string) {
217 this.video.description = description
218 this.setVideoDescriptionHTML()
221 private setVideoDescriptionHTML () {
222 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
225 private handleError (err: any) {
226 const errorMessage: string = typeof err === 'string' ? err : err.message
229 if (errorMessage.indexOf('http error') !== -1) {
230 message = 'Cannot fetch video from server, maybe down.'
232 message = errorMessage
235 this.notificationsService.error('Error', message)
238 private checkUserRating () {
239 // Unlogged users do not have ratings
240 if (this.isUserLoggedIn() === false) return
242 this.videoService.getUserVideoRating(this.video.id)
246 this.userRating = ratingObject.rating
250 err => this.notificationsService.error('Error', err.message)
254 private onVideoFetched (video: VideoDetails) {
258 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
259 observable = this.confirmService.confirm(
260 'This video contains mature or explicit content. Are you sure you want to watch it?',
261 'Mature or explicit content'
264 observable = Observable.of(true)
267 observable.subscribe(
271 return this.router.navigate([ '/videos/list' ])
274 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
276 const videojsOptions = {
281 videoFiles: this.video.files,
282 playerElement: this.playerElement,
289 this.videoPlayerLoaded = true
292 videojs(this.playerElement, videojsOptions, function () {
294 this.on('customError', (event, data) => {
295 self.handleError(data.err)
299 this.setVideoDescriptionHTML()
301 this.setOpenGraphTags()
302 this.checkUserRating()
304 this.prepareViewAdd()
309 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
310 let likesToIncrement = 0
311 let dislikesToIncrement = 0
314 if (oldRating === 'like') likesToIncrement--
315 if (oldRating === 'dislike') dislikesToIncrement--
318 if (newRating === 'like') likesToIncrement++
319 if (newRating === 'dislike') dislikesToIncrement++
321 this.video.likes += likesToIncrement
322 this.video.dislikes += dislikesToIncrement
325 private setOpenGraphTags () {
326 this.metaService.setTitle(this.video.name)
328 this.metaService.setTag('og:type', 'video')
330 this.metaService.setTag('og:title', this.video.name)
331 this.metaService.setTag('name', this.video.name)
333 this.metaService.setTag('og:description', this.video.description)
334 this.metaService.setTag('description', this.video.description)
336 this.metaService.setTag('og:image', this.video.previewPath)
338 this.metaService.setTag('og:duration', this.video.duration.toString())
340 this.metaService.setTag('og:site_name', 'PeerTube')
342 this.metaService.setTag('og:url', window.location.href)
343 this.metaService.setTag('url', window.location.href)
346 private prepareViewAdd () {
347 // After 30 seconds (or 3/4 of the video), increment add a view
348 let viewTimeoutSeconds = 30
349 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
353 .viewVideo(this.video.uuid)
356 }, viewTimeoutSeconds * 1000)