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 if (this.videoPlayerLoaded) {
76 let uuid = routeParams['uuid']
77 this.videoService.getVideo(uuid).subscribe(
78 video => this.onVideoFetched(video),
81 this.videoNotFound = true
89 // Remove player if it exists
90 if (this.videoPlayerLoaded === true) {
91 videojs(this.playerElement).dispose()
94 // Unsubscribe subscriptions
95 this.paramsSub.unsubscribe()
99 if (this.isUserLoggedIn() === false) return
100 // Already liked this video
101 if (this.userRating === 'like') return
103 this.videoService.setVideoLike(this.video.id)
106 // Update the video like attribute
107 this.updateVideoRating(this.userRating, 'like')
108 this.userRating = 'like'
111 err => this.notificationsService.error('Error', err.message)
116 if (this.isUserLoggedIn() === false) return
117 // Already disliked this video
118 if (this.userRating === 'dislike') return
120 this.videoService.setVideoDislike(this.video.id)
123 // Update the video dislike attribute
124 this.updateVideoRating(this.userRating, 'dislike')
125 this.userRating = 'dislike'
128 err => this.notificationsService.error('Error', err.message)
132 blacklistVideo (event: Event) {
133 event.preventDefault()
135 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
137 if (res === false) return
139 this.videoBlacklistService.blacklistVideo(this.video.id)
142 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
143 this.router.navigate(['/videos/list'])
146 error => this.notificationsService.error('Error', error.text)
152 showMoreDescription () {
153 if (this.completeVideoDescription === undefined) {
154 return this.loadCompleteDescription()
157 this.updateVideoDescription(this.completeVideoDescription)
158 this.completeDescriptionShown = true
161 showLessDescription () {
162 this.updateVideoDescription(this.shortVideoDescription)
163 this.completeDescriptionShown = false
166 loadCompleteDescription () {
167 this.descriptionLoading = true
169 this.videoService.loadCompleteDescription(this.video.descriptionPath)
172 this.completeDescriptionShown = true
173 this.descriptionLoading = false
175 this.shortVideoDescription = this.video.description
176 this.completeVideoDescription = description
178 this.updateVideoDescription(this.completeVideoDescription)
182 this.descriptionLoading = false
183 this.notificationsService.error('Error', error.text)
188 showReportModal (event: Event) {
189 event.preventDefault()
190 this.videoReportModal.show()
194 this.videoShareModal.show()
197 showDownloadModal (event: Event) {
198 event.preventDefault()
199 this.videoDownloadModal.show()
203 return this.authService.isLoggedIn()
206 isVideoBlacklistable () {
207 return this.video.isBlackistableBy(this.authService.getUser())
211 return Account.GET_ACCOUNT_AVATAR_PATH(this.video.account)
215 if (!this.video || Array.isArray(this.video.tags) === false) return []
217 return this.video.tags.join(', ')
220 private updateVideoDescription (description: string) {
221 this.video.description = description
222 this.setVideoDescriptionHTML()
225 private setVideoDescriptionHTML () {
226 if (!this.video.description) {
227 this.videoHTMLDescription = ''
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 // Player was already loaded
284 if (this.videoPlayerLoaded !== true) {
285 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
287 const videojsOptions = {
292 videoFiles: this.video.files,
293 playerElement: this.playerElement,
300 this.videoPlayerLoaded = true
303 videojs(this.playerElement, videojsOptions, function () {
305 this.on('customError', (event, data) => {
306 self.handleError(data.err)
310 (this.player as any).setVideoFiles(this.video.files)
313 this.setVideoDescriptionHTML()
315 this.setOpenGraphTags()
316 this.checkUserRating()
318 this.prepareViewAdd()
323 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
324 let likesToIncrement = 0
325 let dislikesToIncrement = 0
328 if (oldRating === 'like') likesToIncrement--
329 if (oldRating === 'dislike') dislikesToIncrement--
332 if (newRating === 'like') likesToIncrement++
333 if (newRating === 'dislike') dislikesToIncrement++
335 this.video.likes += likesToIncrement
336 this.video.dislikes += dislikesToIncrement
339 private setOpenGraphTags () {
340 this.metaService.setTitle(this.video.name)
342 this.metaService.setTag('og:type', 'video')
344 this.metaService.setTag('og:title', this.video.name)
345 this.metaService.setTag('name', this.video.name)
347 this.metaService.setTag('og:description', this.video.description)
348 this.metaService.setTag('description', this.video.description)
350 this.metaService.setTag('og:image', this.video.previewPath)
352 this.metaService.setTag('og:duration', this.video.duration.toString())
354 this.metaService.setTag('og:site_name', 'PeerTube')
356 this.metaService.setTag('og:url', window.location.href)
357 this.metaService.setTag('url', window.location.href)
360 private prepareViewAdd () {
361 // After 30 seconds (or 3/4 of the video), increment add a view
362 let viewTimeoutSeconds = 30
363 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
367 .viewVideo(this.video.uuid)
370 }, viewTimeoutSeconds * 1000)