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