]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Fix player resolution change that plays even if the video was paused
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
a51bad1a 1import { catchError } from 'rxjs/operators'
e945b184 2import { Component, ElementRef, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild, Inject } from '@angular/core'
df98563e 3import { ActivatedRoute, Router } from '@angular/router'
901637bb 4import { RedirectService } from '@app/core/routing/redirect.service'
0bd78bf3 5import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
07fa4c97 6import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
1f3e9fec
C
7import { MetaService } from '@ngx-meta/core'
8import { NotificationsService } from 'angular2-notifications'
db400f44 9import { Subscription } from 'rxjs'
63c4db6d 10import * as videojs from 'video.js'
d7701449 11import 'videojs-hotkeys'
caae7a06 12import * as WebTorrent from 'webtorrent'
cc1561f9 13import { UserVideoRateType, VideoRateType } from '../../../../../shared'
aa8b6df4 14import '../../../assets/player/peertube-videojs-plugin'
df98563e 15import { AuthService, ConfirmService } from '../../core'
a51bad1a 16import { RestExtractor, VideoBlacklistService } from '../../shared'
ff249f49 17import { VideoDetails } from '../../shared/video/video-details.model'
b1fa3eba 18import { Video } from '../../shared/video/video.model'
63c4db6d 19import { VideoService } from '../../shared/video/video.service'
202f6b6c 20import { MarkdownService } from '../shared'
4635f59d
C
21import { VideoDownloadComponent } from './modal/video-download.component'
22import { VideoReportComponent } from './modal/video-report.component'
23import { VideoShareComponent } from './modal/video-share.component'
e945b184 24import { getVideojsOptions, loadLocale, addContextMenu } from '../../../assets/player/peertube-player'
0883b324 25import { ServerService } from '@app/core'
989e526a 26import { I18n } from '@ngx-translate/i18n-polyfill'
e945b184 27import { environment } from '../../../environments/environment'
74b7c6d4 28import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils'
dc8bc31b 29
dc8bc31b
C
30@Component({
31 selector: 'my-video-watch',
ec8d8440
C
32 templateUrl: './video-watch.component.html',
33 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b 34})
0629423c 35export class VideoWatchComponent implements OnInit, OnDestroy {
22b59e80
C
36 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
37
a96aed15 38 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
df98563e
C
39 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
40 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
07fa4c97 41 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
df98563e 42
57a49263 43 otherVideosDisplayed: Video[] = []
b1fa3eba 44
df98563e 45 player: videojs.Player
0826c92d 46 playerElement: HTMLVideoElement
154898b0 47 userRating: UserVideoRateType = null
404b54e1 48 video: VideoDetails = null
f1013131 49 videoNotFound = false
80958c78 50 descriptionLoading = false
2de96f4d
C
51
52 completeDescriptionShown = false
53 completeVideoDescription: string
54 shortVideoDescription: string
9d9597df 55 videoHTMLDescription = ''
e9189001 56 likesBarTooltipText = ''
73e09f27 57 hasAlreadyAcceptedPrivacyConcern = false
df98563e 58
e945b184 59 private videojsLocaleLoaded = false
28832412 60 private otherVideos: Video[] = []
df98563e 61 private paramsSub: Subscription
df98563e
C
62
63 constructor (
4fd8aa32 64 private elementRef: ElementRef,
0629423c 65 private route: ActivatedRoute,
92fb909c 66 private router: Router,
d3ef341a 67 private videoService: VideoService,
35bf0c83 68 private videoBlacklistService: VideoBlacklistService,
92fb909c 69 private confirmService: ConfirmService,
3ec343a4 70 private metaService: MetaService,
7ddd02c9 71 private authService: AuthService,
0883b324 72 private serverService: ServerService,
a51bad1a 73 private restExtractor: RestExtractor,
9d9597df 74 private notificationsService: NotificationsService,
7ae71355 75 private markdownService: MarkdownService,
901637bb 76 private zone: NgZone,
989e526a 77 private redirectService: RedirectService,
e945b184
C
78 private i18n: I18n,
79 @Inject(LOCALE_ID) private localeId: string
d3ef341a 80 ) {}
dc8bc31b 81
b2731bff
C
82 get user () {
83 return this.authService.getUser()
84 }
85
df98563e 86 ngOnInit () {
0bd78bf3
C
87 if (
88 WebTorrent.WEBRTC_SUPPORT === false ||
89 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
90 ) {
2b3b76ab
C
91 this.hasAlreadyAcceptedPrivacyConcern = true
92 }
93
b1fa3eba
C
94 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
95 .subscribe(
649fb082
C
96 data => {
97 this.otherVideos = data.videos
98 this.updateOtherVideosDisplayed()
99 },
100
57a49263 101 err => console.error(err)
b1fa3eba
C
102 )
103
13fc89f4 104 this.paramsSub = this.route.params.subscribe(routeParams => {
09edde40 105 if (this.player) {
ed9f9f5f
C
106 this.player.pause()
107 }
108
1263fc4e 109 const uuid = routeParams['uuid']
a51bad1a 110
244e76a5 111 // Video did not change
1263fc4e 112 if (this.video && this.video.uuid === uuid) return
244e76a5 113 // Video did change
a51bad1a
C
114 this.videoService
115 .getVideo(uuid)
116 .pipe(catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])))
117 .subscribe(
118 video => {
119 const startTime = this.route.snapshot.queryParams.start
120 this.onVideoFetched(video, startTime)
121 .catch(err => this.handleError(err))
122 },
123
124 error => {
125 this.videoNotFound = true
126 console.error(error)
127 }
128 )
df98563e 129 })
d1992b93
C
130 }
131
df98563e 132 ngOnDestroy () {
09edde40 133 this.flushPlayer()
067e3f84 134
13fc89f4 135 // Unsubscribe subscriptions
df98563e 136 this.paramsSub.unsubscribe()
dc8bc31b 137 }
98b01bac 138
df98563e
C
139 setLike () {
140 if (this.isUserLoggedIn() === false) return
57a49263
BB
141 if (this.userRating === 'like') {
142 // Already liked this video
143 this.setRating('none')
144 } else {
145 this.setRating('like')
146 }
d38b8281
C
147 }
148
df98563e
C
149 setDislike () {
150 if (this.isUserLoggedIn() === false) return
57a49263
BB
151 if (this.userRating === 'dislike') {
152 // Already disliked this video
153 this.setRating('none')
154 } else {
155 this.setRating('dislike')
156 }
d38b8281
C
157 }
158
1f30a185 159 async blacklistVideo (event: Event) {
df98563e 160 event.preventDefault()
ab683a8e 161
989e526a 162 const res = await this.confirmService.confirm(this.i18n('Do you really want to blacklist this video?'), this.i18n('Blacklist'))
1f30a185 163 if (res === false) return
198b205c 164
1f30a185
C
165 this.videoBlacklistService.blacklistVideo(this.video.id)
166 .subscribe(
167 status => {
989e526a
C
168 this.notificationsService.success(
169 this.i18n('Success'),
25acef90 170 this.i18n('Video {{videoName}} had been blacklisted.', { videoName: this.video.name })
989e526a 171 )
901637bb 172 this.redirectService.redirectToHomepage()
1f30a185 173 },
198b205c 174
989e526a 175 error => this.notificationsService.error(this.i18n('Error'), error.message)
1f30a185 176 )
198b205c
GS
177 }
178
2de96f4d 179 showMoreDescription () {
2de96f4d
C
180 if (this.completeVideoDescription === undefined) {
181 return this.loadCompleteDescription()
182 }
183
184 this.updateVideoDescription(this.completeVideoDescription)
80958c78 185 this.completeDescriptionShown = true
2de96f4d
C
186 }
187
188 showLessDescription () {
2de96f4d 189 this.updateVideoDescription(this.shortVideoDescription)
80958c78 190 this.completeDescriptionShown = false
2de96f4d
C
191 }
192
193 loadCompleteDescription () {
80958c78
C
194 this.descriptionLoading = true
195
2de96f4d
C
196 this.videoService.loadCompleteDescription(this.video.descriptionPath)
197 .subscribe(
198 description => {
80958c78
C
199 this.completeDescriptionShown = true
200 this.descriptionLoading = false
201
2de96f4d
C
202 this.shortVideoDescription = this.video.description
203 this.completeVideoDescription = description
204
205 this.updateVideoDescription(this.completeVideoDescription)
206 },
207
80958c78
C
208 error => {
209 this.descriptionLoading = false
989e526a 210 this.notificationsService.error(this.i18n('Error'), error.message)
80958c78 211 }
2de96f4d
C
212 )
213 }
214
df98563e
C
215 showReportModal (event: Event) {
216 event.preventDefault()
217 this.videoReportModal.show()
4f8c0eb0
C
218 }
219
07fa4c97
C
220 showSupportModal () {
221 this.videoSupportModal.show()
222 }
223
df98563e
C
224 showShareModal () {
225 this.videoShareModal.show()
99cc4f49
C
226 }
227
a96aed15 228 showDownloadModal (event: Event) {
df98563e 229 event.preventDefault()
a96aed15 230 this.videoDownloadModal.show()
99cc4f49
C
231 }
232
df98563e
C
233 isUserLoggedIn () {
234 return this.authService.isLoggedIn()
4f8c0eb0
C
235 }
236
4635f59d
C
237 isVideoUpdatable () {
238 return this.video.isUpdatableBy(this.authService.getUser())
239 }
240
df98563e 241 isVideoBlacklistable () {
b2731bff 242 return this.video.isBlackistableBy(this.user)
198b205c
GS
243 }
244
6de36768
C
245 getVideoPoster () {
246 if (!this.video) return ''
247
248 return this.video.previewUrl
249 }
250
b1fa3eba
C
251 getVideoTags () {
252 if (!this.video || Array.isArray(this.video.tags) === false) return []
253
254 return this.video.tags.join(', ')
255 }
256
6725d05c
C
257 isVideoRemovable () {
258 return this.video.isRemovableBy(this.authService.getUser())
259 }
260
1f30a185 261 async removeVideo (event: Event) {
6725d05c
C
262 event.preventDefault()
263
989e526a 264 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
1f30a185 265 if (res === false) return
6725d05c 266
1f30a185
C
267 this.videoService.removeVideo(this.video.id)
268 .subscribe(
269 status => {
989e526a
C
270 this.notificationsService.success(
271 this.i18n('Success'),
25acef90 272 this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name })
989e526a 273 )
6725d05c 274
1f30a185 275 // Go back to the video-list.
901637bb 276 this.redirectService.redirectToHomepage()
1f30a185 277 },
6725d05c 278
989e526a 279 error => this.notificationsService.error(this.i18n('Error'), error.message)
e9189001 280 )
6725d05c
C
281 }
282
73e09f27 283 acceptedPrivacyConcern () {
0bd78bf3 284 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
73e09f27
C
285 this.hasAlreadyAcceptedPrivacyConcern = true
286 }
287
2de96f4d
C
288 private updateVideoDescription (description: string) {
289 this.video.description = description
290 this.setVideoDescriptionHTML()
291 }
292
293 private setVideoDescriptionHTML () {
cadb46d8
C
294 if (!this.video.description) {
295 this.videoHTMLDescription = ''
296 return
297 }
298
07fa4c97 299 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
2de96f4d
C
300 }
301
e9189001 302 private setVideoLikesBarTooltipText () {
989e526a 303 this.likesBarTooltipText = this.i18n(
25acef90 304 '{{likesNumber}} likes / {{dislikesNumber}} dislikes',
989e526a
C
305 { likesNumber: this.video.likes, dislikes: this.video.dislikes }
306 )
e9189001
C
307 }
308
0c31c33d
C
309 private handleError (err: any) {
310 const errorMessage: string = typeof err === 'string' ? err : err.message
bf5685f0
C
311 if (!errorMessage) return
312
0c31c33d
C
313 let message = ''
314
315 if (errorMessage.indexOf('http error') !== -1) {
989e526a 316 message = this.i18n('Cannot fetch video from server, maybe down.')
0c31c33d
C
317 } else {
318 message = errorMessage
319 }
320
989e526a 321 this.notificationsService.error(this.i18n('Error'), message)
0c31c33d
C
322 }
323
df98563e 324 private checkUserRating () {
d38b8281 325 // Unlogged users do not have ratings
df98563e 326 if (this.isUserLoggedIn() === false) return
d38b8281
C
327
328 this.videoService.getUserVideoRating(this.video.id)
329 .subscribe(
b632e904 330 ratingObject => {
d38b8281 331 if (ratingObject) {
df98563e 332 this.userRating = ratingObject.rating
d38b8281
C
333 }
334 },
335
989e526a 336 err => this.notificationsService.error(this.i18n('Error'), err.message)
df98563e 337 )
d38b8281
C
338 }
339
f37bad63 340 private async onVideoFetched (video: VideoDetails, startTime = 0) {
df98563e 341 this.video = video
92fb909c 342
c448d412
C
343 // Re init attributes
344 this.descriptionLoading = false
345 this.completeDescriptionShown = false
346
649fb082 347 this.updateOtherVideosDisplayed()
57a49263 348
0883b324 349 if (this.video.isVideoNSFWForUser(this.user, this.serverService.getConfig())) {
22b59e80 350 const res = await this.confirmService.confirm(
989e526a
C
351 this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'),
352 this.i18n('Mature or explicit content')
d6e32a2e 353 )
901637bb 354 if (res === false) return this.redirectService.redirectToHomepage()
92fb909c
C
355 }
356
09edde40
C
357 // Flush old player if needed
358 this.flushPlayer()
b891f9bc
C
359
360 // Build video element, because videojs remove it on dispose
361 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
362 this.playerElement = document.createElement('video')
363 this.playerElement.className = 'video-js vjs-peertube-skin'
e7eb5b39 364 this.playerElement.setAttribute('playsinline', 'true')
b891f9bc
C
365 playerElementWrapper.appendChild(this.playerElement)
366
367 const videojsOptions = getVideojsOptions({
368 autoplay: this.isAutoplay(),
09edde40 369 inactivityTimeout: 2500,
b891f9bc
C
370 videoFiles: this.video.files,
371 playerElement: this.playerElement,
372 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
373 videoDuration: this.video.duration,
374 enableHotkeys: true,
375 peertubeLink: false,
f37bad63
C
376 poster: this.video.previewUrl,
377 startTime
b891f9bc 378 })
aa8b6df4 379
e945b184 380 if (this.videojsLocaleLoaded === false) {
74b7c6d4 381 await loadLocale(environment.apiUrl, videojs, isOnDevLocale() ? getDevLocale() : this.localeId)
e945b184
C
382 this.videojsLocaleLoaded = true
383 }
384
b891f9bc 385 const self = this
e945b184 386 this.zone.runOutsideAngular(async () => {
09edde40 387 videojs(this.playerElement, videojsOptions, function () {
b891f9bc
C
388 self.player = this
389 this.on('customError', (event, data) => self.handleError(data.err))
e945b184
C
390
391 addContextMenu(self.player, self.video.embedUrl)
22b59e80 392 })
b891f9bc 393 })
22b59e80
C
394
395 this.setVideoDescriptionHTML()
396 this.setVideoLikesBarTooltipText()
397
398 this.setOpenGraphTags()
399 this.checkUserRating()
92fb909c
C
400 }
401
57a49263
BB
402 private setRating (nextRating) {
403 let method
404 switch (nextRating) {
405 case 'like':
406 method = this.videoService.setVideoLike
407 break
408 case 'dislike':
409 method = this.videoService.setVideoDislike
410 break
411 case 'none':
412 method = this.videoService.unsetVideoLike
413 break
414 }
415
416 method.call(this.videoService, this.video.id)
417 .subscribe(
418 () => {
419 // Update the video like attribute
420 this.updateVideoRating(this.userRating, nextRating)
421 this.userRating = nextRating
422 },
989e526a 423 err => this.notificationsService.error(this.i18n('Error'), err.message)
57a49263
BB
424 )
425 }
426
154898b0 427 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
428 let likesToIncrement = 0
429 let dislikesToIncrement = 0
d38b8281
C
430
431 if (oldRating) {
df98563e
C
432 if (oldRating === 'like') likesToIncrement--
433 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
434 }
435
df98563e
C
436 if (newRating === 'like') likesToIncrement++
437 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 438
df98563e
C
439 this.video.likes += likesToIncrement
440 this.video.dislikes += dislikesToIncrement
20b40b19 441
22b59e80 442 this.video.buildLikeAndDislikePercents()
20b40b19 443 this.setVideoLikesBarTooltipText()
d38b8281
C
444 }
445
649fb082 446 private updateOtherVideosDisplayed () {
f6dc2fff 447 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
649fb082
C
448 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
449 }
450 }
451
df98563e
C
452 private setOpenGraphTags () {
453 this.metaService.setTitle(this.video.name)
758b996d 454
df98563e 455 this.metaService.setTag('og:type', 'video')
3ec343a4 456
df98563e
C
457 this.metaService.setTag('og:title', this.video.name)
458 this.metaService.setTag('name', this.video.name)
3ec343a4 459
df98563e
C
460 this.metaService.setTag('og:description', this.video.description)
461 this.metaService.setTag('description', this.video.description)
3ec343a4 462
d38309c3 463 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 464
df98563e 465 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 466
df98563e 467 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 468
df98563e
C
469 this.metaService.setTag('og:url', window.location.href)
470 this.metaService.setTag('url', window.location.href)
3ec343a4 471 }
1f3e9fec 472
d4c6a3b9
C
473 private isAutoplay () {
474 // True by default
475 if (!this.user) return true
476
477 // Be sure the autoPlay is set to false
478 return this.user.autoPlayVideo !== false
479 }
09edde40
C
480
481 private flushPlayer () {
482 // Remove player if it exists
483 if (this.player) {
484 this.player.dispose()
485 this.player = undefined
486 }
487 }
dc8bc31b 488}