1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
4 import { MetaService } from '@ngx-meta/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { Observable } from 'rxjs/Observable'
7 import { Subscription } from 'rxjs/Subscription'
8 import * as videojs from 'video.js'
9 import 'videojs-hotkeys'
10 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
11 import '../../../assets/player/peertube-videojs-plugin'
12 import { AuthService, ConfirmService } from '../../core'
13 import { VideoBlacklistService } from '../../shared'
14 import { Account } from '../../shared/account/account.model'
15 import { VideoDetails } from '../../shared/video/video-details.model'
16 import { Video } from '../../shared/video/video.model'
17 import { VideoService } from '../../shared/video/video.service'
18 import { MarkdownService } from '../shared'
19 import { VideoDownloadComponent } from './modal/video-download.component'
20 import { VideoReportComponent } from './modal/video-report.component'
21 import { VideoShareComponent } from './modal/video-share.component'
24 selector: 'my-video-watch',
25 templateUrl: './video-watch.component.html',
26 styleUrls: [ './video-watch.component.scss' ]
28 export class VideoWatchComponent implements OnInit, OnDestroy {
29 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
31 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
32 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
33 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
34 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
36 otherVideosDisplayed: Video[] = []
39 player: videojs.Player
40 playerElement: HTMLVideoElement
41 userRating: UserVideoRateType = null
42 video: VideoDetails = null
43 videoPlayerLoaded = false
45 descriptionLoading = false
47 completeDescriptionShown = false
48 completeVideoDescription: string
49 shortVideoDescription: string
50 videoHTMLDescription = ''
51 likesBarTooltipText = ''
53 private otherVideos: Video[] = []
54 private paramsSub: Subscription
57 private elementRef: ElementRef,
58 private route: ActivatedRoute,
59 private router: Router,
60 private videoService: VideoService,
61 private videoBlacklistService: VideoBlacklistService,
62 private confirmService: ConfirmService,
63 private metaService: MetaService,
64 private authService: AuthService,
65 private notificationsService: NotificationsService,
66 private markdownService: MarkdownService,
71 return this.authService.getUser()
75 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
78 this.otherVideos = data.videos
79 this.updateOtherVideosDisplayed()
82 err => console.error(err)
85 this.paramsSub = this.route.params.subscribe(routeParams => {
86 if (this.videoPlayerLoaded) {
90 const uuid = routeParams['uuid']
91 // Video did not changed
92 if (this.video && this.video.uuid === uuid) return
94 this.videoService.getVideo(uuid).subscribe(
95 video => this.onVideoFetched(video),
98 this.videoNotFound = true
106 // Remove player if it exists
107 if (this.videoPlayerLoaded === true) {
108 videojs(this.playerElement).dispose()
111 // Unsubscribe subscriptions
112 this.paramsSub.unsubscribe()
116 if (this.isUserLoggedIn() === false) return
117 if (this.userRating === 'like') {
118 // Already liked this video
119 this.setRating('none')
121 this.setRating('like')
126 if (this.isUserLoggedIn() === false) return
127 if (this.userRating === 'dislike') {
128 // Already disliked this video
129 this.setRating('none')
131 this.setRating('dislike')
135 async blacklistVideo (event: Event) {
136 event.preventDefault()
138 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
139 if (res === false) return
141 this.videoBlacklistService.blacklistVideo(this.video.id)
144 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
145 this.router.navigate(['/videos/list'])
148 error => this.notificationsService.error('Error', error.message)
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.message)
188 showReportModal (event: Event) {
189 event.preventDefault()
190 this.videoReportModal.show()
193 showSupportModal () {
194 this.videoSupportModal.show()
198 this.videoShareModal.show()
201 showDownloadModal (event: Event) {
202 event.preventDefault()
203 this.videoDownloadModal.show()
207 return this.authService.isLoggedIn()
210 isVideoUpdatable () {
211 return this.video.isUpdatableBy(this.authService.getUser())
214 isVideoBlacklistable () {
215 return this.video.isBlackistableBy(this.user)
219 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
223 if (!this.video) return ''
225 return this.video.previewUrl
229 if (!this.video || Array.isArray(this.video.tags) === false) return []
231 return this.video.tags.join(', ')
234 isVideoRemovable () {
235 return this.video.isRemovableBy(this.authService.getUser())
238 async removeVideo (event: Event) {
239 event.preventDefault()
241 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
242 if (res === false) return
244 this.videoService.removeVideo(this.video.id)
247 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
249 // Go back to the video-list.
250 this.router.navigate([ '/videos/list' ])
253 error => this.notificationsService.error('Error', error.message)
257 private updateVideoDescription (description: string) {
258 this.video.description = description
259 this.setVideoDescriptionHTML()
262 private setVideoDescriptionHTML () {
263 if (!this.video.description) {
264 this.videoHTMLDescription = ''
268 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
271 private setVideoLikesBarTooltipText () {
272 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
275 private handleError (err: any) {
276 const errorMessage: string = typeof err === 'string' ? err : err.message
277 if (!errorMessage) return
281 if (errorMessage.indexOf('http error') !== -1) {
282 message = 'Cannot fetch video from server, maybe down.'
284 message = errorMessage
287 this.notificationsService.error('Error', message)
290 private checkUserRating () {
291 // Unlogged users do not have ratings
292 if (this.isUserLoggedIn() === false) return
294 this.videoService.getUserVideoRating(this.video.id)
298 this.userRating = ratingObject.rating
302 err => this.notificationsService.error('Error', err.message)
306 private async onVideoFetched (video: VideoDetails) {
309 this.updateOtherVideosDisplayed()
311 if (this.video.isVideoNSFWForUser(this.user)) {
312 const res = await this.confirmService.confirm(
313 'This video contains mature or explicit content. Are you sure you want to watch it?',
314 'Mature or explicit content'
316 if (res === false) return this.router.navigate([ '/videos/list' ])
319 if (!this.hasAlreadyAcceptedPrivacyConcern()) {
320 const res = await this.confirmService.confirm(
321 'PeerTube uses P2P, other may know you are watching that video through your public IP address. ' +
322 'Are you okay with that?',
326 if (res === false) return this.router.navigate([ '/videos/list' ])
329 this.acceptedPrivacyConcern()
331 // Player was already loaded
332 if (this.videoPlayerLoaded !== true) {
333 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
335 // If autoplay is true, we don't really need a poster
336 if (this.isAutoplay() === false) {
337 this.playerElement.poster = this.video.previewUrl
340 const videojsOptions = {
342 autoplay: this.isAutoplay(),
343 playbackRates: [0.5, 1, 1.25, 1.5, 2],
346 videoFiles: this.video.files,
347 playerElement: this.playerElement,
349 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
350 videoDuration: this.video.duration
353 enableVolumeScroll: false
358 this.videoPlayerLoaded = true
361 this.zone.runOutsideAngular(() => {
362 videojs(this.playerElement, videojsOptions, function () {
364 this.on('customError', (event, data) => self.handleError(data.err))
368 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
369 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
372 this.setVideoDescriptionHTML()
373 this.setVideoLikesBarTooltipText()
375 this.setOpenGraphTags()
376 this.checkUserRating()
379 private setRating (nextRating) {
381 switch (nextRating) {
383 method = this.videoService.setVideoLike
386 method = this.videoService.setVideoDislike
389 method = this.videoService.unsetVideoLike
393 method.call(this.videoService, this.video.id)
396 // Update the video like attribute
397 this.updateVideoRating(this.userRating, nextRating)
398 this.userRating = nextRating
400 err => this.notificationsService.error('Error', err.message)
404 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
405 let likesToIncrement = 0
406 let dislikesToIncrement = 0
409 if (oldRating === 'like') likesToIncrement--
410 if (oldRating === 'dislike') dislikesToIncrement--
413 if (newRating === 'like') likesToIncrement++
414 if (newRating === 'dislike') dislikesToIncrement++
416 this.video.likes += likesToIncrement
417 this.video.dislikes += dislikesToIncrement
419 this.video.buildLikeAndDislikePercents()
420 this.setVideoLikesBarTooltipText()
423 private updateOtherVideosDisplayed () {
424 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
425 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
429 private setOpenGraphTags () {
430 this.metaService.setTitle(this.video.name)
432 this.metaService.setTag('og:type', 'video')
434 this.metaService.setTag('og:title', this.video.name)
435 this.metaService.setTag('name', this.video.name)
437 this.metaService.setTag('og:description', this.video.description)
438 this.metaService.setTag('description', this.video.description)
440 this.metaService.setTag('og:image', this.video.previewPath)
442 this.metaService.setTag('og:duration', this.video.duration.toString())
444 this.metaService.setTag('og:site_name', 'PeerTube')
446 this.metaService.setTag('og:url', window.location.href)
447 this.metaService.setTag('url', window.location.href)
450 private isAutoplay () {
452 if (!this.user) return true
454 // Be sure the autoPlay is set to false
455 return this.user.autoPlayVideo !== false
458 private hasAlreadyAcceptedPrivacyConcern () {
459 return localStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
462 private acceptedPrivacyConcern () {
463 localStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')