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

558 lines
17 KiB
TypeScript
Raw Normal View History

2018-09-18 09:59:05 +00:00
import { catchError } from 'rxjs/operators'
2018-07-17 15:06:34 +00:00
import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, 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'
import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
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 { Notifier, ServerService } from '@app/core'
2018-07-13 16:21:19 +00:00
import { forkJoin, Subscription } from 'rxjs'
import { Hotkey, HotkeysService } from 'angular2-hotkeys'
2018-03-19 16:16:34 +00:00
import * as WebTorrent from 'webtorrent'
import { UserVideoRateType, VideoCaption, VideoPrivacy, VideoState } from '../../../../../shared'
import { AuthService, ConfirmService } from '../../core'
2018-05-31 09:35:01 +00:00
import { RestExtractor, VideoBlacklistService } from '../../shared'
2017-12-07 10:15:19 +00:00
import { VideoDetails } from '../../shared/video/video-details.model'
2017-12-11 16:36:46 +00:00
import { VideoService } from '../../shared/video/video.service'
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'
2018-08-13 14:57:13 +00:00
import { VideoBlacklistComponent } from './modal/video-blacklist.component'
import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
2018-05-31 16:12:15 +00:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2018-06-06 12:23:40 +00:00
import { environment } from '../../../environments/environment'
2018-07-13 16:21:19 +00:00
import { VideoCaptionService } from '@app/shared/video-caption'
import { MarkdownService } from '@app/shared/renderer'
2019-02-06 09:39:50 +00:00
import {
P2PMediaLoaderOptions,
PeertubePlayerManager,
PeertubePlayerManagerOptions,
PlayerMode,
WebtorrentOptions
} from '../../../assets/player/peertube-player-manager'
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
2018-08-13 14:57:13 +00:00
@ViewChild('videoBlacklistModal') videoBlacklistModal: VideoBlacklistComponent
@ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent
player: any
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
descriptionLoading = false
completeDescriptionShown = false
completeVideoDescription: string
shortVideoDescription: string
videoHTMLDescription = ''
2017-12-21 09:49:52 +00:00
likesBarTooltipText = ''
hasAlreadyAcceptedPrivacyConcern = false
remoteServerDown = false
hotkeys: Hotkey[]
private paramsSub: Subscription
constructor (
2016-05-27 15:49:18 +00:00
private elementRef: ElementRef,
2018-07-17 15:06:34 +00:00
private changeDetector: ChangeDetectorRef,
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 serverService: ServerService,
2018-05-31 09:35:01 +00:00
private restExtractor: RestExtractor,
private notifier: Notifier,
2018-01-10 16:36:35 +00:00
private markdownService: MarkdownService,
2018-03-01 12:57:29 +00:00
private zone: NgZone,
2018-05-31 16:12:15 +00:00
private redirectService: RedirectService,
2018-07-13 16:21:19 +00:00
private videoCaptionService: VideoCaptionService,
2018-06-06 12:23:40 +00:00
private i18n: I18n,
private hotkeysService: HotkeysService,
2018-06-06 12:23:40 +00:00
@Inject(LOCALE_ID) private localeId: string
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 () {
if (
WebTorrent.WEBRTC_SUPPORT === false ||
peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
) {
this.hasAlreadyAcceptedPrivacyConcern = true
}
this.paramsSub = this.route.params.subscribe(routeParams => {
const uuid = routeParams[ 'uuid' ]
2018-05-31 09:35:01 +00:00
// Video did not change
2018-02-19 09:38:24 +00:00
if (this.video && this.video.uuid === uuid) return
if (this.player) this.player.pause()
// Video did change
2018-07-13 16:21:19 +00:00
forkJoin(
this.videoService.getVideo(uuid),
this.videoCaptionService.listCaptions(uuid)
)
.pipe(
2018-08-14 07:08:47 +00:00
// If 401, the video is private or blacklisted so redirect to 404
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ]))
2018-07-13 16:21:19 +00:00
)
.subscribe(([ video, captionsResult ]) => {
const startTime = this.route.snapshot.queryParams.start
const subtitle = this.route.snapshot.queryParams.subtitle
this.onVideoFetched(video, captionsResult.data, { startTime, subtitle })
2018-07-13 16:21:19 +00:00
.catch(err => this.handleError(err))
})
})
this.hotkeys = [
new Hotkey('shift+l', (event: KeyboardEvent): boolean => {
this.setLike()
return false
2018-10-03 08:11:26 +00:00
}, undefined, this.i18n('Like the video')),
new Hotkey('shift+d', (event: KeyboardEvent): boolean => {
this.setDislike()
return false
2018-10-03 08:11:26 +00:00
}, undefined, this.i18n('Dislike the video')),
new Hotkey('shift+s', (event: KeyboardEvent): boolean => {
this.subscribeButton.subscribed ?
this.subscribeButton.unsubscribe() :
this.subscribeButton.subscribe()
return false
2018-10-03 08:11:26 +00:00
}, undefined, this.i18n('Subscribe to the account'))
]
if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
}
ngOnDestroy () {
2018-04-03 16:06:58 +00:00
this.flushPlayer()
2016-11-08 20:17:17 +00:00
// Unsubscribe subscriptions
this.paramsSub.unsubscribe()
// Unbind hotkeys
if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
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
}
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
this.notifier.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 () {
2018-08-27 13:59:00 +00:00
const currentTime = this.player ? this.player.currentTime() : undefined
this.videoShareModal.show(currentTime)
2016-11-08 20:11:57 +00:00
}
showDownloadModal (event: Event) {
event.preventDefault()
this.videoDownloadModal.show()
2016-11-08 20:11:57 +00:00
}
2018-08-13 14:57:13 +00:00
showBlacklistModal (event: Event) {
event.preventDefault()
this.videoBlacklistModal.show()
}
2018-08-14 07:08:47 +00:00
async unblacklistVideo (event: Event) {
event.preventDefault()
const confirmMessage = this.i18n(
'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
)
const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
if (res === false) return
this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe(
() => {
this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name }))
2018-08-14 07:08:47 +00:00
this.video.blacklisted = false
this.video.blacklistedReason = null
},
err => this.notifier.error(err.message)
2018-08-14 07:08:47 +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
}
2018-08-14 07:08:47 +00:00
isVideoUnblacklistable () {
return this.video.isUnblacklistableBy(this.user)
}
2017-12-06 16:15:59 +00:00
getVideoTags () {
if (!this.video || Array.isArray(this.video.tags) === false) return []
return this.video.tags
2017-12-06 16:15:59 +00:00
}
isVideoRemovable () {
return this.video.isRemovableBy(this.authService.getUser())
}
async removeVideo (event: Event) {
event.preventDefault()
2018-05-31 16:12:15 +00:00
const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
if (res === false) return
this.videoService.removeVideo(this.video.id)
.subscribe(
() => {
this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }))
// Go back to the video-list.
this.redirectService.redirectToHomepage()
},
error => this.notifier.error(error.message)
)
}
acceptedPrivacyConcern () {
peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
this.hasAlreadyAcceptedPrivacyConcern = true
}
isVideoToTranscode () {
return this.video && this.video.state.id === VideoState.TO_TRANSCODE
}
isVideoToImport () {
return this.video && this.video.state.id === VideoState.TO_IMPORT
}
hasVideoScheduledPublication () {
return this.video && this.video.scheduledUpdate !== undefined
}
private updateVideoDescription (description: string) {
this.video.description = description
this.setVideoDescriptionHTML()
}
private setVideoDescriptionHTML () {
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.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', {
likesNumber: this.video.likes,
dislikesNumber: this.video.dislikes
})
2017-12-21 09:49:52 +00:00
}
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
// Display a message in the video player instead of a notification
if (errorMessage.indexOf('from xs param') !== -1) {
this.flushPlayer()
this.remoteServerDown = true
2018-07-17 15:06:34 +00:00
this.changeDetector.detectChanges()
return
2017-07-23 09:07:30 +00:00
}
this.notifier.error(errorMessage)
2017-07-23 09:07:30 +00:00
}
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(
ratingObject => {
if (ratingObject) {
this.userRating = ratingObject.rating
}
},
err => this.notifier.error(err.message)
)
2017-03-08 20:35:43 +00:00
}
private async onVideoFetched (video: VideoDetails, videoCaptions: VideoCaption[], urlOptions: { startTime: number, subtitle: string }) {
this.video = video
2017-04-04 19:37:03 +00:00
// Re init attributes
this.descriptionLoading = false
this.completeDescriptionShown = false
this.remoteServerDown = false
let startTime = urlOptions.startTime || (this.video.userHistory ? this.video.userHistory.currentTime : 0)
2019-01-08 14:16:54 +00:00
// If we are at the end of the video, reset the timer
2018-10-05 09:15:06 +00:00
if (this.video.duration - startTime <= 1) startTime = 0
if (this.video.isVideoNSFWForUser(this.user, this.serverService.getConfig())) {
const res = await this.confirmService.confirm(
2018-05-31 16:12:15 +00:00
this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'),
this.i18n('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
}
2018-04-03 16:06:58 +00:00
// Flush old player if needed
this.flushPlayer()
2018-04-03 15:33:39 +00:00
// Build video element, because videojs remove it on dispose
const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
this.playerElement = document.createElement('video')
this.playerElement.className = 'video-js vjs-peertube-skin'
2018-05-22 07:16:05 +00:00
this.playerElement.setAttribute('playsinline', 'true')
2018-04-03 15:33:39 +00:00
playerElementWrapper.appendChild(this.playerElement)
2018-07-13 16:21:19 +00:00
const playerCaptions = videoCaptions.map(c => ({
label: c.language.label,
language: c.language.id,
src: environment.apiUrl + c.captionPath
}))
2019-02-06 09:39:50 +00:00
const options: PeertubePlayerManagerOptions = {
common: {
autoplay: this.isAutoplay(),
2019-02-06 09:39:50 +00:00
playerElement: this.playerElement,
2019-02-06 09:39:50 +00:00
onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element,
videoDuration: this.video.duration,
enableHotkeys: true,
inactivityTimeout: 2500,
poster: this.video.previewUrl,
startTime,
theaterMode: true,
captions: videoCaptions.length !== 0,
peertubeLink: false,
videoViewUrl: this.video.privacy.id !== VideoPrivacy.PRIVATE ? this.videoService.getVideoViewUrl(this.video.uuid) : null,
embedUrl: this.video.embedUrl,
language: this.localeId,
subtitle: urlOptions.subtitle,
userWatching: this.user && this.user.videosHistoryEnabled === true ? {
url: this.videoService.getUserWatchingVideoUrl(this.video.uuid),
authorizationHeader: this.authService.getRequestHeaderValue()
} : undefined,
serverUrl: environment.apiUrl,
videoCaptions: playerCaptions
2019-02-06 09:39:50 +00:00
},
webtorrent: {
videoFiles: this.video.files
2019-01-29 07:37:25 +00:00
}
}
2019-01-29 07:37:25 +00:00
let mode: PlayerMode
const hlsPlaylist = this.video.getHlsPlaylist()
if (hlsPlaylist) {
mode = 'p2p-media-loader'
2019-02-06 09:39:50 +00:00
2019-01-29 07:37:25 +00:00
const p2pMediaLoader = {
playlistUrl: hlsPlaylist.playlistUrl,
segmentsSha256Url: hlsPlaylist.segmentsSha256Url,
redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl),
trackerAnnounce: this.video.trackerUrls,
videoFiles: this.video.files
2019-01-29 07:37:25 +00:00
} as P2PMediaLoaderOptions
Object.assign(options, { p2pMediaLoader })
} else {
mode = 'webtorrent'
2018-06-06 12:23:40 +00:00
}
this.zone.runOutsideAngular(async () => {
2019-01-29 07:37:25 +00:00
this.player = await PeertubePlayerManager.initialize(mode, options)
this.player.on('customError', ({ err }: { err: any }) => this.handleError(err))
2018-04-03 15:33:39 +00:00
})
this.setVideoDescriptionHTML()
this.setVideoLikesBarTooltipText()
this.setOpenGraphTags()
this.checkUserRating()
2017-04-04 19:37:03 +00:00
}
2018-11-14 14:01:28 +00:00
private setRating (nextRating: UserVideoRateType) {
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
2018-10-18 12:35:31 +00:00
this.updateVideoRating(this.userRating, nextRating)
this.userRating = nextRating
},
(err: { message: string }) => this.notifier.error(err.message)
)
}
2018-11-14 14:01:28 +00:00
private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) {
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 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 () {
// We'll jump to the thread id, so do not play the video
if (this.route.snapshot.params['threadId']) return false
// Otherwise true by default
if (!this.user) return true
// Be sure the autoPlay is set to false
return this.user.autoPlayVideo !== false
}
2018-04-03 16:06:58 +00:00
private flushPlayer () {
// Remove player if it exists
if (this.player) {
this.player.dispose()
this.player = undefined
}
}
2016-03-14 12:50:19 +00:00
}