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[] = []
41 player: videojs.Player
42 playerElement: HTMLVideoElement
43 userRating: UserVideoRateType = null
44 video: VideoDetails = null
46 descriptionLoading = false
48 completeDescriptionShown = false
49 completeVideoDescription: string
50 shortVideoDescription: string
51 videoHTMLDescription = ''
52 likesBarTooltipText = ''
53 hasAlreadyAcceptedPrivacyConcern = false
55 private otherVideos: Video[] = []
56 private paramsSub: Subscription
59 private elementRef: ElementRef,
60 private route: ActivatedRoute,
61 private router: Router,
62 private videoService: VideoService,
63 private videoBlacklistService: VideoBlacklistService,
64 private confirmService: ConfirmService,
65 private metaService: MetaService,
66 private authService: AuthService,
67 private notificationsService: NotificationsService,
68 private markdownService: MarkdownService,
70 private redirectService: RedirectService
74 return this.authService.getUser()
79 WebTorrent.WEBRTC_SUPPORT === false ||
80 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
82 this.hasAlreadyAcceptedPrivacyConcern = true
85 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
88 this.otherVideos = data.videos
89 this.updateOtherVideosDisplayed()
92 err => console.error(err)
95 this.paramsSub = this.route.params.subscribe(routeParams => {
100 const uuid = routeParams['uuid']
101 // Video did not changed
102 if (this.video && this.video.uuid === uuid) return
104 this.videoService.getVideo(uuid).subscribe(
106 const startTime = this.route.snapshot.queryParams.start
107 this.onVideoFetched(video, startTime)
108 .catch(err => this.handleError(err))
112 this.videoNotFound = true
122 // Unsubscribe subscriptions
123 this.paramsSub.unsubscribe()
127 if (this.isUserLoggedIn() === false) return
128 if (this.userRating === 'like') {
129 // Already liked this video
130 this.setRating('none')
132 this.setRating('like')
137 if (this.isUserLoggedIn() === false) return
138 if (this.userRating === 'dislike') {
139 // Already disliked this video
140 this.setRating('none')
142 this.setRating('dislike')
146 async blacklistVideo (event: Event) {
147 event.preventDefault()
149 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
150 if (res === false) return
152 this.videoBlacklistService.blacklistVideo(this.video.id)
155 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
156 this.redirectService.redirectToHomepage()
159 error => this.notificationsService.error('Error', error.message)
163 showMoreDescription () {
164 if (this.completeVideoDescription === undefined) {
165 return this.loadCompleteDescription()
168 this.updateVideoDescription(this.completeVideoDescription)
169 this.completeDescriptionShown = true
172 showLessDescription () {
173 this.updateVideoDescription(this.shortVideoDescription)
174 this.completeDescriptionShown = false
177 loadCompleteDescription () {
178 this.descriptionLoading = true
180 this.videoService.loadCompleteDescription(this.video.descriptionPath)
183 this.completeDescriptionShown = true
184 this.descriptionLoading = false
186 this.shortVideoDescription = this.video.description
187 this.completeVideoDescription = description
189 this.updateVideoDescription(this.completeVideoDescription)
193 this.descriptionLoading = false
194 this.notificationsService.error('Error', error.message)
199 showReportModal (event: Event) {
200 event.preventDefault()
201 this.videoReportModal.show()
204 showSupportModal () {
205 this.videoSupportModal.show()
209 this.videoShareModal.show()
212 showDownloadModal (event: Event) {
213 event.preventDefault()
214 this.videoDownloadModal.show()
218 return this.authService.isLoggedIn()
221 isVideoUpdatable () {
222 return this.video.isUpdatableBy(this.authService.getUser())
225 isVideoBlacklistable () {
226 return this.video.isBlackistableBy(this.user)
230 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
234 if (!this.video) return ''
236 return this.video.previewUrl
240 if (!this.video || Array.isArray(this.video.tags) === false) return []
242 return this.video.tags.join(', ')
245 isVideoRemovable () {
246 return this.video.isRemovableBy(this.authService.getUser())
249 async removeVideo (event: Event) {
250 event.preventDefault()
252 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
253 if (res === false) return
255 this.videoService.removeVideo(this.video.id)
258 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
260 // Go back to the video-list.
261 this.redirectService.redirectToHomepage()
264 error => this.notificationsService.error('Error', error.message)
268 acceptedPrivacyConcern () {
269 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
270 this.hasAlreadyAcceptedPrivacyConcern = true
273 private updateVideoDescription (description: string) {
274 this.video.description = description
275 this.setVideoDescriptionHTML()
278 private setVideoDescriptionHTML () {
279 if (!this.video.description) {
280 this.videoHTMLDescription = ''
284 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
287 private setVideoLikesBarTooltipText () {
288 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
291 private handleError (err: any) {
292 const errorMessage: string = typeof err === 'string' ? err : err.message
293 if (!errorMessage) return
297 if (errorMessage.indexOf('http error') !== -1) {
298 message = 'Cannot fetch video from server, maybe down.'
300 message = errorMessage
303 this.notificationsService.error('Error', message)
306 private checkUserRating () {
307 // Unlogged users do not have ratings
308 if (this.isUserLoggedIn() === false) return
310 this.videoService.getUserVideoRating(this.video.id)
314 this.userRating = ratingObject.rating
318 err => this.notificationsService.error('Error', err.message)
322 private async onVideoFetched (video: VideoDetails, startTime = 0) {
325 // Re init attributes
326 this.descriptionLoading = false
327 this.completeDescriptionShown = false
329 this.updateOtherVideosDisplayed()
331 if (this.video.isVideoNSFWForUser(this.user)) {
332 const res = await this.confirmService.confirm(
333 'This video contains mature or explicit content. Are you sure you want to watch it?',
334 'Mature or explicit content'
336 if (res === false) return this.redirectService.redirectToHomepage()
339 // Flush old player if needed
342 // Build video element, because videojs remove it on dispose
343 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
344 this.playerElement = document.createElement('video')
345 this.playerElement.className = 'video-js vjs-peertube-skin'
346 playerElementWrapper.appendChild(this.playerElement)
348 const videojsOptions = getVideojsOptions({
349 autoplay: this.isAutoplay(),
350 inactivityTimeout: 2500,
351 videoFiles: this.video.files,
352 playerElement: this.playerElement,
353 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
354 videoDuration: this.video.duration,
357 poster: this.video.previewUrl,
362 this.zone.runOutsideAngular(() => {
363 videojs(this.playerElement, videojsOptions, function () {
365 this.on('customError', (event, data) => self.handleError(data.err))
369 this.setVideoDescriptionHTML()
370 this.setVideoLikesBarTooltipText()
372 this.setOpenGraphTags()
373 this.checkUserRating()
376 private setRating (nextRating) {
378 switch (nextRating) {
380 method = this.videoService.setVideoLike
383 method = this.videoService.setVideoDislike
386 method = this.videoService.unsetVideoLike
390 method.call(this.videoService, this.video.id)
393 // Update the video like attribute
394 this.updateVideoRating(this.userRating, nextRating)
395 this.userRating = nextRating
397 err => this.notificationsService.error('Error', err.message)
401 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
402 let likesToIncrement = 0
403 let dislikesToIncrement = 0
406 if (oldRating === 'like') likesToIncrement--
407 if (oldRating === 'dislike') dislikesToIncrement--
410 if (newRating === 'like') likesToIncrement++
411 if (newRating === 'dislike') dislikesToIncrement++
413 this.video.likes += likesToIncrement
414 this.video.dislikes += dislikesToIncrement
416 this.video.buildLikeAndDislikePercents()
417 this.setVideoLikesBarTooltipText()
420 private updateOtherVideosDisplayed () {
421 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
422 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
426 private setOpenGraphTags () {
427 this.metaService.setTitle(this.video.name)
429 this.metaService.setTag('og:type', 'video')
431 this.metaService.setTag('og:title', this.video.name)
432 this.metaService.setTag('name', this.video.name)
434 this.metaService.setTag('og:description', this.video.description)
435 this.metaService.setTag('description', this.video.description)
437 this.metaService.setTag('og:image', this.video.previewPath)
439 this.metaService.setTag('og:duration', this.video.duration.toString())
441 this.metaService.setTag('og:site_name', 'PeerTube')
443 this.metaService.setTag('og:url', window.location.href)
444 this.metaService.setTag('url', window.location.href)
447 private isAutoplay () {
449 if (!this.user) return true
451 // Be sure the autoPlay is set to false
452 return this.user.autoPlayVideo !== false
455 private flushPlayer () {
456 // Remove player if it exists
458 this.player.dispose()
459 this.player = undefined