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