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
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 => {
98 if (this.videoPlayerLoaded) {
102 const uuid = routeParams['uuid']
103 // Video did not changed
104 if (this.video && this.video.uuid === uuid) return
106 this.videoService.getVideo(uuid).subscribe(
107 video => this.onVideoFetched(video),
110 this.videoNotFound = true
118 // Remove player if it exists
119 if (this.videoPlayerLoaded === true) {
120 videojs(this.playerElement).dispose()
123 // Unsubscribe subscriptions
124 this.paramsSub.unsubscribe()
128 if (this.isUserLoggedIn() === false) return
129 if (this.userRating === 'like') {
130 // Already liked this video
131 this.setRating('none')
133 this.setRating('like')
138 if (this.isUserLoggedIn() === false) return
139 if (this.userRating === 'dislike') {
140 // Already disliked this video
141 this.setRating('none')
143 this.setRating('dislike')
147 async blacklistVideo (event: Event) {
148 event.preventDefault()
150 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
151 if (res === false) return
153 this.videoBlacklistService.blacklistVideo(this.video.id)
156 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
157 this.redirectService.redirectToHomepage()
160 error => this.notificationsService.error('Error', error.message)
164 showMoreDescription () {
165 if (this.completeVideoDescription === undefined) {
166 return this.loadCompleteDescription()
169 this.updateVideoDescription(this.completeVideoDescription)
170 this.completeDescriptionShown = true
173 showLessDescription () {
174 this.updateVideoDescription(this.shortVideoDescription)
175 this.completeDescriptionShown = false
178 loadCompleteDescription () {
179 this.descriptionLoading = true
181 this.videoService.loadCompleteDescription(this.video.descriptionPath)
184 this.completeDescriptionShown = true
185 this.descriptionLoading = false
187 this.shortVideoDescription = this.video.description
188 this.completeVideoDescription = description
190 this.updateVideoDescription(this.completeVideoDescription)
194 this.descriptionLoading = false
195 this.notificationsService.error('Error', error.message)
200 showReportModal (event: Event) {
201 event.preventDefault()
202 this.videoReportModal.show()
205 showSupportModal () {
206 this.videoSupportModal.show()
210 this.videoShareModal.show()
213 showDownloadModal (event: Event) {
214 event.preventDefault()
215 this.videoDownloadModal.show()
219 return this.authService.isLoggedIn()
222 isVideoUpdatable () {
223 return this.video.isUpdatableBy(this.authService.getUser())
226 isVideoBlacklistable () {
227 return this.video.isBlackistableBy(this.user)
231 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
235 if (!this.video) return ''
237 return this.video.previewUrl
241 if (!this.video || Array.isArray(this.video.tags) === false) return []
243 return this.video.tags.join(', ')
246 isVideoRemovable () {
247 return this.video.isRemovableBy(this.authService.getUser())
250 async removeVideo (event: Event) {
251 event.preventDefault()
253 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
254 if (res === false) return
256 this.videoService.removeVideo(this.video.id)
259 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
261 // Go back to the video-list.
262 this.redirectService.redirectToHomepage()
265 error => this.notificationsService.error('Error', error.message)
269 acceptedPrivacyConcern () {
270 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
271 this.hasAlreadyAcceptedPrivacyConcern = true
274 private updateVideoDescription (description: string) {
275 this.video.description = description
276 this.setVideoDescriptionHTML()
279 private setVideoDescriptionHTML () {
280 if (!this.video.description) {
281 this.videoHTMLDescription = ''
285 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
288 private setVideoLikesBarTooltipText () {
289 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
292 private handleError (err: any) {
293 const errorMessage: string = typeof err === 'string' ? err : err.message
294 if (!errorMessage) return
298 if (errorMessage.indexOf('http error') !== -1) {
299 message = 'Cannot fetch video from server, maybe down.'
301 message = errorMessage
304 this.notificationsService.error('Error', message)
307 private checkUserRating () {
308 // Unlogged users do not have ratings
309 if (this.isUserLoggedIn() === false) return
311 this.videoService.getUserVideoRating(this.video.id)
315 this.userRating = ratingObject.rating
319 err => this.notificationsService.error('Error', err.message)
323 private async onVideoFetched (video: VideoDetails) {
326 this.updateOtherVideosDisplayed()
328 if (this.video.isVideoNSFWForUser(this.user)) {
329 const res = await this.confirmService.confirm(
330 'This video contains mature or explicit content. Are you sure you want to watch it?',
331 'Mature or explicit content'
333 if (res === false) return this.redirectService.redirectToHomepage()
336 // Player was already loaded
337 if (this.videoPlayerLoaded !== true) {
338 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
340 // If autoplay is true, we don't really need a poster
341 if (this.isAutoplay() === false) {
342 this.playerElement.poster = this.video.previewUrl
345 const videojsOptions = getVideojsOptions({
346 autoplay: this.isAutoplay(),
347 inactivityTimeout: 4000,
348 videoFiles: this.video.files,
349 playerElement: this.playerElement,
350 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
351 videoDuration: this.video.duration,
356 this.videoPlayerLoaded = true
359 this.zone.runOutsideAngular(() => {
360 videojs(this.playerElement, videojsOptions, function () {
362 this.on('customError', (event, data) => self.handleError(data.err))
366 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
367 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
370 this.setVideoDescriptionHTML()
371 this.setVideoLikesBarTooltipText()
373 this.setOpenGraphTags()
374 this.checkUserRating()
377 private setRating (nextRating) {
379 switch (nextRating) {
381 method = this.videoService.setVideoLike
384 method = this.videoService.setVideoDislike
387 method = this.videoService.unsetVideoLike
391 method.call(this.videoService, this.video.id)
394 // Update the video like attribute
395 this.updateVideoRating(this.userRating, nextRating)
396 this.userRating = nextRating
398 err => this.notificationsService.error('Error', err.message)
402 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
403 let likesToIncrement = 0
404 let dislikesToIncrement = 0
407 if (oldRating === 'like') likesToIncrement--
408 if (oldRating === 'dislike') dislikesToIncrement--
411 if (newRating === 'like') likesToIncrement++
412 if (newRating === 'dislike') dislikesToIncrement++
414 this.video.likes += likesToIncrement
415 this.video.dislikes += dislikesToIncrement
417 this.video.buildLikeAndDislikePercents()
418 this.setVideoLikesBarTooltipText()
421 private updateOtherVideosDisplayed () {
422 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
423 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
427 private setOpenGraphTags () {
428 this.metaService.setTitle(this.video.name)
430 this.metaService.setTag('og:type', 'video')
432 this.metaService.setTag('og:title', this.video.name)
433 this.metaService.setTag('name', this.video.name)
435 this.metaService.setTag('og:description', this.video.description)
436 this.metaService.setTag('description', this.video.description)
438 this.metaService.setTag('og:image', this.video.previewPath)
440 this.metaService.setTag('og:duration', this.video.duration.toString())
442 this.metaService.setTag('og:site_name', 'PeerTube')
444 this.metaService.setTag('og:url', window.location.href)
445 this.metaService.setTag('url', window.location.href)
448 private isAutoplay () {
450 if (!this.user) return true
452 // Be sure the autoPlay is set to false
453 return this.user.autoPlayVideo !== false