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