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