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