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