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