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