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[] = []
43 player: videojs.Player
44 playerElement: HTMLVideoElement
45 userRating: UserVideoRateType = null
46 video: VideoDetails = null
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
61 private elementRef: ElementRef,
62 private route: ActivatedRoute,
63 private router: Router,
64 private videoService: VideoService,
65 private videoBlacklistService: VideoBlacklistService,
66 private confirmService: ConfirmService,
67 private metaService: MetaService,
68 private authService: AuthService,
69 private notificationsService: NotificationsService,
70 private markdownService: MarkdownService,
72 private redirectService: RedirectService
76 return this.authService.getUser()
81 WebTorrent.WEBRTC_SUPPORT === false ||
82 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
84 this.hasAlreadyAcceptedPrivacyConcern = true
87 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
90 this.otherVideos = data.videos
91 this.updateOtherVideosDisplayed()
94 err => console.error(err)
97 this.paramsSub = this.route.params.subscribe(routeParams => {
102 const uuid = routeParams['uuid']
103 // Video did not change
104 if (this.video && this.video.uuid === uuid) return
106 this.videoService.getVideo(uuid).subscribe(
108 const startTime = this.route.snapshot.queryParams.start
109 this.onVideoFetched(video, startTime)
110 .catch(err => this.handleError(err))
111 this.generateSyndicationList()
115 this.videoNotFound = true
125 // Unsubscribe subscriptions
126 this.paramsSub.unsubscribe()
130 if (this.isUserLoggedIn() === false) return
131 if (this.userRating === 'like') {
132 // Already liked this video
133 this.setRating('none')
135 this.setRating('like')
140 if (this.isUserLoggedIn() === false) return
141 if (this.userRating === 'dislike') {
142 // Already disliked this video
143 this.setRating('none')
145 this.setRating('dislike')
149 async blacklistVideo (event: Event) {
150 event.preventDefault()
152 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
153 if (res === false) return
155 this.videoBlacklistService.blacklistVideo(this.video.id)
158 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
159 this.redirectService.redirectToHomepage()
162 error => this.notificationsService.error('Error', error.message)
166 showMoreDescription () {
167 if (this.completeVideoDescription === undefined) {
168 return this.loadCompleteDescription()
171 this.updateVideoDescription(this.completeVideoDescription)
172 this.completeDescriptionShown = true
175 showLessDescription () {
176 this.updateVideoDescription(this.shortVideoDescription)
177 this.completeDescriptionShown = false
180 loadCompleteDescription () {
181 this.descriptionLoading = true
183 this.videoService.loadCompleteDescription(this.video.descriptionPath)
186 this.completeDescriptionShown = true
187 this.descriptionLoading = false
189 this.shortVideoDescription = this.video.description
190 this.completeVideoDescription = description
192 this.updateVideoDescription(this.completeVideoDescription)
196 this.descriptionLoading = false
197 this.notificationsService.error('Error', error.message)
202 showReportModal (event: Event) {
203 event.preventDefault()
204 this.videoReportModal.show()
207 showSupportModal () {
208 this.videoSupportModal.show()
212 this.videoShareModal.show()
215 showDownloadModal (event: Event) {
216 event.preventDefault()
217 this.videoDownloadModal.show()
221 return this.authService.isLoggedIn()
224 isVideoUpdatable () {
225 return this.video.isUpdatableBy(this.authService.getUser())
228 isVideoBlacklistable () {
229 return this.video.isBlackistableBy(this.user)
233 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
237 if (!this.video) return ''
239 return this.video.previewUrl
243 if (!this.video || Array.isArray(this.video.tags) === false) return []
245 return this.video.tags.join(', ')
248 generateSyndicationList () {
249 this.syndicationItems = this.videoService.getAccountFeedUrls(this.video.account.id)
252 isVideoRemovable () {
253 return this.video.isRemovableBy(this.authService.getUser())
256 async removeVideo (event: Event) {
257 event.preventDefault()
259 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
260 if (res === false) return
262 this.videoService.removeVideo(this.video.id)
265 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
267 // Go back to the video-list.
268 this.redirectService.redirectToHomepage()
271 error => this.notificationsService.error('Error', error.message)
275 acceptedPrivacyConcern () {
276 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
277 this.hasAlreadyAcceptedPrivacyConcern = true
280 private updateVideoDescription (description: string) {
281 this.video.description = description
282 this.setVideoDescriptionHTML()
285 private setVideoDescriptionHTML () {
286 if (!this.video.description) {
287 this.videoHTMLDescription = ''
291 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
294 private setVideoLikesBarTooltipText () {
295 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
298 private handleError (err: any) {
299 const errorMessage: string = typeof err === 'string' ? err : err.message
300 if (!errorMessage) return
304 if (errorMessage.indexOf('http error') !== -1) {
305 message = 'Cannot fetch video from server, maybe down.'
307 message = errorMessage
310 this.notificationsService.error('Error', message)
313 private checkUserRating () {
314 // Unlogged users do not have ratings
315 if (this.isUserLoggedIn() === false) return
317 this.videoService.getUserVideoRating(this.video.id)
321 this.userRating = ratingObject.rating
325 err => this.notificationsService.error('Error', err.message)
329 private async onVideoFetched (video: VideoDetails, startTime = 0) {
332 // Re init attributes
333 this.descriptionLoading = false
334 this.completeDescriptionShown = false
336 this.updateOtherVideosDisplayed()
338 if (this.video.isVideoNSFWForUser(this.user)) {
339 const res = await this.confirmService.confirm(
340 'This video contains mature or explicit content. Are you sure you want to watch it?',
341 'Mature or explicit content'
343 if (res === false) return this.redirectService.redirectToHomepage()
346 // Flush old player if needed
349 // Build video element, because videojs remove it on dispose
350 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
351 this.playerElement = document.createElement('video')
352 this.playerElement.className = 'video-js vjs-peertube-skin'
353 playerElementWrapper.appendChild(this.playerElement)
355 const videojsOptions = getVideojsOptions({
356 autoplay: this.isAutoplay(),
357 inactivityTimeout: 2500,
358 videoFiles: this.video.files,
359 playerElement: this.playerElement,
360 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
361 videoDuration: this.video.duration,
364 poster: this.video.previewUrl,
369 this.zone.runOutsideAngular(() => {
370 videojs(this.playerElement, videojsOptions, function () {
372 this.on('customError', (event, data) => self.handleError(data.err))
376 this.setVideoDescriptionHTML()
377 this.setVideoLikesBarTooltipText()
379 this.setOpenGraphTags()
380 this.checkUserRating()
383 private setRating (nextRating) {
385 switch (nextRating) {
387 method = this.videoService.setVideoLike
390 method = this.videoService.setVideoDislike
393 method = this.videoService.unsetVideoLike
397 method.call(this.videoService, this.video.id)
400 // Update the video like attribute
401 this.updateVideoRating(this.userRating, nextRating)
402 this.userRating = nextRating
404 err => this.notificationsService.error('Error', err.message)
408 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
409 let likesToIncrement = 0
410 let dislikesToIncrement = 0
413 if (oldRating === 'like') likesToIncrement--
414 if (oldRating === 'dislike') dislikesToIncrement--
417 if (newRating === 'like') likesToIncrement++
418 if (newRating === 'dislike') dislikesToIncrement++
420 this.video.likes += likesToIncrement
421 this.video.dislikes += dislikesToIncrement
423 this.video.buildLikeAndDislikePercents()
424 this.setVideoLikesBarTooltipText()
427 private updateOtherVideosDisplayed () {
428 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
429 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
433 private setOpenGraphTags () {
434 this.metaService.setTitle(this.video.name)
436 this.metaService.setTag('og:type', 'video')
438 this.metaService.setTag('og:title', this.video.name)
439 this.metaService.setTag('name', this.video.name)
441 this.metaService.setTag('og:description', this.video.description)
442 this.metaService.setTag('description', this.video.description)
444 this.metaService.setTag('og:image', this.video.previewPath)
446 this.metaService.setTag('og:duration', this.video.duration.toString())
448 this.metaService.setTag('og:site_name', 'PeerTube')
450 this.metaService.setTag('og:url', window.location.href)
451 this.metaService.setTag('url', window.location.href)
454 private isAutoplay () {
456 if (!this.user) return true
458 // Be sure the autoPlay is set to false
459 return this.user.autoPlayVideo !== false
462 private flushPlayer () {
463 // Remove player if it exists
465 this.player.dispose()
466 this.player = undefined