1
0
Fork 0
peertube/client/src/app/videos/+video-watch/video-watch.component.ts

480 lines
14 KiB
TypeScript
Raw Normal View History

2018-01-10 16:36:35 +00:00
import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
2018-03-01 12:57:29 +00:00
import { RedirectService } from '@app/core/routing/redirect.service'
2018-02-20 15:13:05 +00:00
import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
2017-11-30 08:21:11 +00:00
import { MetaService } from '@ngx-meta/core'
import { NotificationsService } from 'angular2-notifications'
import { Subscription } from 'rxjs/Subscription'
2017-12-11 16:36:46 +00:00
import * as videojs from 'video.js'
import 'videojs-hotkeys'
2018-03-19 16:16:34 +00:00
import * as WebTorrent from 'webtorrent'
2017-11-30 08:21:11 +00:00
import { UserVideoRateType, VideoRateType } from '../../../../../shared'
import '../../../assets/player/peertube-videojs-plugin'
import { AuthService, ConfirmService } from '../../core'
2017-11-30 08:21:11 +00:00
import { VideoBlacklistService } from '../../shared'
2017-12-06 16:15:59 +00:00
import { Account } from '../../shared/account/account.model'
2017-12-07 10:15:19 +00:00
import { VideoDetails } from '../../shared/video/video-details.model'
2017-12-06 16:15:59 +00:00
import { Video } from '../../shared/video/video.model'
2017-12-11 16:36:46 +00:00
import { VideoService } from '../../shared/video/video.service'
2017-12-01 17:56:26 +00:00
import { MarkdownService } from '../shared'
2017-12-27 15:11:53 +00:00
import { VideoDownloadComponent } from './modal/video-download.component'
import { VideoReportComponent } from './modal/video-report.component'
import { VideoShareComponent } from './modal/video-share.component'
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-video-watch',
templateUrl: './video-watch.component.html',
styleUrls: [ './video-watch.component.scss' ]
2016-03-14 12:50:19 +00:00
})
2016-07-08 15:15:14 +00:00
export class VideoWatchComponent implements OnInit, OnDestroy {
private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
@ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
@ViewChild('videoShareModal') videoShareModal: VideoShareComponent
@ViewChild('videoReportModal') videoReportModal: VideoReportComponent
2018-02-20 15:13:05 +00:00
@ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
otherVideosDisplayed: Video[] = []
2017-12-06 16:15:59 +00:00
error = false
player: videojs.Player
2017-12-20 08:57:00 +00:00
playerElement: HTMLVideoElement
2017-06-17 09:28:11 +00:00
userRating: UserVideoRateType = null
2017-10-25 14:43:19 +00:00
video: VideoDetails = null
2017-10-19 13:39:08 +00:00
videoPlayerLoaded = false
2017-10-19 13:47:56 +00:00
videoNotFound = false
descriptionLoading = false
completeDescriptionShown = false
completeVideoDescription: string
shortVideoDescription: string
videoHTMLDescription = ''
2017-12-21 09:49:52 +00:00
likesBarTooltipText = ''
hasAlreadyAcceptedPrivacyConcern = false
2018-01-08 09:46:41 +00:00
private otherVideos: Video[] = []
private paramsSub: Subscription
constructor (
2016-05-27 15:49:18 +00:00
private elementRef: ElementRef,
2016-07-08 15:15:14 +00:00
private route: ActivatedRoute,
2017-04-04 19:37:03 +00:00
private router: Router,
2016-05-31 20:39:36 +00:00
private videoService: VideoService,
2017-10-10 08:02:18 +00:00
private videoBlacklistService: VideoBlacklistService,
2017-04-04 19:37:03 +00:00
private confirmService: ConfirmService,
2016-11-04 16:37:44 +00:00
private metaService: MetaService,
private authService: AuthService,
private notificationsService: NotificationsService,
2018-01-10 16:36:35 +00:00
private markdownService: MarkdownService,
2018-03-01 12:57:29 +00:00
private zone: NgZone,
private redirectService: RedirectService
2016-05-31 20:39:36 +00:00
) {}
2016-03-14 12:50:19 +00:00
2017-12-12 13:41:59 +00:00
get user () {
return this.authService.getUser()
}
ngOnInit () {
2018-03-19 16:16:34 +00:00
if (WebTorrent.WEBRTC_SUPPORT === false || localStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true') {
this.hasAlreadyAcceptedPrivacyConcern = true
}
2017-12-06 16:15:59 +00:00
this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
.subscribe(
data => {
this.otherVideos = data.videos
this.updateOtherVideosDisplayed()
},
err => console.error(err)
2017-12-06 16:15:59 +00:00
)
this.paramsSub = this.route.params.subscribe(routeParams => {
2017-12-11 10:20:54 +00:00
if (this.videoPlayerLoaded) {
this.player.pause()
}
2018-02-19 09:38:24 +00:00
const uuid = routeParams['uuid']
// Video did not changed
if (this.video && this.video.uuid === uuid) return
this.videoService.getVideo(uuid).subscribe(
2017-04-04 19:37:03 +00:00
video => this.onVideoFetched(video),
2017-10-19 13:47:56 +00:00
error => {
this.videoNotFound = true
console.error(error)
}
)
})
}
ngOnDestroy () {
// Remove player if it exists
2017-10-19 13:39:08 +00:00
if (this.videoPlayerLoaded === true) {
videojs(this.playerElement).dispose()
}
2016-11-08 20:17:17 +00:00
// Unsubscribe subscriptions
this.paramsSub.unsubscribe()
2016-03-14 12:50:19 +00:00
}
2016-03-14 21:16:43 +00:00
setLike () {
if (this.isUserLoggedIn() === false) return
if (this.userRating === 'like') {
// Already liked this video
this.setRating('none')
} else {
this.setRating('like')
}
2017-03-08 20:35:43 +00:00
}
setDislike () {
if (this.isUserLoggedIn() === false) return
if (this.userRating === 'dislike') {
// Already disliked this video
this.setRating('none')
} else {
this.setRating('dislike')
}
2017-03-08 20:35:43 +00:00
}
async blacklistVideo (event: Event) {
event.preventDefault()
2017-04-26 19:42:36 +00:00
const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
if (res === false) return
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 19:22:10 +00:00
this.videoBlacklistService.blacklistVideo(this.video.id)
.subscribe(
status => {
this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
2018-03-01 12:57:29 +00:00
this.redirectService.redirectToHomepage()
},
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 19:22:10 +00:00
error => this.notificationsService.error('Error', error.message)
)
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 19:22:10 +00:00
}
showMoreDescription () {
if (this.completeVideoDescription === undefined) {
return this.loadCompleteDescription()
}
this.updateVideoDescription(this.completeVideoDescription)
this.completeDescriptionShown = true
}
showLessDescription () {
this.updateVideoDescription(this.shortVideoDescription)
this.completeDescriptionShown = false
}
loadCompleteDescription () {
this.descriptionLoading = true
this.videoService.loadCompleteDescription(this.video.descriptionPath)
.subscribe(
description => {
this.completeDescriptionShown = true
this.descriptionLoading = false
this.shortVideoDescription = this.video.description
this.completeVideoDescription = description
this.updateVideoDescription(this.completeVideoDescription)
},
error => {
this.descriptionLoading = false
2017-12-29 18:10:13 +00:00
this.notificationsService.error('Error', error.message)
}
)
}
showReportModal (event: Event) {
event.preventDefault()
this.videoReportModal.show()
2017-01-20 18:22:15 +00:00
}
2018-02-20 15:13:05 +00:00
showSupportModal () {
this.videoSupportModal.show()
}
showShareModal () {
this.videoShareModal.show()
2016-11-08 20:11:57 +00:00
}
showDownloadModal (event: Event) {
event.preventDefault()
this.videoDownloadModal.show()
2016-11-08 20:11:57 +00:00
}
isUserLoggedIn () {
return this.authService.isLoggedIn()
2017-01-20 18:22:15 +00:00
}
2017-12-27 15:11:53 +00:00
isVideoUpdatable () {
return this.video.isUpdatableBy(this.authService.getUser())
}
isVideoBlacklistable () {
2017-12-12 13:41:59 +00:00
return this.video.isBlackistableBy(this.user)
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 19:22:10 +00:00
}
2017-12-06 16:15:59 +00:00
getAvatarPath () {
2017-12-29 18:10:13 +00:00
return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
2017-12-06 16:15:59 +00:00
}
getVideoPoster () {
if (!this.video) return ''
return this.video.previewUrl
}
2017-12-06 16:15:59 +00:00
getVideoTags () {
if (!this.video || Array.isArray(this.video.tags) === false) return []
return this.video.tags.join(', ')
}
isVideoRemovable () {
return this.video.isRemovableBy(this.authService.getUser())
}
async removeVideo (event: Event) {
event.preventDefault()
const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
if (res === false) return
this.videoService.removeVideo(this.video.id)
.subscribe(
status => {
this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
// Go back to the video-list.
2018-03-01 12:57:29 +00:00
this.redirectService.redirectToHomepage()
},
error => this.notificationsService.error('Error', error.message)
2017-12-21 09:49:52 +00:00
)
}
acceptedPrivacyConcern () {
localStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
this.hasAlreadyAcceptedPrivacyConcern = true
}
private updateVideoDescription (description: string) {
this.video.description = description
this.setVideoDescriptionHTML()
}
private setVideoDescriptionHTML () {
2017-12-08 07:39:15 +00:00
if (!this.video.description) {
this.videoHTMLDescription = ''
return
}
2018-02-20 15:13:05 +00:00
this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
}
2017-12-21 09:49:52 +00:00
private setVideoLikesBarTooltipText () {
this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
}
2017-07-23 09:07:30 +00:00
private handleError (err: any) {
const errorMessage: string = typeof err === 'string' ? err : err.message
2018-02-26 08:55:23 +00:00
if (!errorMessage) return
2017-07-23 09:07:30 +00:00
let message = ''
if (errorMessage.indexOf('http error') !== -1) {
message = 'Cannot fetch video from server, maybe down.'
} else {
message = errorMessage
}
this.notificationsService.error('Error', message)
}
private checkUserRating () {
2017-03-08 20:35:43 +00:00
// Unlogged users do not have ratings
if (this.isUserLoggedIn() === false) return
2017-03-08 20:35:43 +00:00
this.videoService.getUserVideoRating(this.video.id)
.subscribe(
2017-06-20 18:20:09 +00:00
ratingObject => {
2017-03-08 20:35:43 +00:00
if (ratingObject) {
this.userRating = ratingObject.rating
2017-03-08 20:35:43 +00:00
}
},
err => this.notificationsService.error('Error', err.message)
)
2017-03-08 20:35:43 +00:00
}
private async onVideoFetched (video: VideoDetails) {
this.video = video
2017-04-04 19:37:03 +00:00
this.updateOtherVideosDisplayed()
2017-12-12 13:41:59 +00:00
if (this.video.isVideoNSFWForUser(this.user)) {
const res = await this.confirmService.confirm(
'This video contains mature or explicit content. Are you sure you want to watch it?',
'Mature or explicit content'
)
2018-03-01 12:57:29 +00:00
if (res === false) return this.redirectService.redirectToHomepage()
2017-04-04 19:37:03 +00:00
}
// Player was already loaded
if (this.videoPlayerLoaded !== true) {
this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
2017-12-11 10:20:54 +00:00
// If autoplay is true, we don't really need a poster
if (this.isAutoplay() === false) {
this.playerElement.poster = this.video.previewUrl
}
2017-12-20 08:57:00 +00:00
const videojsOptions = {
controls: true,
autoplay: this.isAutoplay(),
2018-03-01 13:14:56 +00:00
playbackRates: [ 0.5, 1, 1.5, 2 ],
plugins: {
peertube: {
videoFiles: this.video.files,
playerElement: this.playerElement,
videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
videoDuration: this.video.duration
},
hotkeys: {
enableVolumeScroll: false
}
},
controlBar: {
children: [
'playToggle',
'currentTimeDisplay',
'timeDivider',
'durationDisplay',
'liveDisplay',
'flexibleWidthSpacer',
'progressControl',
'webTorrentButton',
'playbackRateMenuButton',
'muteToggle',
'volumeControl',
'resolutionMenuButton',
'fullscreenToggle'
]
2017-12-11 10:20:54 +00:00
}
}
this.videoPlayerLoaded = true
const self = this
this.zone.runOutsideAngular(() => {
videojs(this.playerElement, videojsOptions, function () {
self.player = this
this.on('customError', (event, data) => self.handleError(data.err))
})
})
} else {
const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
}
this.setVideoDescriptionHTML()
this.setVideoLikesBarTooltipText()
this.setOpenGraphTags()
this.checkUserRating()
2017-04-04 19:37:03 +00:00
}
private setRating (nextRating) {
let method
switch (nextRating) {
case 'like':
method = this.videoService.setVideoLike
break
case 'dislike':
method = this.videoService.setVideoDislike
break
case 'none':
method = this.videoService.unsetVideoLike
break
}
method.call(this.videoService, this.video.id)
.subscribe(
() => {
// Update the video like attribute
this.updateVideoRating(this.userRating, nextRating)
this.userRating = nextRating
},
err => this.notificationsService.error('Error', err.message)
)
}
2017-06-17 09:28:11 +00:00
private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
let likesToIncrement = 0
let dislikesToIncrement = 0
2017-03-08 20:35:43 +00:00
if (oldRating) {
if (oldRating === 'like') likesToIncrement--
if (oldRating === 'dislike') dislikesToIncrement--
2017-03-08 20:35:43 +00:00
}
if (newRating === 'like') likesToIncrement++
if (newRating === 'dislike') dislikesToIncrement++
2017-03-08 20:35:43 +00:00
this.video.likes += likesToIncrement
this.video.dislikes += dislikesToIncrement
2018-02-28 08:49:40 +00:00
this.video.buildLikeAndDislikePercents()
2018-02-28 08:49:40 +00:00
this.setVideoLikesBarTooltipText()
2017-03-08 20:35:43 +00:00
}
private updateOtherVideosDisplayed () {
2018-02-08 09:19:09 +00:00
if (this.video && this.otherVideos && this.otherVideos.length > 0) {
this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
}
}
private setOpenGraphTags () {
this.metaService.setTitle(this.video.name)
2017-03-10 09:33:36 +00:00
this.metaService.setTag('og:type', 'video')
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:title', this.video.name)
this.metaService.setTag('name', this.video.name)
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:description', this.video.description)
this.metaService.setTag('description', this.video.description)
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:image', this.video.previewPath)
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:duration', this.video.duration.toString())
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:site_name', 'PeerTube')
2016-11-04 16:37:44 +00:00
this.metaService.setTag('og:url', window.location.href)
this.metaService.setTag('url', window.location.href)
2016-11-04 16:37:44 +00:00
}
2017-11-30 08:21:11 +00:00
private isAutoplay () {
// True by default
if (!this.user) return true
// Be sure the autoPlay is set to false
return this.user.autoPlayVideo !== false
}
2016-03-14 12:50:19 +00:00
}