]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Fix start/count/sort params in documentation
[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
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,
0629423c 68 private route: ActivatedRoute,
92fb909c 69 private router: Router,
d3ef341a 70 private videoService: VideoService,
35bf0c83 71 private videoBlacklistService: VideoBlacklistService,
92fb909c 72 private confirmService: ConfirmService,
3ec343a4 73 private metaService: MetaService,
7ddd02c9 74 private authService: AuthService,
0883b324 75 private serverService: ServerService,
a51bad1a 76 private restExtractor: RestExtractor,
9d9597df 77 private notificationsService: NotificationsService,
7ae71355 78 private markdownService: MarkdownService,
901637bb 79 private zone: NgZone,
989e526a 80 private redirectService: RedirectService,
16f7022b 81 private videoCaptionService: VideoCaptionService,
e945b184
C
82 private i18n: I18n,
83 @Inject(LOCALE_ID) private localeId: string
d3ef341a 84 ) {}
dc8bc31b 85
b2731bff
C
86 get user () {
87 return this.authService.getUser()
88 }
89
df98563e 90 ngOnInit () {
0bd78bf3
C
91 if (
92 WebTorrent.WEBRTC_SUPPORT === false ||
93 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
94 ) {
2b3b76ab
C
95 this.hasAlreadyAcceptedPrivacyConcern = true
96 }
97
b1fa3eba 98 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
2186386c
C
99 .subscribe(
100 data => {
101 this.otherVideos = data.videos
102 this.updateOtherVideosDisplayed()
103 },
649fb082 104
2186386c
C
105 err => console.error(err)
106 )
b1fa3eba 107
13fc89f4 108 this.paramsSub = this.route.params.subscribe(routeParams => {
2186386c 109 const uuid = routeParams[ 'uuid' ]
a51bad1a 110
244e76a5 111 // Video did not change
1263fc4e 112 if (this.video && this.video.uuid === uuid) return
bf079b7b
C
113
114 if (this.player) this.player.pause()
115
244e76a5 116 // Video did change
16f7022b
C
117 forkJoin(
118 this.videoService.getVideo(uuid),
119 this.videoCaptionService.listCaptions(uuid)
120 )
121 .pipe(
122 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
123 )
124 .subscribe(([ video, captionsResult ]) => {
125 const startTime = this.route.snapshot.queryParams.start
126 this.onVideoFetched(video, captionsResult.data, startTime)
127 .catch(err => this.handleError(err))
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 165 this.videoBlacklistService.blacklistVideo(this.video.id)
2186386c
C
166 .subscribe(
167 () => {
168 this.notificationsService.success(
169 this.i18n('Success'),
170 this.i18n('Video {{videoName}} had been blacklisted.', { videoName: this.video.name })
171 )
172 this.redirectService.redirectToHomepage()
173 },
174
175 error => this.notificationsService.error(this.i18n('Error'), error.message)
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 196 this.videoService.loadCompleteDescription(this.video.descriptionPath)
2186386c
C
197 .subscribe(
198 description => {
199 this.completeDescriptionShown = true
200 this.descriptionLoading = false
201
202 this.shortVideoDescription = this.video.description
203 this.completeVideoDescription = description
204
205 this.updateVideoDescription(this.completeVideoDescription)
206 },
207
208 error => {
209 this.descriptionLoading = false
210 this.notificationsService.error(this.i18n('Error'), error.message)
211 }
212 )
2de96f4d
C
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 267 this.videoService.removeVideo(this.video.id)
2186386c
C
268 .subscribe(
269 status => {
270 this.notificationsService.success(
271 this.i18n('Success'),
272 this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name })
273 )
274
275 // Go back to the video-list.
276 this.redirectService.redirectToHomepage()
277 },
278
279 error => this.notificationsService.error(this.i18n('Error'), error.message)
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
2186386c
C
288 isVideoToTranscode () {
289 return this.video && this.video.state.id === VideoState.TO_TRANSCODE
290 }
291
bbe0f064
C
292 hasVideoScheduledPublication () {
293 return this.video && this.video.scheduledUpdate !== undefined
294 }
295
2de96f4d
C
296 private updateVideoDescription (description: string) {
297 this.video.description = description
298 this.setVideoDescriptionHTML()
299 }
300
301 private setVideoDescriptionHTML () {
07fa4c97 302 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
2de96f4d
C
303 }
304
e9189001 305 private setVideoLikesBarTooltipText () {
2186386c
C
306 this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', {
307 likesNumber: this.video.likes,
308 dislikesNumber: this.video.dislikes
309 })
e9189001
C
310 }
311
0c31c33d
C
312 private handleError (err: any) {
313 const errorMessage: string = typeof err === 'string' ? err : err.message
bf5685f0
C
314 if (!errorMessage) return
315
6d88de72 316 // Display a message in the video player instead of a notification
0f7fedc3 317 if (errorMessage.indexOf('from xs param') !== -1) {
6d88de72
C
318 this.flushPlayer()
319 this.remoteServerDown = true
320 return
0c31c33d
C
321 }
322
6d88de72 323 this.notificationsService.error(this.i18n('Error'), errorMessage)
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
6d88de72 348 this.remoteServerDown = false
c448d412 349
649fb082 350 this.updateOtherVideosDisplayed()
57a49263 351
0883b324 352 if (this.video.isVideoNSFWForUser(this.user, this.serverService.getConfig())) {
22b59e80 353 const res = await this.confirmService.confirm(
989e526a
C
354 this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'),
355 this.i18n('Mature or explicit content')
d6e32a2e 356 )
901637bb 357 if (res === false) return this.redirectService.redirectToHomepage()
92fb909c
C
358 }
359
09edde40
C
360 // Flush old player if needed
361 this.flushPlayer()
b891f9bc
C
362
363 // Build video element, because videojs remove it on dispose
364 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
365 this.playerElement = document.createElement('video')
366 this.playerElement.className = 'video-js vjs-peertube-skin'
e7eb5b39 367 this.playerElement.setAttribute('playsinline', 'true')
b891f9bc
C
368 playerElementWrapper.appendChild(this.playerElement)
369
16f7022b
C
370 const playerCaptions = videoCaptions.map(c => ({
371 label: c.language.label,
372 language: c.language.id,
373 src: environment.apiUrl + c.captionPath
374 }))
375
b891f9bc
C
376 const videojsOptions = getVideojsOptions({
377 autoplay: this.isAutoplay(),
09edde40 378 inactivityTimeout: 2500,
b891f9bc 379 videoFiles: this.video.files,
16f7022b 380 videoCaptions: playerCaptions,
b891f9bc 381 playerElement: this.playerElement,
f5a2dc48 382 videoViewUrl: this.video.privacy.id !== VideoPrivacy.PRIVATE ? this.videoService.getVideoViewUrl(this.video.uuid) : null,
b891f9bc
C
383 videoDuration: this.video.duration,
384 enableHotkeys: true,
385 peertubeLink: false,
f37bad63 386 poster: this.video.previewUrl,
054a103b
C
387 startTime,
388 theaterMode: true
b891f9bc 389 })
aa8b6df4 390
e945b184 391 if (this.videojsLocaleLoaded === false) {
74b7c6d4 392 await loadLocale(environment.apiUrl, videojs, isOnDevLocale() ? getDevLocale() : this.localeId)
e945b184
C
393 this.videojsLocaleLoaded = true
394 }
395
b891f9bc 396 const self = this
e945b184 397 this.zone.runOutsideAngular(async () => {
09edde40 398 videojs(this.playerElement, videojsOptions, function () {
b891f9bc
C
399 self.player = this
400 this.on('customError', (event, data) => self.handleError(data.err))
e945b184
C
401
402 addContextMenu(self.player, self.video.embedUrl)
22b59e80 403 })
b891f9bc 404 })
22b59e80
C
405
406 this.setVideoDescriptionHTML()
407 this.setVideoLikesBarTooltipText()
408
409 this.setOpenGraphTags()
410 this.checkUserRating()
92fb909c
C
411 }
412
57a49263
BB
413 private setRating (nextRating) {
414 let method
415 switch (nextRating) {
416 case 'like':
417 method = this.videoService.setVideoLike
418 break
419 case 'dislike':
420 method = this.videoService.setVideoDislike
421 break
422 case 'none':
423 method = this.videoService.unsetVideoLike
424 break
425 }
426
427 method.call(this.videoService, this.video.id)
2186386c
C
428 .subscribe(
429 () => {
430 // Update the video like attribute
431 this.updateVideoRating(this.userRating, nextRating)
432 this.userRating = nextRating
433 },
434
435 err => this.notificationsService.error(this.i18n('Error'), err.message)
436 )
57a49263
BB
437 }
438
154898b0 439 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
440 let likesToIncrement = 0
441 let dislikesToIncrement = 0
d38b8281
C
442
443 if (oldRating) {
df98563e
C
444 if (oldRating === 'like') likesToIncrement--
445 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
446 }
447
df98563e
C
448 if (newRating === 'like') likesToIncrement++
449 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 450
df98563e
C
451 this.video.likes += likesToIncrement
452 this.video.dislikes += dislikesToIncrement
20b40b19 453
22b59e80 454 this.video.buildLikeAndDislikePercents()
20b40b19 455 this.setVideoLikesBarTooltipText()
d38b8281
C
456 }
457
649fb082 458 private updateOtherVideosDisplayed () {
f6dc2fff 459 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
649fb082
C
460 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
461 }
462 }
463
df98563e
C
464 private setOpenGraphTags () {
465 this.metaService.setTitle(this.video.name)
758b996d 466
df98563e 467 this.metaService.setTag('og:type', 'video')
3ec343a4 468
df98563e
C
469 this.metaService.setTag('og:title', this.video.name)
470 this.metaService.setTag('name', this.video.name)
3ec343a4 471
df98563e
C
472 this.metaService.setTag('og:description', this.video.description)
473 this.metaService.setTag('description', this.video.description)
3ec343a4 474
d38309c3 475 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 476
df98563e 477 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 478
df98563e 479 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 480
df98563e
C
481 this.metaService.setTag('og:url', window.location.href)
482 this.metaService.setTag('url', window.location.href)
3ec343a4 483 }
1f3e9fec 484
d4c6a3b9 485 private isAutoplay () {
bf079b7b
C
486 // We'll jump to the thread id, so do not play the video
487 if (this.route.snapshot.params['threadId']) return false
488
489 // Otherwise true by default
d4c6a3b9
C
490 if (!this.user) return true
491
492 // Be sure the autoPlay is set to false
493 return this.user.autoPlayVideo !== false
494 }
09edde40
C
495
496 private flushPlayer () {
497 // Remove player if it exists
498 if (this.player) {
499 this.player.dispose()
500 this.player = undefined
501 }
502 }
dc8bc31b 503}