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 { 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,
68 private redirectService: RedirectService
72 return this.authService.getUser()
76 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
79 this.otherVideos = data.videos
80 this.updateOtherVideosDisplayed()
83 err => console.error(err)
86 this.paramsSub = this.route.params.subscribe(routeParams => {
87 if (this.videoPlayerLoaded) {
91 const uuid = routeParams['uuid']
92 // Video did not changed
93 if (this.video && this.video.uuid === uuid) return
95 this.videoService.getVideo(uuid).subscribe(
96 video => this.onVideoFetched(video),
99 this.videoNotFound = true
107 // Remove player if it exists
108 if (this.videoPlayerLoaded === true) {
109 videojs(this.playerElement).dispose()
112 // Unsubscribe subscriptions
113 this.paramsSub.unsubscribe()
117 if (this.isUserLoggedIn() === false) return
118 if (this.userRating === 'like') {
119 // Already liked this video
120 this.setRating('none')
122 this.setRating('like')
127 if (this.isUserLoggedIn() === false) return
128 if (this.userRating === 'dislike') {
129 // Already disliked this video
130 this.setRating('none')
132 this.setRating('dislike')
136 async blacklistVideo (event: Event) {
137 event.preventDefault()
139 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
140 if (res === false) return
142 this.videoBlacklistService.blacklistVideo(this.video.id)
145 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
146 this.redirectService.redirectToHomepage()
149 error => this.notificationsService.error('Error', error.message)
153 showMoreDescription () {
154 if (this.completeVideoDescription === undefined) {
155 return this.loadCompleteDescription()
158 this.updateVideoDescription(this.completeVideoDescription)
159 this.completeDescriptionShown = true
162 showLessDescription () {
163 this.updateVideoDescription(this.shortVideoDescription)
164 this.completeDescriptionShown = false
167 loadCompleteDescription () {
168 this.descriptionLoading = true
170 this.videoService.loadCompleteDescription(this.video.descriptionPath)
173 this.completeDescriptionShown = true
174 this.descriptionLoading = false
176 this.shortVideoDescription = this.video.description
177 this.completeVideoDescription = description
179 this.updateVideoDescription(this.completeVideoDescription)
183 this.descriptionLoading = false
184 this.notificationsService.error('Error', error.message)
189 showReportModal (event: Event) {
190 event.preventDefault()
191 this.videoReportModal.show()
194 showSupportModal () {
195 this.videoSupportModal.show()
199 this.videoShareModal.show()
202 showDownloadModal (event: Event) {
203 event.preventDefault()
204 this.videoDownloadModal.show()
208 return this.authService.isLoggedIn()
211 isVideoUpdatable () {
212 return this.video.isUpdatableBy(this.authService.getUser())
215 isVideoBlacklistable () {
216 return this.video.isBlackistableBy(this.user)
220 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
224 if (!this.video) return ''
226 return this.video.previewUrl
230 if (!this.video || Array.isArray(this.video.tags) === false) return []
232 return this.video.tags.join(', ')
235 isVideoRemovable () {
236 return this.video.isRemovableBy(this.authService.getUser())
239 async removeVideo (event: Event) {
240 event.preventDefault()
242 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
243 if (res === false) return
245 this.videoService.removeVideo(this.video.id)
248 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
250 // Go back to the video-list.
251 this.redirectService.redirectToHomepage()
254 error => this.notificationsService.error('Error', error.message)
258 private updateVideoDescription (description: string) {
259 this.video.description = description
260 this.setVideoDescriptionHTML()
263 private setVideoDescriptionHTML () {
264 if (!this.video.description) {
265 this.videoHTMLDescription = ''
269 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
272 private setVideoLikesBarTooltipText () {
273 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
276 private handleError (err: any) {
277 const errorMessage: string = typeof err === 'string' ? err : err.message
278 if (!errorMessage) return
282 if (errorMessage.indexOf('http error') !== -1) {
283 message = 'Cannot fetch video from server, maybe down.'
285 message = errorMessage
288 this.notificationsService.error('Error', message)
291 private checkUserRating () {
292 // Unlogged users do not have ratings
293 if (this.isUserLoggedIn() === false) return
295 this.videoService.getUserVideoRating(this.video.id)
299 this.userRating = ratingObject.rating
303 err => this.notificationsService.error('Error', err.message)
307 private async onVideoFetched (video: VideoDetails) {
310 this.updateOtherVideosDisplayed()
312 if (this.video.isVideoNSFWForUser(this.user)) {
313 const res = await this.confirmService.confirm(
314 'This video contains mature or explicit content. Are you sure you want to watch it?',
315 'Mature or explicit content'
317 if (res === false) return this.redirectService.redirectToHomepage()
320 if (!this.hasAlreadyAcceptedPrivacyConcern()) {
321 const res = await this.confirmService.confirm(
322 'PeerTube uses P2P, other may know you are watching that video through your public IP address. ' +
323 'Are you okay with that?',
327 if (res === false) return this.redirectService.redirectToHomepage()
330 this.acceptedPrivacyConcern()
332 // Player was already loaded
333 if (this.videoPlayerLoaded !== true) {
334 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
336 // If autoplay is true, we don't really need a poster
337 if (this.isAutoplay() === false) {
338 this.playerElement.poster = this.video.previewUrl
341 const videojsOptions = {
343 autoplay: this.isAutoplay(),
344 playbackRates: [0.5, 1, 1.25, 1.5, 2],
347 videoFiles: this.video.files,
348 playerElement: this.playerElement,
350 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
351 videoDuration: this.video.duration
354 enableVolumeScroll: false
359 this.videoPlayerLoaded = true
362 this.zone.runOutsideAngular(() => {
363 videojs(this.playerElement, videojsOptions, function () {
365 this.on('customError', (event, data) => self.handleError(data.err))
369 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
370 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
373 this.setVideoDescriptionHTML()
374 this.setVideoLikesBarTooltipText()
376 this.setOpenGraphTags()
377 this.checkUserRating()
380 private setRating (nextRating) {
382 switch (nextRating) {
384 method = this.videoService.setVideoLike
387 method = this.videoService.setVideoDislike
390 method = this.videoService.unsetVideoLike
394 method.call(this.videoService, this.video.id)
397 // Update the video like attribute
398 this.updateVideoRating(this.userRating, nextRating)
399 this.userRating = nextRating
401 err => this.notificationsService.error('Error', err.message)
405 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
406 let likesToIncrement = 0
407 let dislikesToIncrement = 0
410 if (oldRating === 'like') likesToIncrement--
411 if (oldRating === 'dislike') dislikesToIncrement--
414 if (newRating === 'like') likesToIncrement++
415 if (newRating === 'dislike') dislikesToIncrement++
417 this.video.likes += likesToIncrement
418 this.video.dislikes += dislikesToIncrement
420 this.video.buildLikeAndDislikePercents()
421 this.setVideoLikesBarTooltipText()
424 private updateOtherVideosDisplayed () {
425 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
426 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
430 private setOpenGraphTags () {
431 this.metaService.setTitle(this.video.name)
433 this.metaService.setTag('og:type', 'video')
435 this.metaService.setTag('og:title', this.video.name)
436 this.metaService.setTag('name', this.video.name)
438 this.metaService.setTag('og:description', this.video.description)
439 this.metaService.setTag('description', this.video.description)
441 this.metaService.setTag('og:image', this.video.previewPath)
443 this.metaService.setTag('og:duration', this.video.duration.toString())
445 this.metaService.setTag('og:site_name', 'PeerTube')
447 this.metaService.setTag('og:url', window.location.href)
448 this.metaService.setTag('url', window.location.href)
451 private isAutoplay () {
453 if (!this.user) return true
455 // Be sure the autoPlay is set to false
456 return this.user.autoPlayVideo !== false
459 private hasAlreadyAcceptedPrivacyConcern () {
460 return localStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
463 private acceptedPrivacyConcern () {
464 localStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')