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 { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
5 import { MetaService } from '@ngx-meta/core'
6 import { NotificationsService } from 'angular2-notifications'
7 import { Subscription } from 'rxjs/Subscription'
8 import * as videojs from 'video.js'
9 import 'videojs-hotkeys'
10 import * as WebTorrent from 'webtorrent'
11 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
12 import '../../../assets/player/peertube-videojs-plugin'
13 import { AuthService, ConfirmService } from '../../core'
14 import { VideoBlacklistService } from '../../shared'
15 import { Account } from '../../shared/account/account.model'
16 import { VideoDetails } from '../../shared/video/video-details.model'
17 import { Video } from '../../shared/video/video.model'
18 import { VideoService } from '../../shared/video/video.service'
19 import { MarkdownService } from '../shared'
20 import { VideoDownloadComponent } from './modal/video-download.component'
21 import { VideoReportComponent } from './modal/video-report.component'
22 import { VideoShareComponent } from './modal/video-share.component'
25 selector: 'my-video-watch',
26 templateUrl: './video-watch.component.html',
27 styleUrls: [ './video-watch.component.scss' ]
29 export class VideoWatchComponent implements OnInit, OnDestroy {
30 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
32 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
33 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
34 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
35 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
37 otherVideosDisplayed: Video[] = []
40 player: videojs.Player
41 playerElement: HTMLVideoElement
42 userRating: UserVideoRateType = null
43 video: VideoDetails = null
44 videoPlayerLoaded = false
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()
78 if (WebTorrent.WEBRTC_SUPPORT === false || localStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true') {
79 this.hasAlreadyAcceptedPrivacyConcern = true
82 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
85 this.otherVideos = data.videos
86 this.updateOtherVideosDisplayed()
89 err => console.error(err)
92 this.paramsSub = this.route.params.subscribe(routeParams => {
93 if (this.videoPlayerLoaded) {
97 const uuid = routeParams['uuid']
98 // Video did not changed
99 if (this.video && this.video.uuid === uuid) return
101 this.videoService.getVideo(uuid).subscribe(
102 video => this.onVideoFetched(video),
105 this.videoNotFound = true
113 // Remove player if it exists
114 if (this.videoPlayerLoaded === true) {
115 videojs(this.playerElement).dispose()
118 // Unsubscribe subscriptions
119 this.paramsSub.unsubscribe()
123 if (this.isUserLoggedIn() === false) return
124 if (this.userRating === 'like') {
125 // Already liked this video
126 this.setRating('none')
128 this.setRating('like')
133 if (this.isUserLoggedIn() === false) return
134 if (this.userRating === 'dislike') {
135 // Already disliked this video
136 this.setRating('none')
138 this.setRating('dislike')
142 async blacklistVideo (event: Event) {
143 event.preventDefault()
145 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
146 if (res === false) return
148 this.videoBlacklistService.blacklistVideo(this.video.id)
151 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
152 this.redirectService.redirectToHomepage()
155 error => this.notificationsService.error('Error', error.message)
159 showMoreDescription () {
160 if (this.completeVideoDescription === undefined) {
161 return this.loadCompleteDescription()
164 this.updateVideoDescription(this.completeVideoDescription)
165 this.completeDescriptionShown = true
168 showLessDescription () {
169 this.updateVideoDescription(this.shortVideoDescription)
170 this.completeDescriptionShown = false
173 loadCompleteDescription () {
174 this.descriptionLoading = true
176 this.videoService.loadCompleteDescription(this.video.descriptionPath)
179 this.completeDescriptionShown = true
180 this.descriptionLoading = false
182 this.shortVideoDescription = this.video.description
183 this.completeVideoDescription = description
185 this.updateVideoDescription(this.completeVideoDescription)
189 this.descriptionLoading = false
190 this.notificationsService.error('Error', error.message)
195 showReportModal (event: Event) {
196 event.preventDefault()
197 this.videoReportModal.show()
200 showSupportModal () {
201 this.videoSupportModal.show()
205 this.videoShareModal.show()
208 showDownloadModal (event: Event) {
209 event.preventDefault()
210 this.videoDownloadModal.show()
214 return this.authService.isLoggedIn()
217 isVideoUpdatable () {
218 return this.video.isUpdatableBy(this.authService.getUser())
221 isVideoBlacklistable () {
222 return this.video.isBlackistableBy(this.user)
226 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
230 if (!this.video) return ''
232 return this.video.previewUrl
236 if (!this.video || Array.isArray(this.video.tags) === false) return []
238 return this.video.tags.join(', ')
241 isVideoRemovable () {
242 return this.video.isRemovableBy(this.authService.getUser())
245 async removeVideo (event: Event) {
246 event.preventDefault()
248 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
249 if (res === false) return
251 this.videoService.removeVideo(this.video.id)
254 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
256 // Go back to the video-list.
257 this.redirectService.redirectToHomepage()
260 error => this.notificationsService.error('Error', error.message)
264 acceptedPrivacyConcern () {
265 localStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
266 this.hasAlreadyAcceptedPrivacyConcern = true
269 private updateVideoDescription (description: string) {
270 this.video.description = description
271 this.setVideoDescriptionHTML()
274 private setVideoDescriptionHTML () {
275 if (!this.video.description) {
276 this.videoHTMLDescription = ''
280 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
283 private setVideoLikesBarTooltipText () {
284 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
287 private handleError (err: any) {
288 const errorMessage: string = typeof err === 'string' ? err : err.message
289 if (!errorMessage) return
293 if (errorMessage.indexOf('http error') !== -1) {
294 message = 'Cannot fetch video from server, maybe down.'
296 message = errorMessage
299 this.notificationsService.error('Error', message)
302 private checkUserRating () {
303 // Unlogged users do not have ratings
304 if (this.isUserLoggedIn() === false) return
306 this.videoService.getUserVideoRating(this.video.id)
310 this.userRating = ratingObject.rating
314 err => this.notificationsService.error('Error', err.message)
318 private async onVideoFetched (video: VideoDetails) {
321 this.updateOtherVideosDisplayed()
323 if (this.video.isVideoNSFWForUser(this.user)) {
324 const res = await this.confirmService.confirm(
325 'This video contains mature or explicit content. Are you sure you want to watch it?',
326 'Mature or explicit content'
328 if (res === false) return this.redirectService.redirectToHomepage()
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.5, 2 ],
346 videoFiles: this.video.files,
347 playerElement: this.playerElement,
348 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
349 videoDuration: this.video.duration
352 enableVolumeScroll: false
358 'currentTimeDisplay',
363 'flexibleWidthSpacer',
368 'playbackRateMenuButton',
373 'resolutionMenuButton',
380 this.videoPlayerLoaded = true
383 this.zone.runOutsideAngular(() => {
384 videojs(this.playerElement, videojsOptions, function () {
386 this.on('customError', (event, data) => self.handleError(data.err))
390 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
391 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
394 this.setVideoDescriptionHTML()
395 this.setVideoLikesBarTooltipText()
397 this.setOpenGraphTags()
398 this.checkUserRating()
401 private setRating (nextRating) {
403 switch (nextRating) {
405 method = this.videoService.setVideoLike
408 method = this.videoService.setVideoDislike
411 method = this.videoService.unsetVideoLike
415 method.call(this.videoService, this.video.id)
418 // Update the video like attribute
419 this.updateVideoRating(this.userRating, nextRating)
420 this.userRating = nextRating
422 err => this.notificationsService.error('Error', err.message)
426 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
427 let likesToIncrement = 0
428 let dislikesToIncrement = 0
431 if (oldRating === 'like') likesToIncrement--
432 if (oldRating === 'dislike') dislikesToIncrement--
435 if (newRating === 'like') likesToIncrement++
436 if (newRating === 'dislike') dislikesToIncrement++
438 this.video.likes += likesToIncrement
439 this.video.dislikes += dislikesToIncrement
441 this.video.buildLikeAndDislikePercents()
442 this.setVideoLikesBarTooltipText()
445 private updateOtherVideosDisplayed () {
446 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
447 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
451 private setOpenGraphTags () {
452 this.metaService.setTitle(this.video.name)
454 this.metaService.setTag('og:type', 'video')
456 this.metaService.setTag('og:title', this.video.name)
457 this.metaService.setTag('name', this.video.name)
459 this.metaService.setTag('og:description', this.video.description)
460 this.metaService.setTag('description', this.video.description)
462 this.metaService.setTag('og:image', this.video.previewPath)
464 this.metaService.setTag('og:duration', this.video.duration.toString())
466 this.metaService.setTag('og:site_name', 'PeerTube')
468 this.metaService.setTag('og:url', window.location.href)
469 this.metaService.setTag('url', window.location.href)
472 private isAutoplay () {
474 if (!this.user) return true
476 // Be sure the autoPlay is set to false
477 return this.user.autoPlayVideo !== false