]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/video-watch.component.ts
Add support to video support on client
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
4 import { MetaService } from '@ngx-meta/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { Observable } from 'rxjs/Observable'
7 import { Subscription } from 'rxjs/Subscription'
8 import * as videojs from 'video.js'
9 import 'videojs-hotkeys'
10 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
11 import '../../../assets/player/peertube-videojs-plugin'
12 import { AuthService, ConfirmService } from '../../core'
13 import { VideoBlacklistService } from '../../shared'
14 import { Account } from '../../shared/account/account.model'
15 import { VideoDetails } from '../../shared/video/video-details.model'
16 import { Video } from '../../shared/video/video.model'
17 import { VideoService } from '../../shared/video/video.service'
18 import { MarkdownService } from '../shared'
19 import { VideoDownloadComponent } from './modal/video-download.component'
20 import { VideoReportComponent } from './modal/video-report.component'
21 import { VideoShareComponent } from './modal/video-share.component'
22
23 @Component({
24 selector: 'my-video-watch',
25 templateUrl: './video-watch.component.html',
26 styleUrls: [ './video-watch.component.scss' ]
27 })
28 export class VideoWatchComponent implements OnInit, OnDestroy {
29 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
30 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
31 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
32 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
33
34 otherVideosDisplayed: Video[] = []
35
36 error = false
37 player: videojs.Player
38 playerElement: HTMLVideoElement
39 userRating: UserVideoRateType = null
40 video: VideoDetails = null
41 videoPlayerLoaded = false
42 videoNotFound = false
43 descriptionLoading = false
44
45 completeDescriptionShown = false
46 completeVideoDescription: string
47 shortVideoDescription: string
48 videoHTMLDescription = ''
49 likesBarTooltipText = ''
50
51 private otherVideos: Video[] = []
52 private paramsSub: Subscription
53
54 constructor (
55 private elementRef: ElementRef,
56 private route: ActivatedRoute,
57 private router: Router,
58 private videoService: VideoService,
59 private videoBlacklistService: VideoBlacklistService,
60 private confirmService: ConfirmService,
61 private metaService: MetaService,
62 private authService: AuthService,
63 private notificationsService: NotificationsService,
64 private markdownService: MarkdownService,
65 private zone: NgZone
66 ) {}
67
68 get user () {
69 return this.authService.getUser()
70 }
71
72 ngOnInit () {
73 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
74 .subscribe(
75 data => {
76 this.otherVideos = data.videos
77 this.updateOtherVideosDisplayed()
78 },
79
80 err => console.error(err)
81 )
82
83 this.paramsSub = this.route.params.subscribe(routeParams => {
84 if (this.videoPlayerLoaded) {
85 this.player.pause()
86 }
87
88 const uuid = routeParams['uuid']
89 // Video did not changed
90 if (this.video && this.video.uuid === uuid) return
91
92 this.videoService.getVideo(uuid).subscribe(
93 video => this.onVideoFetched(video),
94
95 error => {
96 this.videoNotFound = true
97 console.error(error)
98 }
99 )
100 })
101 }
102
103 ngOnDestroy () {
104 // Remove player if it exists
105 if (this.videoPlayerLoaded === true) {
106 videojs(this.playerElement).dispose()
107 }
108
109 // Unsubscribe subscriptions
110 this.paramsSub.unsubscribe()
111 }
112
113 setLike () {
114 if (this.isUserLoggedIn() === false) return
115 if (this.userRating === 'like') {
116 // Already liked this video
117 this.setRating('none')
118 } else {
119 this.setRating('like')
120 }
121 }
122
123 setDislike () {
124 if (this.isUserLoggedIn() === false) return
125 if (this.userRating === 'dislike') {
126 // Already disliked this video
127 this.setRating('none')
128 } else {
129 this.setRating('dislike')
130 }
131 }
132
133 blacklistVideo (event: Event) {
134 event.preventDefault()
135
136 this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist').subscribe(
137 res => {
138 if (res === false) return
139
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 },
146
147 error => this.notificationsService.error('Error', error.message)
148 )
149 }
150 )
151 }
152
153 showMoreDescription () {
154 if (this.completeVideoDescription === undefined) {
155 return this.loadCompleteDescription()
156 }
157
158 this.updateVideoDescription(this.completeVideoDescription)
159 this.completeDescriptionShown = true
160 }
161
162 showLessDescription () {
163 this.updateVideoDescription(this.shortVideoDescription)
164 this.completeDescriptionShown = false
165 }
166
167 loadCompleteDescription () {
168 this.descriptionLoading = true
169
170 this.videoService.loadCompleteDescription(this.video.descriptionPath)
171 .subscribe(
172 description => {
173 this.completeDescriptionShown = true
174 this.descriptionLoading = false
175
176 this.shortVideoDescription = this.video.description
177 this.completeVideoDescription = description
178
179 this.updateVideoDescription(this.completeVideoDescription)
180 },
181
182 error => {
183 this.descriptionLoading = false
184 this.notificationsService.error('Error', error.message)
185 }
186 )
187 }
188
189 showReportModal (event: Event) {
190 event.preventDefault()
191 this.videoReportModal.show()
192 }
193
194 showSupportModal () {
195 this.videoSupportModal.show()
196 }
197
198 showShareModal () {
199 this.videoShareModal.show()
200 }
201
202 showDownloadModal (event: Event) {
203 event.preventDefault()
204 this.videoDownloadModal.show()
205 }
206
207 isUserLoggedIn () {
208 return this.authService.isLoggedIn()
209 }
210
211 isVideoUpdatable () {
212 return this.video.isUpdatableBy(this.authService.getUser())
213 }
214
215 isVideoBlacklistable () {
216 return this.video.isBlackistableBy(this.user)
217 }
218
219 getAvatarPath () {
220 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
221 }
222
223 getVideoPoster () {
224 if (!this.video) return ''
225
226 return this.video.previewUrl
227 }
228
229 getVideoTags () {
230 if (!this.video || Array.isArray(this.video.tags) === false) return []
231
232 return this.video.tags.join(', ')
233 }
234
235 isVideoRemovable () {
236 return this.video.isRemovableBy(this.authService.getUser())
237 }
238
239 removeVideo (event: Event) {
240 event.preventDefault()
241
242 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
243 .subscribe(
244 res => {
245 if (res === false) return
246
247 this.videoService.removeVideo(this.video.id)
248 .subscribe(
249 status => {
250 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
251
252 // Go back to the video-list.
253 this.router.navigate([ '/videos/list' ])
254 },
255
256 error => this.notificationsService.error('Error', error.message)
257 )
258 }
259 )
260 }
261
262 private updateVideoDescription (description: string) {
263 this.video.description = description
264 this.setVideoDescriptionHTML()
265 }
266
267 private setVideoDescriptionHTML () {
268 if (!this.video.description) {
269 this.videoHTMLDescription = ''
270 return
271 }
272
273 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
274 }
275
276 private setVideoLikesBarTooltipText () {
277 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
278 }
279
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
293 private checkUserRating () {
294 // Unlogged users do not have ratings
295 if (this.isUserLoggedIn() === false) return
296
297 this.videoService.getUserVideoRating(this.video.id)
298 .subscribe(
299 ratingObject => {
300 if (ratingObject) {
301 this.userRating = ratingObject.rating
302 }
303 },
304
305 err => this.notificationsService.error('Error', err.message)
306 )
307 }
308
309 private onVideoFetched (video: VideoDetails) {
310 this.video = video
311
312 this.updateOtherVideosDisplayed()
313
314 let observable
315 if (this.video.isVideoNSFWForUser(this.user)) {
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 )
320 } else {
321 observable = Observable.of(true)
322 }
323
324 observable.subscribe(
325 res => {
326 if (res === false) {
327
328 return this.router.navigate([ '/videos/list' ])
329 }
330
331 // Player was already loaded
332 if (this.videoPlayerLoaded !== true) {
333 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
334
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 }
339
340 const videojsOptions = {
341 controls: true,
342 autoplay: this.isAutoplay(),
343 plugins: {
344 peertube: {
345 videoFiles: this.video.files,
346 playerElement: this.playerElement,
347 peerTubeLink: false,
348 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
349 videoDuration: this.video.duration
350 },
351 hotkeys: {
352 enableVolumeScroll: false
353 }
354 }
355 }
356
357 this.videoPlayerLoaded = true
358
359 const self = this
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 })
366 })
367 })
368 } else {
369 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
370 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
371 }
372
373 this.setVideoDescriptionHTML()
374 this.setVideoLikesBarTooltipText()
375
376 this.setOpenGraphTags()
377 this.checkUserRating()
378 }
379 )
380 }
381
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
407 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
408 let likesToIncrement = 0
409 let dislikesToIncrement = 0
410
411 if (oldRating) {
412 if (oldRating === 'like') likesToIncrement--
413 if (oldRating === 'dislike') dislikesToIncrement--
414 }
415
416 if (newRating === 'like') likesToIncrement++
417 if (newRating === 'dislike') dislikesToIncrement++
418
419 this.video.likes += likesToIncrement
420 this.video.dislikes += dislikesToIncrement
421 }
422
423 private updateOtherVideosDisplayed () {
424 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
425 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
426 }
427 }
428
429 private setOpenGraphTags () {
430 this.metaService.setTitle(this.video.name)
431
432 this.metaService.setTag('og:type', 'video')
433
434 this.metaService.setTag('og:title', this.video.name)
435 this.metaService.setTag('name', this.video.name)
436
437 this.metaService.setTag('og:description', this.video.description)
438 this.metaService.setTag('description', this.video.description)
439
440 this.metaService.setTag('og:image', this.video.previewPath)
441
442 this.metaService.setTag('og:duration', this.video.duration.toString())
443
444 this.metaService.setTag('og:site_name', 'PeerTube')
445
446 this.metaService.setTag('og:url', window.location.href)
447 this.metaService.setTag('url', window.location.href)
448 }
449
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 }
457 }