]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Improve player
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
7ae71355 1import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e 2import { ActivatedRoute, Router } from '@angular/router'
901637bb 3import { RedirectService } from '@app/core/routing/redirect.service'
0bd78bf3 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
07fa4c97 5import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
1f3e9fec
C
6import { MetaService } from '@ngx-meta/core'
7import { NotificationsService } from 'angular2-notifications'
df98563e 8import { Subscription } from 'rxjs/Subscription'
63c4db6d 9import * as videojs from 'video.js'
d7701449 10import 'videojs-hotkeys'
caae7a06 11import * as WebTorrent from 'webtorrent'
1f3e9fec 12import { UserVideoRateType, VideoRateType } from '../../../../../shared'
aa8b6df4 13import '../../../assets/player/peertube-videojs-plugin'
df98563e 14import { AuthService, ConfirmService } from '../../core'
1f3e9fec 15import { VideoBlacklistService } from '../../shared'
b1fa3eba 16import { Account } from '../../shared/account/account.model'
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'
c6352f2c 24import { getVideojsOptions } from '../../../assets/player/peertube-player'
dc8bc31b 25
dc8bc31b
C
26@Component({
27 selector: 'my-video-watch',
ec8d8440
C
28 templateUrl: './video-watch.component.html',
29 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b 30})
0629423c 31export class VideoWatchComponent implements OnInit, OnDestroy {
22b59e80
C
32 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
33
a96aed15 34 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
df98563e
C
35 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
36 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
07fa4c97 37 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
df98563e 38
57a49263 39 otherVideosDisplayed: Video[] = []
b1fa3eba 40
df98563e 41 error = false
df98563e 42 player: videojs.Player
0826c92d 43 playerElement: HTMLVideoElement
154898b0 44 userRating: UserVideoRateType = null
404b54e1 45 video: VideoDetails = null
efee3505 46 videoPlayerLoaded = false
f1013131 47 videoNotFound = false
80958c78 48 descriptionLoading = false
2de96f4d
C
49
50 completeDescriptionShown = false
51 completeVideoDescription: string
52 shortVideoDescription: string
9d9597df 53 videoHTMLDescription = ''
e9189001 54 likesBarTooltipText = ''
73e09f27 55 hasAlreadyAcceptedPrivacyConcern = false
df98563e 56
28832412 57 private otherVideos: Video[] = []
df98563e 58 private paramsSub: Subscription
df98563e
C
59
60 constructor (
4fd8aa32 61 private elementRef: ElementRef,
0629423c 62 private route: ActivatedRoute,
92fb909c 63 private router: Router,
d3ef341a 64 private videoService: VideoService,
35bf0c83 65 private videoBlacklistService: VideoBlacklistService,
92fb909c 66 private confirmService: ConfirmService,
3ec343a4 67 private metaService: MetaService,
7ddd02c9 68 private authService: AuthService,
9d9597df 69 private notificationsService: NotificationsService,
7ae71355 70 private markdownService: MarkdownService,
901637bb
C
71 private zone: NgZone,
72 private redirectService: RedirectService
d3ef341a 73 ) {}
dc8bc31b 74
b2731bff
C
75 get user () {
76 return this.authService.getUser()
77 }
78
df98563e 79 ngOnInit () {
0bd78bf3
C
80 if (
81 WebTorrent.WEBRTC_SUPPORT === false ||
82 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
83 ) {
2b3b76ab
C
84 this.hasAlreadyAcceptedPrivacyConcern = true
85 }
86
b1fa3eba
C
87 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
88 .subscribe(
649fb082
C
89 data => {
90 this.otherVideos = data.videos
91 this.updateOtherVideosDisplayed()
92 },
93
57a49263 94 err => console.error(err)
b1fa3eba
C
95 )
96
13fc89f4 97 this.paramsSub = this.route.params.subscribe(routeParams => {
ed9f9f5f
C
98 if (this.videoPlayerLoaded) {
99 this.player.pause()
100 }
101
1263fc4e
C
102 const uuid = routeParams['uuid']
103 // Video did not changed
104 if (this.video && this.video.uuid === uuid) return
105
0a6658fd 106 this.videoService.getVideo(uuid).subscribe(
92fb909c
C
107 video => this.onVideoFetched(video),
108
f1013131
C
109 error => {
110 this.videoNotFound = true
111 console.error(error)
112 }
df98563e
C
113 )
114 })
d1992b93
C
115 }
116
df98563e 117 ngOnDestroy () {
2ed6a0ae 118 // Remove player if it exists
efee3505 119 if (this.videoPlayerLoaded === true) {
2ed6a0ae
C
120 videojs(this.playerElement).dispose()
121 }
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
1f30a185 147 async blacklistVideo (event: Event) {
df98563e 148 event.preventDefault()
ab683a8e 149
1f30a185
C
150 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
151 if (res === false) return
198b205c 152
1f30a185
C
153 this.videoBlacklistService.blacklistVideo(this.video.id)
154 .subscribe(
155 status => {
156 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
901637bb 157 this.redirectService.redirectToHomepage()
1f30a185 158 },
198b205c 159
1f30a185
C
160 error => this.notificationsService.error('Error', error.message)
161 )
198b205c
GS
162 }
163
2de96f4d 164 showMoreDescription () {
2de96f4d
C
165 if (this.completeVideoDescription === undefined) {
166 return this.loadCompleteDescription()
167 }
168
169 this.updateVideoDescription(this.completeVideoDescription)
80958c78 170 this.completeDescriptionShown = true
2de96f4d
C
171 }
172
173 showLessDescription () {
2de96f4d 174 this.updateVideoDescription(this.shortVideoDescription)
80958c78 175 this.completeDescriptionShown = false
2de96f4d
C
176 }
177
178 loadCompleteDescription () {
80958c78
C
179 this.descriptionLoading = true
180
2de96f4d
C
181 this.videoService.loadCompleteDescription(this.video.descriptionPath)
182 .subscribe(
183 description => {
80958c78
C
184 this.completeDescriptionShown = true
185 this.descriptionLoading = false
186
2de96f4d
C
187 this.shortVideoDescription = this.video.description
188 this.completeVideoDescription = description
189
190 this.updateVideoDescription(this.completeVideoDescription)
191 },
192
80958c78
C
193 error => {
194 this.descriptionLoading = false
c5911fd3 195 this.notificationsService.error('Error', error.message)
80958c78 196 }
2de96f4d
C
197 )
198 }
199
df98563e
C
200 showReportModal (event: Event) {
201 event.preventDefault()
202 this.videoReportModal.show()
4f8c0eb0
C
203 }
204
07fa4c97
C
205 showSupportModal () {
206 this.videoSupportModal.show()
207 }
208
df98563e
C
209 showShareModal () {
210 this.videoShareModal.show()
99cc4f49
C
211 }
212
a96aed15 213 showDownloadModal (event: Event) {
df98563e 214 event.preventDefault()
a96aed15 215 this.videoDownloadModal.show()
99cc4f49
C
216 }
217
df98563e
C
218 isUserLoggedIn () {
219 return this.authService.isLoggedIn()
4f8c0eb0
C
220 }
221
4635f59d
C
222 isVideoUpdatable () {
223 return this.video.isUpdatableBy(this.authService.getUser())
224 }
225
df98563e 226 isVideoBlacklistable () {
b2731bff 227 return this.video.isBlackistableBy(this.user)
198b205c
GS
228 }
229
b1fa3eba 230 getAvatarPath () {
c5911fd3 231 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
b1fa3eba
C
232 }
233
6de36768
C
234 getVideoPoster () {
235 if (!this.video) return ''
236
237 return this.video.previewUrl
238 }
239
b1fa3eba
C
240 getVideoTags () {
241 if (!this.video || Array.isArray(this.video.tags) === false) return []
242
243 return this.video.tags.join(', ')
244 }
245
6725d05c
C
246 isVideoRemovable () {
247 return this.video.isRemovableBy(this.authService.getUser())
248 }
249
1f30a185 250 async removeVideo (event: Event) {
6725d05c
C
251 event.preventDefault()
252
1f30a185
C
253 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
254 if (res === false) return
6725d05c 255
1f30a185
C
256 this.videoService.removeVideo(this.video.id)
257 .subscribe(
258 status => {
259 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
6725d05c 260
1f30a185 261 // Go back to the video-list.
901637bb 262 this.redirectService.redirectToHomepage()
1f30a185 263 },
6725d05c 264
1f30a185 265 error => this.notificationsService.error('Error', error.message)
e9189001 266 )
6725d05c
C
267 }
268
73e09f27 269 acceptedPrivacyConcern () {
0bd78bf3 270 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
73e09f27
C
271 this.hasAlreadyAcceptedPrivacyConcern = true
272 }
273
2de96f4d
C
274 private updateVideoDescription (description: string) {
275 this.video.description = description
276 this.setVideoDescriptionHTML()
277 }
278
279 private setVideoDescriptionHTML () {
cadb46d8
C
280 if (!this.video.description) {
281 this.videoHTMLDescription = ''
282 return
283 }
284
07fa4c97 285 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
2de96f4d
C
286 }
287
e9189001
C
288 private setVideoLikesBarTooltipText () {
289 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
290 }
291
0c31c33d
C
292 private handleError (err: any) {
293 const errorMessage: string = typeof err === 'string' ? err : err.message
bf5685f0
C
294 if (!errorMessage) return
295
0c31c33d
C
296 let message = ''
297
298 if (errorMessage.indexOf('http error') !== -1) {
299 message = 'Cannot fetch video from server, maybe down.'
300 } else {
301 message = errorMessage
302 }
303
304 this.notificationsService.error('Error', message)
305 }
306
df98563e 307 private checkUserRating () {
d38b8281 308 // Unlogged users do not have ratings
df98563e 309 if (this.isUserLoggedIn() === false) return
d38b8281
C
310
311 this.videoService.getUserVideoRating(this.video.id)
312 .subscribe(
b632e904 313 ratingObject => {
d38b8281 314 if (ratingObject) {
df98563e 315 this.userRating = ratingObject.rating
d38b8281
C
316 }
317 },
318
bfb3a98f 319 err => this.notificationsService.error('Error', err.message)
df98563e 320 )
d38b8281
C
321 }
322
22b59e80 323 private async onVideoFetched (video: VideoDetails) {
df98563e 324 this.video = video
92fb909c 325
649fb082 326 this.updateOtherVideosDisplayed()
57a49263 327
b2731bff 328 if (this.video.isVideoNSFWForUser(this.user)) {
22b59e80 329 const res = await this.confirmService.confirm(
d6e32a2e
C
330 'This video contains mature or explicit content. Are you sure you want to watch it?',
331 'Mature or explicit content'
332 )
901637bb 333 if (res === false) return this.redirectService.redirectToHomepage()
92fb909c
C
334 }
335
22b59e80
C
336 // Player was already loaded
337 if (this.videoPlayerLoaded !== true) {
338 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
ed9f9f5f 339
22b59e80
C
340 // If autoplay is true, we don't really need a poster
341 if (this.isAutoplay() === false) {
342 this.playerElement.poster = this.video.previewUrl
343 }
0826c92d 344
c6352f2c 345 const videojsOptions = getVideojsOptions({
22b59e80 346 autoplay: this.isAutoplay(),
c6352f2c
C
347 inactivityTimeout: 4000,
348 videoFiles: this.video.files,
349 playerElement: this.playerElement,
350 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
351 videoDuration: this.video.duration,
352 enableHotkeys: true,
353 peertubeLink: false
354 })
aa8b6df4 355
22b59e80 356 this.videoPlayerLoaded = true
9d9597df 357
22b59e80
C
358 const self = this
359 this.zone.runOutsideAngular(() => {
360 videojs(this.playerElement, videojsOptions, function () {
361 self.player = this
362 this.on('customError', (event, data) => self.handleError(data.err))
363 })
364 })
365 } else {
366 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
367 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
368 }
369
370 this.setVideoDescriptionHTML()
371 this.setVideoLikesBarTooltipText()
372
373 this.setOpenGraphTags()
374 this.checkUserRating()
92fb909c
C
375 }
376
57a49263
BB
377 private setRating (nextRating) {
378 let method
379 switch (nextRating) {
380 case 'like':
381 method = this.videoService.setVideoLike
382 break
383 case 'dislike':
384 method = this.videoService.setVideoDislike
385 break
386 case 'none':
387 method = this.videoService.unsetVideoLike
388 break
389 }
390
391 method.call(this.videoService, this.video.id)
392 .subscribe(
393 () => {
394 // Update the video like attribute
395 this.updateVideoRating(this.userRating, nextRating)
396 this.userRating = nextRating
397 },
398 err => this.notificationsService.error('Error', err.message)
399 )
400 }
401
154898b0 402 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
403 let likesToIncrement = 0
404 let dislikesToIncrement = 0
d38b8281
C
405
406 if (oldRating) {
df98563e
C
407 if (oldRating === 'like') likesToIncrement--
408 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
409 }
410
df98563e
C
411 if (newRating === 'like') likesToIncrement++
412 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 413
df98563e
C
414 this.video.likes += likesToIncrement
415 this.video.dislikes += dislikesToIncrement
20b40b19 416
22b59e80 417 this.video.buildLikeAndDislikePercents()
20b40b19 418 this.setVideoLikesBarTooltipText()
d38b8281
C
419 }
420
649fb082 421 private updateOtherVideosDisplayed () {
f6dc2fff 422 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
649fb082
C
423 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
424 }
425 }
426
df98563e
C
427 private setOpenGraphTags () {
428 this.metaService.setTitle(this.video.name)
758b996d 429
df98563e 430 this.metaService.setTag('og:type', 'video')
3ec343a4 431
df98563e
C
432 this.metaService.setTag('og:title', this.video.name)
433 this.metaService.setTag('name', this.video.name)
3ec343a4 434
df98563e
C
435 this.metaService.setTag('og:description', this.video.description)
436 this.metaService.setTag('description', this.video.description)
3ec343a4 437
d38309c3 438 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 439
df98563e 440 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 441
df98563e 442 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 443
df98563e
C
444 this.metaService.setTag('og:url', window.location.href)
445 this.metaService.setTag('url', window.location.href)
3ec343a4 446 }
1f3e9fec 447
d4c6a3b9
C
448 private isAutoplay () {
449 // True by default
450 if (!this.user) return true
451
452 // Be sure the autoPlay is set to false
453 return this.user.autoPlayVideo !== false
454 }
dc8bc31b 455}