]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/video-watch.component.ts
Begin to add avatar to actors
[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 './modal/video-download.component'
18 import { VideoReportComponent } from './modal/video-report.component'
19 import { VideoShareComponent } from './modal/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.message)
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.message)
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 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 getVideoTags () {
224 if (!this.video || Array.isArray(this.video.tags) === false) return []
225
226 return this.video.tags.join(', ')
227 }
228
229 isVideoRemovable () {
230 return this.video.isRemovableBy(this.authService.getUser())
231 }
232
233 removeVideo (event: Event) {
234 event.preventDefault()
235
236 this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
237 .subscribe(
238 res => {
239 if (res === false) return
240
241 this.videoService.removeVideo(this.video.id)
242 .subscribe(
243 status => {
244 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
245
246 // Go back to the video-list.
247 this.router.navigate([ '/videos/list' ])
248 },
249
250 error => this.notificationsService.error('Error', error.message)
251 )
252 }
253 )
254 }
255
256 private updateVideoDescription (description: string) {
257 this.video.description = description
258 this.setVideoDescriptionHTML()
259 }
260
261 private setVideoDescriptionHTML () {
262 if (!this.video.description) {
263 this.videoHTMLDescription = ''
264 return
265 }
266
267 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
268 }
269
270 private setVideoLikesBarTooltipText () {
271 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
272 }
273
274 private handleError (err: any) {
275 const errorMessage: string = typeof err === 'string' ? err : err.message
276 let message = ''
277
278 if (errorMessage.indexOf('http error') !== -1) {
279 message = 'Cannot fetch video from server, maybe down.'
280 } else {
281 message = errorMessage
282 }
283
284 this.notificationsService.error('Error', message)
285 }
286
287 private checkUserRating () {
288 // Unlogged users do not have ratings
289 if (this.isUserLoggedIn() === false) return
290
291 this.videoService.getUserVideoRating(this.video.id)
292 .subscribe(
293 ratingObject => {
294 if (ratingObject) {
295 this.userRating = ratingObject.rating
296 }
297 },
298
299 err => this.notificationsService.error('Error', err.message)
300 )
301 }
302
303 private onVideoFetched (video: VideoDetails) {
304 this.video = video
305
306 let observable
307 if (this.video.isVideoNSFWForUser(this.user)) {
308 observable = this.confirmService.confirm(
309 'This video contains mature or explicit content. Are you sure you want to watch it?',
310 'Mature or explicit content'
311 )
312 } else {
313 observable = Observable.of(true)
314 }
315
316 observable.subscribe(
317 res => {
318 if (res === false) {
319
320 return this.router.navigate([ '/videos/list' ])
321 }
322
323 // Player was already loaded
324 if (this.videoPlayerLoaded !== true) {
325 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
326
327 // If autoplay is true, we don't really need a poster
328 if (this.isAutoplay() === false) {
329 this.playerElement.poster = this.video.previewUrl
330 }
331
332 const videojsOptions = {
333 controls: true,
334 autoplay: this.isAutoplay(),
335 plugins: {
336 peertube: {
337 videoFiles: this.video.files,
338 playerElement: this.playerElement,
339 autoplay: this.isAutoplay(),
340 peerTubeLink: false
341 }
342 }
343 }
344
345 this.videoPlayerLoaded = true
346
347 const self = this
348 videojs(this.playerElement, videojsOptions, function () {
349 self.player = this
350 this.on('customError', (event, data) => {
351 self.handleError(data.err)
352 })
353 })
354 } else {
355 (this.player as any).setVideoFiles(this.video.files)
356 }
357
358 this.setVideoDescriptionHTML()
359 this.setVideoLikesBarTooltipText()
360
361 this.setOpenGraphTags()
362 this.checkUserRating()
363
364 this.prepareViewAdd()
365 }
366 )
367 }
368
369 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
370 let likesToIncrement = 0
371 let dislikesToIncrement = 0
372
373 if (oldRating) {
374 if (oldRating === 'like') likesToIncrement--
375 if (oldRating === 'dislike') dislikesToIncrement--
376 }
377
378 if (newRating === 'like') likesToIncrement++
379 if (newRating === 'dislike') dislikesToIncrement++
380
381 this.video.likes += likesToIncrement
382 this.video.dislikes += dislikesToIncrement
383 }
384
385 private setOpenGraphTags () {
386 this.metaService.setTitle(this.video.name)
387
388 this.metaService.setTag('og:type', 'video')
389
390 this.metaService.setTag('og:title', this.video.name)
391 this.metaService.setTag('name', this.video.name)
392
393 this.metaService.setTag('og:description', this.video.description)
394 this.metaService.setTag('description', this.video.description)
395
396 this.metaService.setTag('og:image', this.video.previewPath)
397
398 this.metaService.setTag('og:duration', this.video.duration.toString())
399
400 this.metaService.setTag('og:site_name', 'PeerTube')
401
402 this.metaService.setTag('og:url', window.location.href)
403 this.metaService.setTag('url', window.location.href)
404 }
405
406 private prepareViewAdd () {
407 // After 30 seconds (or 3/4 of the video), increment add a view
408 let viewTimeoutSeconds = 30
409 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
410
411 setTimeout(() => {
412 this.videoService
413 .viewVideo(this.video.uuid)
414 .subscribe()
415
416 }, viewTimeoutSeconds * 1000)
417 }
418
419 private isAutoplay () {
420 // True by default
421 if (!this.user) return true
422
423 // Be sure the autoPlay is set to false
424 return this.user.autoPlayVideo !== false
425 }
426 }