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