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