1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { RedirectService } from '@app/core/routing/redirect.service'
4 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
5 import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
6 import { MetaService } from '@ngx-meta/core'
7 import { NotificationsService } from 'angular2-notifications'
8 import { Subscription } from 'rxjs/Subscription'
9 import * as videojs from 'video.js'
10 import 'videojs-hotkeys'
11 import * as WebTorrent from 'webtorrent'
12 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
13 import '../../../assets/player/peertube-videojs-plugin'
14 import { AuthService, ConfirmService } from '../../core'
15 import { VideoBlacklistService } from '../../shared'
16 import { Account } from '../../shared/account/account.model'
17 import { VideoDetails } from '../../shared/video/video-details.model'
18 import { Video } from '../../shared/video/video.model'
19 import { VideoService } from '../../shared/video/video.service'
20 import { MarkdownService } from '../shared'
21 import { VideoDownloadComponent } from './modal/video-download.component'
22 import { VideoReportComponent } from './modal/video-report.component'
23 import { VideoShareComponent } from './modal/video-share.component'
24 import { getVideojsOptions } from '../../../assets/player/peertube-player'
27 selector: 'my-video-watch',
28 templateUrl: './video-watch.component.html',
29 styleUrls: [ './video-watch.component.scss' ]
31 export class VideoWatchComponent implements OnInit, OnDestroy {
32 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
34 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
35 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
36 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
37 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
39 otherVideosDisplayed: Video[] = []
42 player: videojs.Player
43 playerElement: HTMLVideoElement
44 userRating: UserVideoRateType = null
45 video: VideoDetails = null
46 videoPlayerLoaded = false
48 descriptionLoading = false
50 completeDescriptionShown = false
51 completeVideoDescription: string
52 shortVideoDescription: string
53 videoHTMLDescription = ''
54 likesBarTooltipText = ''
55 hasAlreadyAcceptedPrivacyConcern = false
57 private otherVideos: Video[] = []
58 private paramsSub: Subscription
59 private videojsInstance: videojs.Player
62 private elementRef: ElementRef,
63 private route: ActivatedRoute,
64 private router: Router,
65 private videoService: VideoService,
66 private videoBlacklistService: VideoBlacklistService,
67 private confirmService: ConfirmService,
68 private metaService: MetaService,
69 private authService: AuthService,
70 private notificationsService: NotificationsService,
71 private markdownService: MarkdownService,
73 private redirectService: RedirectService
77 return this.authService.getUser()
82 WebTorrent.WEBRTC_SUPPORT === false ||
83 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
85 this.hasAlreadyAcceptedPrivacyConcern = true
88 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
91 this.otherVideos = data.videos
92 this.updateOtherVideosDisplayed()
95 err => console.error(err)
98 this.paramsSub = this.route.params.subscribe(routeParams => {
99 if (this.videoPlayerLoaded) {
103 const uuid = routeParams['uuid']
104 // Video did not changed
105 if (this.video && this.video.uuid === uuid) return
107 this.videoService.getVideo(uuid).subscribe(
108 video => this.onVideoFetched(video),
111 this.videoNotFound = true
119 // Remove player if it exists
120 if (this.videoPlayerLoaded === true) {
121 videojs(this.playerElement).dispose()
124 // Unsubscribe subscriptions
125 this.paramsSub.unsubscribe()
129 if (this.isUserLoggedIn() === false) return
130 if (this.userRating === 'like') {
131 // Already liked this video
132 this.setRating('none')
134 this.setRating('like')
139 if (this.isUserLoggedIn() === false) return
140 if (this.userRating === 'dislike') {
141 // Already disliked this video
142 this.setRating('none')
144 this.setRating('dislike')
148 async blacklistVideo (event: Event) {
149 event.preventDefault()
151 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
152 if (res === false) return
154 this.videoBlacklistService.blacklistVideo(this.video.id)
157 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
158 this.redirectService.redirectToHomepage()
161 error => this.notificationsService.error('Error', error.message)
165 showMoreDescription () {
166 if (this.completeVideoDescription === undefined) {
167 return this.loadCompleteDescription()
170 this.updateVideoDescription(this.completeVideoDescription)
171 this.completeDescriptionShown = true
174 showLessDescription () {
175 this.updateVideoDescription(this.shortVideoDescription)
176 this.completeDescriptionShown = false
179 loadCompleteDescription () {
180 this.descriptionLoading = true
182 this.videoService.loadCompleteDescription(this.video.descriptionPath)
185 this.completeDescriptionShown = true
186 this.descriptionLoading = false
188 this.shortVideoDescription = this.video.description
189 this.completeVideoDescription = description
191 this.updateVideoDescription(this.completeVideoDescription)
195 this.descriptionLoading = false
196 this.notificationsService.error('Error', error.message)
201 showReportModal (event: Event) {
202 event.preventDefault()
203 this.videoReportModal.show()
206 showSupportModal () {
207 this.videoSupportModal.show()
211 this.videoShareModal.show()
214 showDownloadModal (event: Event) {
215 event.preventDefault()
216 this.videoDownloadModal.show()
220 return this.authService.isLoggedIn()
223 isVideoUpdatable () {
224 return this.video.isUpdatableBy(this.authService.getUser())
227 isVideoBlacklistable () {
228 return this.video.isBlackistableBy(this.user)
232 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
236 if (!this.video) return ''
238 return this.video.previewUrl
242 if (!this.video || Array.isArray(this.video.tags) === false) return []
244 return this.video.tags.join(', ')
247 isVideoRemovable () {
248 return this.video.isRemovableBy(this.authService.getUser())
251 async removeVideo (event: Event) {
252 event.preventDefault()
254 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
255 if (res === false) return
257 this.videoService.removeVideo(this.video.id)
260 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
262 // Go back to the video-list.
263 this.redirectService.redirectToHomepage()
266 error => this.notificationsService.error('Error', error.message)
270 acceptedPrivacyConcern () {
271 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
272 this.hasAlreadyAcceptedPrivacyConcern = true
275 private updateVideoDescription (description: string) {
276 this.video.description = description
277 this.setVideoDescriptionHTML()
280 private setVideoDescriptionHTML () {
281 if (!this.video.description) {
282 this.videoHTMLDescription = ''
286 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
289 private setVideoLikesBarTooltipText () {
290 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
293 private handleError (err: any) {
294 const errorMessage: string = typeof err === 'string' ? err : err.message
295 if (!errorMessage) return
299 if (errorMessage.indexOf('http error') !== -1) {
300 message = 'Cannot fetch video from server, maybe down.'
302 message = errorMessage
305 this.notificationsService.error('Error', message)
308 private checkUserRating () {
309 // Unlogged users do not have ratings
310 if (this.isUserLoggedIn() === false) return
312 this.videoService.getUserVideoRating(this.video.id)
316 this.userRating = ratingObject.rating
320 err => this.notificationsService.error('Error', err.message)
324 private async onVideoFetched (video: VideoDetails) {
327 this.updateOtherVideosDisplayed()
329 if (this.video.isVideoNSFWForUser(this.user)) {
330 const res = await this.confirmService.confirm(
331 'This video contains mature or explicit content. Are you sure you want to watch it?',
332 'Mature or explicit content'
334 if (res === false) return this.redirectService.redirectToHomepage()
337 // Player was already loaded, remove old videojs
338 if (this.videojsInstance) {
339 this.videojsInstance.dispose()
340 this.videojsInstance = undefined
343 // Build video element, because videojs remove it on dispose
344 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
345 this.playerElement = document.createElement('video')
346 this.playerElement.className = 'video-js vjs-peertube-skin'
347 playerElementWrapper.appendChild(this.playerElement)
349 const videojsOptions = getVideojsOptions({
350 autoplay: this.isAutoplay(),
351 inactivityTimeout: 4000,
352 videoFiles: this.video.files,
353 playerElement: this.playerElement,
354 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
355 videoDuration: this.video.duration,
358 poster: this.video.previewUrl
361 this.videoPlayerLoaded = true
364 this.zone.runOutsideAngular(() => {
365 self.videojsInstance = videojs(this.playerElement, videojsOptions, function () {
367 this.on('customError', (event, data) => self.handleError(data.err))
371 this.setVideoDescriptionHTML()
372 this.setVideoLikesBarTooltipText()
374 this.setOpenGraphTags()
375 this.checkUserRating()
378 private setRating (nextRating) {
380 switch (nextRating) {
382 method = this.videoService.setVideoLike
385 method = this.videoService.setVideoDislike
388 method = this.videoService.unsetVideoLike
392 method.call(this.videoService, this.video.id)
395 // Update the video like attribute
396 this.updateVideoRating(this.userRating, nextRating)
397 this.userRating = nextRating
399 err => this.notificationsService.error('Error', err.message)
403 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
404 let likesToIncrement = 0
405 let dislikesToIncrement = 0
408 if (oldRating === 'like') likesToIncrement--
409 if (oldRating === 'dislike') dislikesToIncrement--
412 if (newRating === 'like') likesToIncrement++
413 if (newRating === 'dislike') dislikesToIncrement++
415 this.video.likes += likesToIncrement
416 this.video.dislikes += dislikesToIncrement
418 this.video.buildLikeAndDislikePercents()
419 this.setVideoLikesBarTooltipText()
422 private updateOtherVideosDisplayed () {
423 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
424 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
428 private setOpenGraphTags () {
429 this.metaService.setTitle(this.video.name)
431 this.metaService.setTag('og:type', 'video')
433 this.metaService.setTag('og:title', this.video.name)
434 this.metaService.setTag('name', this.video.name)
436 this.metaService.setTag('og:description', this.video.description)
437 this.metaService.setTag('description', this.video.description)
439 this.metaService.setTag('og:image', this.video.previewPath)
441 this.metaService.setTag('og:duration', this.video.duration.toString())
443 this.metaService.setTag('og:site_name', 'PeerTube')
445 this.metaService.setTag('og:url', window.location.href)
446 this.metaService.setTag('url', window.location.href)
449 private isAutoplay () {
451 if (!this.user) return true
453 // Be sure the autoPlay is set to false
454 return this.user.autoPlayVideo !== false